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

$correctname=substr($0,2);
#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 [-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";

#$indir="$rootout/gpms/input/fgdc";
$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";
$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){
#print "The file $combolist has files\n";
}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);
};

#$outputdir
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 $monnum = monName($mon);
my $monName=monName($mon);
my $monnum =monNumber($mon);
#my $Month_Number = monNumber($mon);

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

#rint "\$dateString = $dateString\n";
#print "\$year = $year\n"; 
#print "\$monnum = $monnum\n";
#print "\$day = $day\n";
#print "\$daynum = $daynum\n";
#print "\$hour = $hour\n";
#print "\$min = $min\n";

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


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

#-----------------------------------------making the directories------------------------------------------------
$newdir=$Branch . $dateString;
#system "mkdir $rootout/gpms/output/mrf/$newdir";
#system "mkdir $fgdcdir/$newdir";
$newerrdir ="$errlogdir/$newdir";
system "mkdir $newerrdir";
system "mkdir $ISOdir/$newdir";
#chmod 0777, "$outputdir/$newdir";
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;
#print "reading $combolist (reading list)\n";
open INPUTFILE, "$combolist";
while(<INPUTFILE>){

$slashpos= rindex($_, "/");
#print "getting slash position--------------\n";
if ($slashpos < 0){


$filenameflag=$_;
$noflagarray[$flag_array_counter]=$filenameflag;
$flag_array_counter++;
$flagpos=rindex($_,".");
$flagless=substr($_, 0,$flagpos+4);
#print "\$flagless = $flagless\n";
#chomp($flagless);
$finalarray[$finalarraycounter]=$flagless;
#print SAVEOUT "\$finalarray[$finalarraycounter] = $finalarray[$finalarraycounter]\n";
$finalarraycounter++;

}elsif($slashpos > 0){

#for filenames with directories
#$slashfilename = $newdir . "/" . $infile;
#print "------------------------------------------\n";
#print "\$_ = $_\n";
my $basename = basename $_;
my $dirnameA = dirname $_;
#print "\$basename = $basename\n";
#print "\$dirnameA = $dirnameA\n";

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

$flagpos=rindex($_,".");
#print "\$flagpos = $flagpos\n";
$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

$iso_var= "ISO_" . $finalarray;

#$iso_var_ea= "ISO_EA_" . $finalarray;
#print "\$iso_var = $iso_var\n"; 

open STDOUT,">$iso_var";
open STDERR, ">$iso_var.err";
#rint SAVEOUT "\

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

#print SAVEOUT "\$mif = $mif\n";

if ($mif == 0){
#print SAVEOUT "moving the ISO file to $ISOdir\/$newdir!\n";
system "mv $iso_var $ISOdir/$newdir";
system "rm $iso_var.err";

#close STDOUT;
#open STDOUT,">$iso_var_ea";
#open STDERR, ">$iso_var_ea.err";
#print SAVEOUT "Now making the Entity and attribute and Info document \n";
#my @csaxea = "java com.saxonica.Transform -vw  $indir/$finalarray  $root/gpms/IsoSchema/Sec52iso.xslt";
#my $mifea = system(@csax);
#print SAVEOUT  "\$mifea =  $mifea\n";

#if ($mifea == 0){
#print SAVEOUT "now moving the EA fire \n";
#system "mv $iso_var_ea $ISOdir/$newdir";
#system "rm $iso_var_ea.err";
#close STDOUT;
#}else{
#system "mv $iso_var_ea $newerrdir";
#system "mv $iso_var_ea.err $newerrdir";
#$badFile[$badFileCounter] =$finalarray;
#$badFileCounter++;
#};


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

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

print SAVEOUT "Now making the Attribute and Info XSLT by transforming the files into the ISO 19110 format\n"; 
foreach $finalarray  (@finalarray){
$iso_var_ea= "ISO_EA_" . $finalarray;
$iso_var_ea_error = "ISO_EA_" . $finalarray . ".err";

open STDOUT,">$iso_var_ea";
open STDERR, ">$iso_var_ea_error";
#print SAVEOUT "Now making the Entity and attribute and Info document \n";
my @csaxea = "java com.saxonica.Transform -vw  $indir/$finalarray  $root/gpms/IsoSchema/Sec52iso.xslt";
my $mifea = system(@csaxea);
#print SAVEOUT  "\$mifea =  $mifea\n";
 
if ($mifea == 0){
#print SAVEOUT "now moving the EA file \n";
system "mv $iso_var_ea $ISOdir/$newdir";
system "rm $iso_var_ea.err";
close STDOUT;
}else{ 
system "mv $iso_var_ea $newerrdir";
system "mv $iso_var_ea_error $newerrdir";
$badFile[$badFileCounter] =$finalarray;
$badFileCounter++;
}; 



};
#-----------------------------------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";
#print SAVEOUT "\$logname = $logname\n";
#print SAVEOUT "\$newerrdir = $newerrdir\n";

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

#print SAVEOUT "\$finalarrayCounter = $finalarrayCounter\n";
if ( $finalarrayCounter >1){
print SAVEOUT "The following files were transformed into an ISO 19115 format metadata files:\n"; 
print LOG "The following files were transformed into an ISO 19115 format metadata files:\n";
}elsif ($finalarrayCounter == 1){
print SAVEOUT "The following file was transformed into an ISO 19115 format metadata file:\n";
print LOG "The following file was transformed into an ISO 19115 format metadata file:\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.";
};

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


