#!/usr/bin/perl

use Getopt::Long;
use File::Copy;

$preCorrect = substr($0,0,2);

$correctname=substr($0,0);
#print "\$correctname = $correctname\n";

use lib "/mt/apps/gpms/DateTrans/lib";
use DateTrans qw(monName monNumber timeFormat AmPmFormat dayNumbeFormat);

use lib "/mt/apps/gpms/DateTrans/lib";
use  xsdxsltcheck qw(xsdcheck xsltcheck FGDC2MRFcheck xsl4mrf2fgdccheck);

use File::Basename;

sub usage() {
    print "Usage: perl $correctname [-file=<file>][-output_dir=<output_dir>][-profile=<profile>]\n";
    print "Example:\n";
    print "perl $correctname -file=filename -output_dir=outputDirectory -profile=profile \n";
    print "where -file is the full path of the file to be validated\n";
    print "-output_dir is the full path where the newly created files are going\n";
    print "and -profile is the profile of the file. This value is either v for vector files or k for KML files\n";
};

%gOptions;
&GetOptions(\%gOptions, qw(file=s output_dir=s profile=s));
unless ($gOptions{file} and $gOptions{output_dir} and $gOptions{profile}) {
        usage();
        exit(1);
};

print "This program is working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "version: MRF2ISO_FGDC_Single_Ver1\n";


#---------------------------------------------------------------constansts----------------------------------------------------------------------
$root = "/mt/apps";
#          /mtdata/geo/shpgen/tigerline/fe_2007/
$rootout="/mtdata/geo";

#--------------------------------------------------------------dealing with the directory-------------------------------------------------------------
chomp($FullFile=$gOptions{file});
chomp($outDir=$gOptions{output_dir});
chomp($profile=$gOptions{profile});

print "\$FullFile = $FullFile\n";
print "\$outDir = $outDir\n";
print "\$profile= $profile\n";

$inlist="$rootout/gpms/input/list";
$errlogdir ="$rootout/gpms/log";
$combolist = $inlist . "/" . $Infile;
$fgdcdir = "$rootout/gpms/output/fgdc";
$stylesheet ="$root/gpms/FGDC2MRF/MIF2MRF.xsl";
$ISOdir="$rootout/gpms/output/ISO";
$SeriesInfo="$rootout/gpms/output/ISOParent";
$EADir= "EAFiles";
$newdir=$Branch . $dateString;
#/mtdata001/geo/gpms/output/ISOParent
chmod 0777, "$SeriesInfo";

#-----------------------------------------------------counter variable mangement section--------------------------------------------------------------------
$invalidcounter=0;
$badxmlcounter=0;
$nopostcounter=0;
$fgdcoutcounter=0;
$fgdcoutcountera=0;
$flag_array_countera=0;
$postarraycounter=0;
$passcounter=0;
$mifarraycounter=0;
#------------------------------------------------------error trap--------------------------------------------------------------------------------------------
&xsdcheck;
&xsltcheck;
&FGDC2MRFcheck;
&xsl4mrf2fgdccheck;


#-----------------------------------redirecting the output---------------------------------------------------------------------------------------------------
open (SAVEOUT,">&STDOUT");

#----------------------------------------------------------Saxon-----------------------------------------------------------------------------------------


$ENV{'PATH'} = "/apps/saxon/jdk1.5.0_24/bin::/apps/saxon/saxon9ee.jar::/apps/saxon:$rootout/mrf/$newerrdir:$ENV{'PATH'}";

$cp = "CLASSPATH";#goodone
$ENV{$cp} = "/apps/saxon/jdk1.5.0_24/lib/:/apps/saxon/saxon9ee.jar:/apps/saxon/saxon9ee.jar $CLASSPATH:/usr/share/java/saxon.jar";


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------




if ($profile eq 'v' or $profile eq 'V'){
print "dealing with vector files!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
$percentVectorCounter++;
$vectorIndicatorCounter++;

print SAVEOUT "now validating $FullFile against the Vector Profile-----------------------------\n";
$slashpos= rindex($FullFile, "/") +1;
$newfilename = substr($FullFile,$slashpos);
$dotlocation = index($newfilename, ".");
$finalfilename=substr($newfilename, 0, $dotlocation);

if ($valcounter >0){
close STDERR;
system "rm $TMP";
};

$MRF = $finalfilename . ".mrf";
$TMP = "TMP" . "-" . $newfilename;
$valcounter++;



#making the error file and redirecting the STDERR to the error file
#have a new $dotlocation variable because the $filename and $newfilename variables are different lengths
$errmain= substr($newfilename, 0, $dotlocation);
$errfile = $errmain .".err";
open STDERR,">$errfile";




$filecounter++;
my @csax = "java com.saxonica.Validate -t -val:strict  -s:$FullFile -xsd:/mt/apps/gpms/xsd/Vector.xsd";
my $mrf = system(@csax);
$SizeErrorFile= -s $errfile;


open ERRHANDLE, "$errfile ";
while (<ERRHANDLE>){
  if($_=~/does not exist/){
   $doesNotExist[$doesNotExistCounter]=$errfile;
   $doesNotExistCounter++;
   #system "rm $errfile";
   system "rm $TMP";
 }elsif ($_=~/Validation error/){
 $mrf =512;
};
};
close ERRHANDLE;

print "\$mrf = $mrf\n";
if ($mrf == 0){
system "rm $errfile";
}elsif ($mrf != 0){

#print SAVEOUT "We have a failure!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
$failArray[$failArrayCounter]=$vectorArray;
$reRunArray[$failArrayCounter]="$vectorArray v p";
$failArrayCounter++;

system "cp $indir/$vectorArray $newerrdir";
system "mv $errfile $newerrdir";

};

}elsif($profile eq 'k' or $profile eq 'K'){

};
