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

$correctname=substr($0,2);

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 [-listfile=<listfile>][-directory=<directory>][-product=<product>]\n";
    print "Example:\n";
    print "perl $correctname -listfile=list.txt -directory=/dir/subdir -product=TLFE2007\n";
    print"where list.txt is the filename of the list file containg the names of the files to validated,\n";
       print "/dir/subdir is the directory containing the files to be validated\n";
       print "and TLFE2007 is the product.\n";
    print "The following keyword shortcuts are also available for the directory:\n";
       print "mtdata for /mtdata/geo/gpms/input/fgdc\n";
       print "nfs for /nfs/gpms/input/fgdc\n";
       print "prodmtdata for /prodmtdata/geo/gpms/input/fgdc\n";

}
%gOptions;
&GetOptions(\%gOptions, qw(listfile=s directory=s  product=s));
unless ($gOptions{listfile} and $gOptions{directory} and $gOptions{product}) {
        usage();
        exit(1);
};

#-----------------------------------------------Notes Section-------------------------------------------------------------------------------------------
#Written by Matthew McCready
#This script takes a FGDC file and transforms it into an ISO 19115, 19139 and 19110 file.

#---------------------------------------------file mangement section----------------------------------------------------------------------------------------
$Infile = $gOptions{listfile};
chomp($Branch = $gOptions{product});
chomp($indir= $gOptions{directory});

#enabling keywords for the input directory
if ($indir eq "mtdata"){
$indir = "/mtdata/geo/gpms/input/fgdc";
}elsif ($indir eq "nfs"){
$indir = "/nfs/gpms/input/fgdc";
}elsif ($indir eq "prodmtdata"){
$indir = "/prodmtdata/geo/gpms/input/fgdc";
}elsif ($indir eq "GPMSscripts"){
$indir = "/home/mccre004/GPMSscripts";
};

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

$inlist="$rootout/gpms/input/list";
$outputdir ="$rootout/gpms/output/mrf";
$errlogdir ="$rootout/gpms/log";
$combolist = $inlist . "/" . $Infile;
$fgdcdir = "$rootout/gpms/output/fgdc";
$stylesheet ="$root/gpms/FGDC2MRF/MIF2MRF.xsl";
$ISOdir="$rootout/gpms/output/ISO";
$EADir= "EAFiles";
$newdir=$Branch . $dateString;

#-----------------------------------------------------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;

if (-e $indir){
print "Please Wait, Now processing the files ----------------------------------------------\n\n";
}else{
print "The directory $indir  could not be found! \n";
print "Please make sure the pathname, $indir, was spelled correctly and that it exists.\n";
exit(1);
};

if (-s $indir){
}else{
print "The directory $indir does not contain any files to be validated! \n";
exit(1);
};

if (-r $indir){
}else{
print "The directory $indir is not readable!!!!!!!!!!!!!!!!!!!!!\n";
print "Please make sure you have the permissions to read the directory. \n";
exit(1);
};

if (-w $indir){
}else{
print "The directory $indir is not writeable!!!!!!!!!!!!!!!!!!!!!\n";
print "Please make sure you have the permissions to write to the directory. \n";
exit(1);
};

if (-e $outputdir){
}else{
print "The directory $outputdir  could not be found! \n";
print "Please make sure the pathname, $outputdir, was spelled correctly and that it exists.\n";
exit(1);
};

if (-w $outputdir){
}else{
print "The directory $outputdir can not be written to!!!!!!!!!! \n";
print "Please make sure that you have the permissions needed to write to the directory.\n";
exit(1);
};

if (-e $combolist){
}else{
print "The file $combolist could not be found! \n";
print "Please make sure the file, $Infile,  was spelled correctly and that the file is in the $inlist directory.\n";
exit(1);
};

#making sure the file list contains something

if (-s $combolist>2){
}else{
$size=-s $combolist;
print "The file $combolist does not contain the list of files to be validated! (It has a size of $size kb) \n";
print "Please make sure the file, $Infile,  contains a list of files to be validated.\n";
exit(1);
};

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

my $now=`date`;
$mon = substr($now,4,3);
$day=substr($now,8,2);
$year =substr($now,24,4);
$hour = substr($now,11,2);
$min = substr($now,14,2);
my $daynum= dayNumbeFormat($day);

#converting the date to a number
my $monName=monName($mon);
my $monnum =monNumber($mon);

$dateString =$year . $monnum . $daynum . $hour . $min;


$time = $hour . ":" . $min;
$oldhour = $hour;
my $newhour =timeFormat($hour);
my $ampm =AmPmFormat($oldhour);


$newtime = $newhour . ":" . $min . " $ampm";

#-----------------------------------------making the directories------------------------------------------------
$newdir=$Branch . $dateString;
$newerrdir ="$errlogdir/$newdir";
system "mkdir $newerrdir";
system "mkdir $ISOdir/$newdir";
system "mkdir $ISOdir/$newdir/$EADir";
chmod 0777, "$newerrdir";
chmod 0777, "$fgdcdir/$newdir";
chmod 0777, " $ISOdir/$newdir";

#-----------------------------------------Filename array creation section------------------------------------------------------------------------------
#open a filehamdle for the file containing the filenames
$flag_array_counter=0;
open INPUTFILE, "$combolist";
while(<INPUTFILE>){

$slashpos= rindex($_, "/");
if ($slashpos < 0){


$filenameflag=$_;
$noflagarray[$flag_array_counter]=$filenameflag;
$flag_array_counter++;
$flagpos=rindex($_,".");
$flagless=substr($_, 0,$flagpos+4);
$finalarray[$finalarraycounter]=$flagless;
$finalarraycounter++;

}elsif($slashpos > 0){

my $basename = basename $_;
my $dirnameA = dirname $_;

$slash_directory_name[$slash_directory_name_counter]=$dirnameA;
$slash_directory_name_counter++;

$flagpos=rindex($_,".");
$flagless=substr($_, 0,$flagpos+4);#was 3
};
};


#----------------------------------------------------------Saxon-----------------------------------------------------------------------------------------
$ENV{'PATH'} = "/opt/saxon_lib/jdk1.5.0_11/bin:/mt/apps/gpms/output/mrf$newerrdir:$ENV{'PATH'}";#goodone
$cp = "CLASSPATH";#goodone
$ENV{$cp} = "/opt/saxon_lib/jdk1.5.0_11/lib/:/opt/saxon_lib/saxon9/saxon9sa.jar:/opt/saxon_lib/saxon9/saxon9-jdom.jar:/opt/saxon_lib/saxon9/:/opt/saxon_lib/saxon9/saxon9.jar:$ENV{$cp}";
#---------------------------------------------------transforming to ISO Standard------------------------------------------------------------------------

print SAVEOUT "Now Transforming the files to ISO 19115 format-----------------------------------------------------------------------------\n";

foreach $finalarray  (@finalarray){
$finalarrayCounter++;
print SAVEOUT "now reading $finalarray\n";
#/mt/apps/gpms/IsoSchema


$dotLocation=index($finalarray,".");
$finalarranywoutmif=substr($finalarray,0,$dotLocation);
$iso_var= "ISO_" . $finalarranywoutmif . ".xml";


open STDOUT,">$iso_var";
open STDERR, ">$iso_var.err";

my @csax = "java com.saxonica.Transform -vw  $indir/$finalarray  $root/gpms/MIF2ISO/MIF2iso19115-2_master.xslt ";
my $mif = system(@csax);


if ($mif == 0){
system "mv $iso_var $ISOdir/$newdir";
system "rm $iso_var.err";

}else{
system "mv $iso_var $newerrdir";
system "mv $iso_var.err $newerrdir";
$badFile[$badFileCounter] =$finalarray;
$badFileCounter++; 
};

} ;
#####-----------------------------------for the entity and attributes --------------------------------------------------------------------------------------

print SAVEOUT "\nNow making the Attribute and Info XSLT by transforming the files into the ISO 19110 format\n"; 

foreach $finalarray  (@finalarray){
$iso_var_ea= "ISO_EA1_" . $finalarray;
$iso_var_ea_error = "ISO_EA1_" . $finalarray . ".err";

open STDOUT,">$iso_var_ea";
open STDERR, ">$iso_var_ea_error";
my @csaxea = "java com.saxonica.Transform -vw  $indir/$finalarray  $root/gpms/MIF2ISO/EAFile.xsl";#Sec52iso.xslt
my $mifea = system(@csaxea);
$sizeOfEAFile = -s $iso_var_ea;

 
if ($mifea == 0 and $sizeOfEAFile>43){
$FinalEA[$numOfEAFiles]=$finalarray;
$EAFilesArray[$numOfEAFiles]=$iso_var_ea; 
$numOfEAFiles ++;
system "rm $iso_var_ea_error";
close STDOUT;
}elsif($sizeOfEAFile <= 43 and $mifea == 0){
system "rm $iso_var_ea";
system "rm $iso_var_ea_error";
}else{ 
system "mv $iso_var_ea $newerrdir";
system "mv $iso_var_ea_error $newerrdir";
$badFile[$badFileCounter] =$finalarray;
$badFileCounter++;
}; 


};
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
print SAVEOUT "\n";
foreach $FinalEA(@FinalEA){

print SAVEOUT "BeginingXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
print SAVEOUT "\$FinalEA = $FinalEA\n";
$CodesetNumber=1;
$EnumeratedDomainValueNumber=1;
$MIFLocationA=index($FinalEA,"MIF_");
$MIFLocation=index($FinalEA,"MIF_")+4;

if($MIFLocationA >0){
#print SAVEOUT "in the MIF Section!!!!!!!!!\n";
$FirstFile=substr($FinalEA,$MIFLocation);
$MIFYesNo="Yes";
}else{
print SAVEOUT "in the Regular Section!!!!!!!!!\n";
$FirstFile = $FinalEA;
$MIFYesNo="No";
};

#print SAVEOUT "\$MIFLocation = $MIFLocation\n";
#print SAVEOUT "\$FirstFile = $FirstFile\n";
#print SAVEOUT " ----------------------------------------\n";
open OLDEA, "<$indir/$FinalEA";

while(<OLDEA>){
$Line=$_;
if ($Line=~/Codeset_Name/){
$FirstBracketLocation=index($Line,"gid3")+9;
$LastBracketLocation-index($Line,"</");
$LineLength=$LastBracketLocation-$FirstBracketLocation;
$NewLine=substr($Line,$FirstBracketLocation,$LineLength);
$CodesetLine[$CodesetNumber]=$NewLine;
$CodesetNumber++;

}elsif($Line=~/Enumerated_Domain_Value /){
$FirstBracketLocation=index($Line,"gid3")+9;
$LastBracketLocation=index($Line,"</");
$LineLength=$LastBracketLocation-$FirstBracketLocation;
$NewLine=substr($Line,$FirstBracketLocation,$LineLength);
$EnumeratedDomainValue[$EnumeratedDomainValueNumber]=$NewLine;
$EnumeratedDomainValueNumber++;
}else{
};
};

$CodesetNumberTwo=1;
$EnumeratedDomainValueNumberTwo=1;

print SAVEOUT "222222222222222 In the second loop  222222222222222222222222222\n";
foreach $EAFilesArray (@EAFilesArray){
#print SAVEOUT "\$EAFilesArray = $EAFilesArray \n";

$MIFLocationEA=index($EAFilesArray,"MIF_")+4;

print SAVEOUT "\$MIFYesNo = $MIFYesNo\n";

if($MIFYesNo eq "Yes"){
#print SAVEOUT "\$EAFilesArray = $EAFilesArray (in the yes section)\n";
$secondFile=substr($EAFilesArray,$MIFLocationEA);
#$secondFile = $EAFilesArray;
#open OLDISO, "<$EAFilesArray";
}else{
print SAVEOUT "\$EAFilesArray = $EAFilesArray (in the else section)\n";
$MIFLocationD=index($EAFilesArray,"MIF");
print SAVEOUT "\$MIFLocationD = $MIFLocationD\n";

if ($MIFLocationD <0){
$secondFile =substr($EAFilesArray,8);
print SAVEOUT "\$secondFile = $secondFile\n";

};

#rint SAVEOUT "\$secondFile = $secondFile\n";
#print SAVEOUT "-----------\nIn the non MIF section\n";
#$secondFile= " ISO_EA_". $EAFilesArray;
#$secondFile= $EAFilesArray;

#print SAVEOUT"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n";
#print SAVEOUT"\$EAFilesArray = $EAFilesArray\n";
#$ISO_EALocation=index($EAFilesArray,"ISO_EA")+7;
#$secondFile =substr($EAFilesArray,$ISO_EALocation);
#print SAVEOUT "\$ISO_EALocation = $ISO_EALocation\n";
#print SAVEOUT "\$secondFile = $secondFile\n\n";
#print SAVEOUT"ssssssssssssssssssssssssssssssssssssssssssssssssssssssssssss\n\n\n";
};

$NewISOFile= "ISO_EA_" . $secondFile;

#print SAVEOUT "\nMatchbox --------------------------\n"; 
#print SAVEOUT "\$FirstFile = $FirstFile\n";
#print SAVEOUT "\$secondFile = $secondFile\n";
#print SAVEOUT "End of Matchbox -------------------\n\n";

if ($FirstFile eq $secondFile){
print SAVEOUT "Got a match!!!!!!!!!!!!!\n";
print SAVEOUT "\$EAFilesArray = $EAFilesArray\n";
open OLDISO, "<$EAFilesArray";
print SAVEOUT "error: $!\n";
open NEWISO, ">$NewISOFile";

while (<OLDISO>){
$LineISO= $_;
if ($LineISO =~/Put Codeset_Name here!!/){
print NEWISO "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\" >$CodesetLine[$CodesetNumberTwo]  </gco:CharacterString>\n";
$CodesetNumberTwo++;
}elsif($LineISO =~/Put Enumerated_Domain_Value here!!/){
print NEWISO "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\" > $EnumeratedDomainValue[$EnumeratedDomainValueNumberTwo] </gco:CharacterString>\n";
$EnumeratedDomainValueNumberTwo++;
}else{
print NEWISO "$LineISO";
};#e
};

print SAVEOUT "End of the inner Loop XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
};

system "mv $NewISOFile $ISOdir/$newdir/$EADir";
#system "rm $EAFilesArray";
close NEWISO;


};
print SAVEOUT "End of Outer Loop YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY\n\n";

};
#-----------------------------------getting the total number of files------------------------------------------------------------------------------------------
print SAVEOUT "Now calculating the total number of files-------------------------------------------------------------------------------\n\n";
$total=$finalarrayCounter+$goodcounter+$badFileCounter;

if ($finalarrayCounter>0){
$perTrans=($finalarrayCounter/$total)*100;
}else{
$perTrans=0;
};

if ($badFileCounter>0){
$perBad=($badFileCounter/$total)*100
}else{
$perBad=0;
};

#--------------------------setting up the log file-------------------------------------------------------------------------------------------------------------
$logname="$newdir.log";

open LOG,">$newerrdir/$logname";

print LOG "This log details the transformation of $total the FGDC files in the $Infile listfile to the ISO 19115 format. \n ";
print LOG "These files can be found in the $indir directory. ";
print LOG "It was run on $monnum/$daynum/$year at $time ($newtime) for the $Branch product.\n\n"; 

print LOG "Of these $total files, $finalarrayCounter ,or $perTrans%, were successfully transformed into the ISO format and $badFileCounter, or $perBad%, were not \n";
print LOG "transformed because of errors in the FGDC file.\n";
print LOG "Please see the list of files below:\n\n";
#--------------------------indicator section ----------------------------------------------------------------------------------------------------------------------

if ( $finalarrayCounter >1){
print SAVEOUT "The following files were transformed into an ISO 19115 format metadata files. Please see these files at $ISOdir/$newdir:\n"; 
print LOG "The following files were transformed into an ISO 19115 format metadata files. Please see these files at $ISOdir/$newdir:\n";
}elsif ($finalarrayCounter == 1){
print SAVEOUT "The following file was transformed into an ISO 19115 format metadata file. Please see this file at $ISOdir/$newdir:\n";
print LOG "The following file was transformed into an ISO 19115 format metadata file. Please see this file at $ISOdir/$newdir:\n";
};

$goodcounter=1;
foreach $finalarray  (@finalarray){
print SAVEOUT "$goodcounter:  $finalarray\n";
print LOG "$goodcounter:  $finalarray\n";
$goodcounter++;
};

if ($badFileCounter > 1){
print SAVEOUT "\nThe following files could not be transformed into an ISO 19115 format metadata files because they contain errors. Please see the error files and the original xml files  at $newerrdir.\n";
print LOG "\nThe following files could not be transformed into an ISO 19115 format metadata files because they contain errors. Please see the error files and the original xml files at $newerrdir.\n";
}elsif($badFileCounter ==1){
print SAVEOUT "\nThe following file could not be transformed into an ISO 19115 format metadata file because it contains errors. Please see the error file at and the original xml file $newerrdir.\n";
print LOG "\nThe following file could not be transformed into an ISO 19115 format metadata file because it contains errors. Please see the error file and the original xml file at $newerrdir.\n.";
};

if ($numOfEAFiles >0){
print SAVEOUT "\n The following files have had Entity and Attribute and Entity Files (ISO 19110) made for them. Please see them at $ISOdir/$newdir/$EADir:\n";
print LOG  "\n The following files have had Entity and Attribute and Entity Files (ISO 19110) made for them. Please see them at $ISOdir/$newdir/$EADir:\n";

$EAFileCounter=1;
foreach $EAFilesArray (@EAFilesArray){
print SAVEOUT "$EAFileCounter: $EAFilesArray\n";
print LOG   "$EAFileCounter: $EAFilesArray\n";
$EAFileCounter++;
};

};

$badcounter=1;
foreach $badFile (@badFile){
print SAVEOUT "$badcounter:$badFile\n";
print LOG "$badcounter:$badFile\n";
$badcounter++;
};

print SAVEOUT "\n";
