#!/usr/bin/perl

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

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

$correctname=substr($0,0);

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

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

#-----------------------------------------------------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 $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";
system "mkdir $fgdcdir/$newdir";

if (-e $SeriesInfo){
}else{
system "mkdir $SeriesInfo";
};

chmod 0777, "$SeriesInfo";
chmod 0777, "$newerrdir";
chmod 0777, "$fgdcdir/$newdir";
chmod 0777, " $ISOdir/$newdir";
#/mtdata001/geo/gpms/output/ISOParent



#-----------------------------------------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){


#Getting the filename
$filenameflag=$_;
$noflagarray[$flag_array_counter]=$filenameflag;


$flag_array_counter++;
$flagpos=rindex($_,".");
$flagless=substr($_, 0,$flagpos+4);
$ProfileFlag=substr($_,$flagpos+5,1);

if ($ProfileFlag eq 'v'){
$vectorArray[$vectorArrayCounter]=$flagless;
$vectorArrayCounter++;
}elsif ($ProfileFlag eq 'k'){
$kmlArray[$kmlArrayCounter]=$flagless;
};

}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
};
};
#------------------------------total number of files------------------------------------------
$totalNumberOfValidFiles= $vectorArrayCounter + $kmlArrayCounter;
#------------------------------subroutine section--------------------------------------------------
sub mif_is_zero{
$mif=$_[0];
if ($mif==0){
$mifarray[$mifarraycounter]=$unordered;
$mifarraycounter++;
system "rm $unordered.err";
}else{
$badxml[$badxmlcounter]=$unordered;
$badxmlcounter++;
};
};

sub mif_copy{
if (-e $mifFile){
system "cp $mifFile $newmifFile";
}else{
};
};

sub no_exist{
$noexistb = $noexista . ".err";
open NEWERR,">$noexistb";
print NEWERR"The file $noexistb could not be found!\n";
$noexistarray[$existcounter]=$noexistb;
$existcounter++;
system "mv $noexistb $errlogdir/$newdir";
close NEWERR;
};

sub flagless{
$noPostFlagArray[$noPostFlagArray_counter]="$flagless";
$noPostFlagArray_counter++;
};


sub TIGERFileType{
my $aml;
$aml=@_;
#print SAVEOUT "\$aml = $aml\n";
return "$aml";

};

sub PostingFlagD{
$InputFile= @_[0];
$inputDirectory = @_[1];
print SAVEOUT "\$InputFile = $InputFile\n";
print SAVEOUT "\$inputDirectory = $inputDirectory\n";

$CompleteInputFile= $inputDirectory . "/" . $InputFile;
print SAVEOUT "\$CompleteInputFile = $CompleteInputFile\n";
#return $CompleteInputFile;

open STDIN,"<$CompleteInputFile" or die "Unable to open $CompleteInputFile";
print SAVEOUT"pre-loop\n";
while (<STDIN>){
print SAVEOUT"in the loop\n";
$line=$_;
print SAVEOUT "\$line = $line\n";
print SAVEOUT "\$_ = $_\n";
if ($_ =~ /Title_13_Restrictions/){
if ($_ =~ /Yes/){
return "p";
}else{
return "np";
};
}elsif ($_ =~ /MIF/){
return "np";
};
};

print SAVEOUT "Post loop\n";
};

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


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

foreach $vectorArray  (@vectorArray){
#print SAVEOUT "=================================================================================================\n";


$percentVectorCounter++;
$vectorIndicatorCounter++;

if ($vectorIndicatorCounter==10){
$vectorPercentDone =($percentVectorCounter/$vectorArrayCounter)*100;
print SAVEOUT "\n $vectorPercentDone% of the vector files have been validated-----------\n";
$vectorIndicatorCounter=0;
};

print SAVEOUT "now validating $vectorArray against the Vector Profile-----------------------------\n";
$slashpos= rindex($vectorArray, "/") +1;
$newfilename = substr($vectorArray,$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:$indir/$vectorArray -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;



if ($mrf == 0){
$passArray[$passArrayCounter]= $vectorArray;
$passArrayCounter++;
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";



};
}; 


foreach $kmlArray (@kmlArray){

$percentKMLCounter++;
$KMLIndicatorCounter++;

if ($KMLIndicatorCounter==10){
$vectorPercentDone =($percentVectorCounter/$KMLArrayCounter)*100;
print SAVEOUT "\n$vectorPercentDone% of the vector files have been validated-----------\n";
$vectorIndicatorCounter=0;
};



print SAVEOUT "now validating $kmlArray against the KML Profile-----------------------------\n";
#$ISOind="no";
#chomp($finalarray);
#setting up the variables necessary to make the the finalfilename variable, which is needed to make the MRF filerray 
$slashpos= rindex($kmlArray, "/") +1;
$newfilename = substr($kmlArray ,$slashpos);
$dotlocation = index($newfilename, ".");
$finalfilename=substr($newfilename, 0, $dotlocation);

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

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


#open STDOUT,">$TMP";

#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);
$errfileKML = $errmain .".err";
#print SAVEOUT "\$errfileKML = $errfileKML\n";
open STDERR,">$errfileKML";



#starts schema aware transformation. This will cause the transformation to fail if the document is invalid

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

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

if ($mrf == 0){
$passArray[$passArrayCounter]= $kmlArray;
$passArrayCounter++;
system "rm $errfileKML";
}elsif ($mrf != 0){
$failArray[$failArrayCounter]=$kmlArray;
$reRunArray[$failArrayCounter]="$kmlArray k p";
$failArrayCounter++;
system "cp $indir/$kmlArray $newerrdir";
system "mv $errfileKML $newerrdir";
};
};

foreach $passArray (@passArray){
$dotLocation=index($passArray,".");
$mainPart=substr($passArray,0,$dotLocation);
$FgdcFile=$mainPart . ".xml";
$FgdcError =$mainPart . "_fgdc.err";

open STDOUT,">$FgdcFile";
open STDERR,">$FgdcError";

$fileCoversionCounter++;
print SAVEOUT "\n$fileCoversionCounter: Now transforming $passArray to the FGDC format!!!!!!!!!!\n";#-outval:recover
my @csax = "java com.saxonica.Transform  $indir/$passArray  $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);

if ($fgdcout == 0){
system "mv $FgdcFile $fgdcdir/$newdir";
system "rm $passArray";
}else{
system "mv $FgdcFile $newerrdir";
system "mv $FgdcError  $newerrdir"; 
};

$ISOname ="$mainPart.shp.iso.xml";
$ISONAMEerror="$mainPart.shp.iso.err";
open STDOUT,">$ISOname";
open STDERR,">$ISONAMEerror";


print SAVEOUT "$fileCoversionCounter: now transforming $passArray to the ISO 19115-2 format\n";
my @csax = "java com.saxonica.Transform  -outval:recover $indir/$passArray $root/gpms/mrf2ISO/mrf2ISOMaster.xsl";
my $mif = system(@csax);

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



if ($mif ==0){
system "mv $ISOname $ISOdir/$newdir";
}else{
system "mv $ISOname $newerrdir";
system "mv $ISONAMEerror $newerrdir";
};

#one EA file for every file porduced

$firstUnderscoreLocation=index($mainPart,"_");
$preFirst=substr($mainPart,0,$firstUnderscoreLocation+1);
$postFirst=substr($mainPart,$firstUnderscoreLocation+1);

$secondUnderscorelocation=index($postFirst,"_");
$preSecond=substr($postFirst,0,$secondUnderscorelocation+1);
$postSecond=substr($postFirst,$secondUnderscorelocation+1);

$thirdUnderscoreLocation=index($postSecond,"_");
$postThird=substr($postSecond,$thirdUnderscoreLocation+1);


#dealing with the EA files-------------------------------------------------------
$ISOEAName = $preFirst . $preSecond . $postThird . ".shp.ea.iso.xml";

$fullFile = "$ISOdir/$newdir/$EADir/$ISOEAName";
if (-e $fullFile){
}else{
open STDOUT,">$ISOEAName";
open STDERR,">$ISOEANameError";

print SAVEOUT "$fileCoversionCounter: now transforming $passArray to the ISO Entity and Attribute format\n ";
my @csaxEA = "java com.saxonica.Transform  -outval:recover $indir/$passArray /mt/apps/gpms/mrf2ea/mrf2eamaster.xsl ";
my $eaInd = system(@csaxEA);

if ($eaInd == 0){
system "mv $ISOEAName $ISOdir/$newdir/$EADir";
$EAFilesArray[$numOfEAFiles]=$ISOEAName;
$numOfEAFiles++;
}else{
$badEAFile[$badEAFileCounter]=$ISOEAName;
$badEAFileCounter++;
system "mv $ISOEAName $newerrdir";
system "mv $ISOEANameError $newerrdir";
};

$transformCounter++;
$transformIndicatorCounter++;

#if ($transformIndicatorCounter==10){
#sprintf ("%.2f",$percentTransformed = ($transformCounter/$totalNumberOfValidFiles)*100);
#print SAVEOUT "\n-----$percentTransformed% of the files have been transformed into FGDC and ISO files----\n";
#$transformIndicatorCounter=0;
#};

};


#making the Series Information files

#print SAVEOUT "\$passArray = $passArray\n";
$type=substr( $passArray,0,2);


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

if ($type eq "cb"){
#print SAVEOUT "dealing with cartographic files!!!!!!!!!!!!!!!!!!\n";

$locationFirstUnderscore=index($passArray,"_")+1;
$postUnder1=substr($passArray,$locationFirstUnderscore);

$locationOfSweondUnderscore=index($postUnder1,"_");
$year=substr($postUnder1,0,$locationOfSweondUnderscore);
$postUnder2=substr($postUnder1,$locationOfSweondUnderscore+1);

$locationOfThirdUnderscore=index($postUnder2,"_")+1;
$postUnder3=substr($postUnder2,$locationOfThirdUnderscore);


$locationOfFirstDot=index($postUnder3,".");
$themeScale=substr($postUnder3,0,$locationOfFirstDot);

$SeriesInfoFile="SeriesCollection_" . $type . "_" . $year . "_" . $themeScale . ".shp.iso.xml";

#print SAVEOUT "\$postUnder1 = $postUnder1\n";
#print SAVEOUT"\$year = $year\n";
#print SAVEOUT"\$postUnder2 = $postUnder2\n";
#print SAVEOUT"\$postUnder3=$postUnder3\n";
#print SAVEOUT"\$themeScale= $themeScale\n";
#print SAVEOUT"\$SeriesInfoFile = $SeriesInfoFile\n";


$SeriesInfoFile="SeriesCollection_" . $type . "_" . $year . "_" . $themeScale . ".shp.iso.xml";
$seriesInfoError="SeriesCollection_" . $type . "_" . $year . "_" . $themeScale . ".err";
#$SeriesInfo
$SeriesInfoFull= "$SeriesInfo/$SeriesInfoFile";
#print SAVEOUT "\$SeriesInfoFull = $SeriesInfoFull\n";

if (-e $SeriesInfoFull){
#print SAVEOUT "$SeriesInfoFull already exists!!!!!!!!!!!!!!\n";
}else{
open STDOUT,">$SeriesInfoFile";
open STDERR,">$seriesInfoError";

print SAVEOUT "$fileCoversionCounter: now transforming $passArray to the ISO Series Information format\n ";
my @csaxSeries = "java com.saxonica.Transform  -outval:recover $indir/$passArray /mt/apps/gpms/mrf2ISOParent/MRF2ISOParent.xsl ";
my $SeriesInd = system(@csaxSeries);

if ($SeriesInd == 0){
system "mv $SeriesInfoFile $SeriesInfo";
system "rm $seriesInfoError";
$SeriesInfoArray[$SeriesInfoArrayCounter]=$SeriesInfoFile;
$SeriesInfoArrayCounter++;
}else{
print SAVEOUT "We have an error\n";
system "mv $SeriesInfoFile $newerrdir";
system "mv $seriesInfoError $newerrdir";
};


};





}else{

};



#};






if ($transformIndicatorCounter==10){
sprintf ("%.2f",$percentTransformed = ($transformCounter/$totalNumberOfValidFiles)*100);
print SAVEOUT "\n-----$percentTransformed% of the files have been transformed into FGDC and ISO files----\n";
$transformIndicatorCounter=0;
};


};


#-----------------------------------getting the total number of files------------------------------------------------------------------------------------------
print SAVEOUT "Now calculating the total number of files-------------------------------------------------------------------------------\n\n";
$total=$passArrayCounter+$failArrayCounter;

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

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

#-------------------------------------grammar-----------------------------------------------------------------------------------------------------------------
if ($numOfEAFiles == 1){
$EAPhrase =" Entity and Attribute (ISO 19110) file was created";
}else{
$EAPhrase =" Entity and Attribute (ISO 19110) files were created";
};

#-Setting up the Failed ------------------------------------------------------------------------------------------------------


if ($failArrayCounter>0){
print SAVEOUT "Making the Failed Files\n";
$ReRunFile ="$errlogdir/$newdir/FailedFiles.txt";
open RERUN, ">$ReRunFile";

foreach $reRunArray (@reRunArray){
print RERUN "$reRunArray\n";
};

};


#--------------------------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, $passArrayCounter ,or $perTrans%, were successfully transformed into the ISO format and $failArrayCounter, or $perBad%, were not \n";
print LOG "transformed because of errors in the  MRF file. Also, $numOfEAFiles $EAPhrase";

print SAVEOUT "Of the $total files that were validated,  $passArrayCounter,or $perTrans%, were successfully transformed into the ISO format and $failArrayCounter, or $perBad%, were not \n";
print SAVEOUT"transformed because of errors in the  MRF file. Also, $numOfEAFiles $EAPhrase";

if ($UnIdThemes ==1){
print LOG " and there was $UnIdThemes file whose theme could not be identified.\n";
print SAVEOUT " and there was $UnIdThemes file whose theme could not be identified.\n";
}elsif($UnIdThemes > 1){
print LOG " and there were $UnIdThemes files whose theme could not be identified.\n";
print SAVEOUT" and there were $UnIdThemes files whose theme could not be identified.\n";
}else{
print LOG ".";
print SAVEOUT ".";
};

if ($doesNotExistCounter >0){
print LOG "Also, $doesNotExistCounter files could not be found.\n";
print SAVEOUT "Also, $doesNotExistCounter files could not be found.\n";

};

print LOG " Please see the list of files below:\n\n";
print SAVEOUT " Please see the list of files below:\n\n";

#--------------------------indicator section ----------------------------------------------------------------------------------------------------------------------

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

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


if ($failArrayCounter>0){
if ($failArrayCounter == 1){
print SAVEOUT "\nThe following file was NOT transformed into an ISO 19115 format metadata file. Please see this file and the error file at $newerrdir\n";
print LOG "\nThe following file was NOT transformed into an ISO 19115 format metadata file. Please see this file and the error file at $newerrdir:\n";
}else{
print SAVEOUT "\nThe following files were NOT transformed into an ISO 19115 format metadata file. Please see this file and the error file at $newerrdir\n";
print LOG "\nThe following files were NOT transformed into an ISO 19115 format metadata file. Please see this file and the error file at $newerrdir:\n"
};
$loopcounter=1;
foreach $failArray (@failArray){
print SAVEOUT "$loopcounter: $failArray\n";
print LOG "$loopcounter: $failArray\n";
$loopcounter++;
};
};




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++;
};

print SAVEOUT "\n";


$badcounter=1;
if ($badEAFileCounter >0){
print SAVEOUT "\n The following EA files could not be transformed!!!!\n";
print LOG  "\n The following EA files could not be transformed!!!!\n";
foreach $badEAFile (@badEAFile){
print SAVEOUT "$badcounter:$badFile\n";
print LOG "$badcounter:$badFile\n";
$badcounter++;
};
};
};



if ($SeriesInfoArrayCounter>0){
print SAVEOUT "The following $SeriesInfoArrayCounter Series Information Files were created for publication on data.gov:\n";
print LOG "The following $SeriesInfoArrayCounter Series Information Files were created for publication on data.gov:\n";

foreach $SeriesInfoArray (@SeriesInfoArray){
$SeriesCounter++;
print SAVEOUT"$SeriesCounter: $SeriesInfoArray\n";
print LOG  "$SeriesCounter: $SeriesInfoArray\n";


};


};


if ($UnIdThemes >0){
if ($UnIdThemes >1){
print SAVEOUT "\nThe theme for the following files could not be identified. Please see the file of unknown themes(UnknownThemes.txt) at $newerrdir:\n";
print LOG "\nThe theme for the following files could not be identified. Please see the file of unknown themes(UnknownThemes.txt) at $newerrdir: \n";
}elsif($UnIdThemes ==1){
print SAVEOUT "\nThe theme for the following file could not be identified. Please see the file of unknown themes(UnknownThemes.txt) at $newerrdir:\n";
print LOG "\nThe theme for the following file could not be identified. Please see the file of unknown themes(UnknownThemes.txt) at $newerrdir:\n";
};
$MissingThemeCounter=1;
foreach $UnIdThemeArray (@UnIdThemeArray){
print SAVEOUT "$MissingThemeCounter: $UnIdThemeArray\n";
print LOG "$MissingThemeCounter: $UnIdThemeArray\n";
$MissingThemeCounter++;
};
};
print SAVEOUT "\n";


if ($doesNotExistCounter >0){
print SAVEOUT "The following files could not be found:\n";
print LOG "The following files could not be found:\n";
$NewdoesNotExistCounter=1;
foreach $doesNotExist (@doesNotExist){
print SAVEOUT "$NewdoesNotExistCounter: $doesNotExist\n";
print LOG "$NewdoesNotExistCounter: $doesNotExist\n";
$NewdoesNotExistCounter++;
};
print SAVEOUT "\n";
};



close RERUN;
close LOG;

system "rm *.err";

if ($badFile >0){
exit(2);
}else{
exit(0);
};

