#!/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 [-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";
$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 $outputdir/$newdir";
system "mkdir $ISOdir/$newdir";
system "mkdir $ISOdir/$newdir/$EADir";
system "mkdir $fgdcdir/$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;
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
};
};

#------------------------------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";
#if ($aml =~ /_P_/){
#printnt OUTFILE "$aml e p\n";
#return "e";
#}elsif($aml =~ /dbf/){
#print OUTFILE "$aml t p\n";
#return "t";
#}else{
#print OUTFILE "$aml v p\n";
#return "v";
#};

};

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'} = "/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}";



$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 $finalarray  (@finalarray){
print SAVEOUT "now validating $finalarray-----------------------------\n";
$ISOind="no";
chomp($finalarray);
#setting up the variables necessary to make the the finalfilename variable, which is needed to make the MRF file
$slashpos= rindex($finalarray, "/") +1;
$newfilename = substr($finalarray,$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);
$errfile = $errmain .".err";
#print SAVEOUT "\$errfile = $errfile\n";
open STDERR,">$errfile";



#starts schema aware transformation. This will cause the transformation to fail if the document is invalid
# the -outval:recover option indicates that validation errors found when validating the result tree should be treated as warnings only.
#This option is available only with the Saxon-SA version of the command, com.saxonica.Transform.

$filecounter++;
my @csax = "java com.saxonica.Transform -outval:recover $indir/$finalarray $root/gpms/xsl/MIF2MRF.xsl";#works sortof
#my @csax = "java com.saxonica.Transform -vw -outval:recover $indir/$finalarray $root/gpms/xsl/MIF2MRF.xsl";
#my @csax = "net.sf.saxon.com.Validation.STRICT $indir/$finalarray $root/gpms/xsl/MIF2MRF.xsl";
my $mif = system(@csax);
print SAVEOUT "\$mif = $mif\n";

#opening fielhandles so that *.xsd will be replaced by Repository.xsd
#basically replicating the sed command 
open MRFOUTPUT, ">$MRF";
open TMPFILE, "<$TMP";

if ($mif > 0 or $mif <0) {
print SAVEOUT "MIF Not Zero\n";
open ERRHANDLE, "$errfile ";

while (<ERRHANDLE>){

if($_=~/does not exist/){
$doesNotExist[$doesNotExistCounter]=$errfile;
$doesNotExistCounter++;
system "rm $errfile";
system "rm $TMP";
}else{

if ($badMIFfile_counter==0){
$badMIFfile[$badMIFfile_counter] = $errfile;
print SAVEOUT "\$badMIFfile[$badMIFfile_counter] = $badMIFfile[$badMIFfile_counter]\n";
$badMIFfile_counter++;
$badMIFfilePrev=$errfile;
}elsif($errfile ne $badMIFfilePrev){
$badMIFfile[$badMIFfile_counter] = $errfile;
print SAVEOUT "\$badMIFfile[$badMIFfile_counter] = $badMIFfile[$badMIFfile_counter]\n";
$badMIFfile_counter++;
$badMIFfilePrev=$errfile;
};

$DashLocation=index($TMP, "-")+1;
$mainpartTMP=substr($TMP,$DashLocation);

$sizeTMP= -s $TMP;
$sizeErr= -s $errfile;

};#if($_=~/does not exist/){
};#While

print SAVOUT "\$sizeTMP = $sizeTMP\n";
if ($sizeTMP >0 ){
system "mv $TMP $newerrdir";
system "mv $errfile $newerrdir";

}else{
system "rm $TMP";
};#if ($sizeTMP >0 ){

$sizeMRF = -s $MRF;

if ($sizeMRF >0){
system "mv $MRF $newerrdir";
}else{
system "rm $MRF";
};#if ($sizeMRF >0){


}else{
unlink $errfile;

$goodmif[$goodMif_counter]=$finalarray;
$goodMif_counter++;

system "rm $errfile";
$goodfile[$goodfile_counter]= $MRF;


$goodflaghash{$MRF}=$flaghash{$finalarray};
$npos=index($flaghash{$finalarray},"n");
if ($npos==0){
$nopassarray[$no_pass_counter]=$goodfile[$goodfile_counter];
$no_pass_counter++;

}elsif ($npos<0){
$passarray[$pass_counter]=$goodfile[$goodfile_counter];
$passmif[$pass_counter]=$finalarray;

$passMRF[$pass_counter]=$MRF;

$pass_counter++;

};
$goodfile_counter++;
unlink $errfile;

 while(<TMPFILE>){
  if($_ =~ /xsd/){
   #this happens if the the line has xsd in it
   $xsdlocation =index($_, "/xsd");
   $originalstring= substr($_,0,$xsdlocation);
   $Repository = "xsd/Repository.xsd\">\n";
   printf MRFOUTPUT ("$originalstring/$Repository");
  }elsif ($_ =~ /<\/MRF>/){
   printf  MRFOUTPUT ("$_");
   printf  MRFOUTPUT "\n";
   }elsif($_=~ /%/){
        chomp($_);
        $perloc=index($_, "%");
        $firsthalf =substr($_,0,$perloc);
        $secondhalf =substr($_, $perloc+1);
        $perloc2=index($secondhalf, "%");
                if ($perloc2 < 0){
                        printf  MRFOUTPUT"$firsthalf" . "%%" . "$secondhalf";
                }elsif($perloc2 >0){
                        $thirdhalf =substr($secondhalf,0,$perloc2);
                        $fourthhalf =substr($secondhalf, $perloc2+1);
                        $newstring = "$firsthalf" . "%%" . "$thirdhalf" . "%%" . "$fourthhalf\n";
if ($newstring=~/&#147;/){
$amploc2 =index($newstring, "&#148;");
                           $amp1string = substr($newstring,0,$amploc1);
                           $midstringlength= $amploc2 - $amploc1-6;
                           $amp2string = substr($newstring,$amploc1+6,$midstringlength);
                           $amp3string = substr($newstring,$amploc2+6);
                           printf  MRFOUTPUT $amp1string . "\"" . $amp2string . "\"" . $amp3string;
                         }else{
                        printf  MRFOUTPUT"$firsthalf" . "%%" . "$thirdhalf" . "%%" . "$fourthhalf\n";
                         };;#if ($newstring=~/&#147;/){
                         };#if ($perloc2 <  0){
 }elsif($_=~/&#147;/){
$amploc1=index($_, "&#147;");
 $amploc2 =index($_, "&#148;");
 $amp1string = substr($_,0,$amploc1);
 $midstringlength= $amploc2 - $amploc1-6;
 $amp2string = substr($_,$amploc1+6,$midstringlength);
 $amp3string = substr($_,$amploc2+6);
 printf  MRFOUTPUT $amp1string . "\"" . $amp2string . "\"" . $amp3string;

}elsif($_=~/&#146;/){
$aposloc1 = index($_,"&#146;");
$aposstring1 = substr($_,0,$aposloc1);
$aposstring2 = substr($_,$aposloc1+6);
printf  MRFOUTPUT $aposstring1 . "'" . $aposstring2;


  }else{
   printf  MRFOUTPUT "$_";
  };#if xsd

 };#while<TMPFILE>
close MRFOUTPUT;
close TMPFILE;
open TMPFILE, "<$TMP";


};#if mif>0
unlink $TMP
};#foreach

printf SAVEOUT "\n";
close STDOUT;
$MRFPASSLoopNumber=1;
foreach $passarray (@passarray){


$fgdcdotloc = index($passarray, ".");
$mainpart=substr($passarray,0,$fgdcdotloc);

$FGDC = $mainpart . ".xml";
open STDOUT,">$FGDC";
open STDERR,">$FGDC.err";

my @csax = "java com.saxonica.Transform -outval:recover $MRF $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);

if ($fgdcout == 0){
$goodFGDC[$goodFGDC_counter]=$FGDC;
$goodFGDC_counter++;
}else{
$badFGDC[$badFGDC_counter]=$FGDC;
$badFGDC_counter++;
};





#$completeISODir
$ISOname ="$mainpart.iso.xml";
$EAmainpart=substr($mainpart,11);
$ISOEAName ="$EAmainpart.ea.iso.xml";
#print SAVEOUT "\$MRFdir = $MRFdir\n";
#print SAVEOUT "\$FGDCname = $FGDCname\n";
open STDOUT,">$ISOname";


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

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

if ($mif ==0){
$goodISO[$goodISO_counter]=$ISOname;
$goodISO_counter++;
}else{
$badISO[$badISO_counter]=$ISOname;
$badISO_counter++;
};

$ISOEANameDir = $ISOdir . "/" . $newdir . "/" . "EAFiles" . "/". $ISOEAName;
if (-e $ISOEANameDir){
}else{
open STDOUT,">$ISOEAName";
$numOfEAFilesCounter++;
print SAVEOUT "\n$numOfEAFilesCounter: Now making the Entity and Attribute Files ------------------------------------------------------------------------------------\n";
my @csaxea = "java com.saxonica.Transform -outval:recover  $passarray  $root/gpms/mrf2ea/mrf2eamaster.xsl";#Sec52iso.xslt
my $mifea = system(@csaxea);
system "mv $ISOEAName $ISOdir/$newdir/EAFiles";
$EAFilesArray[$numOfEAFiles]=$ISOEAName;
$numOfEAFiles++;
};


if (-e $passarray){
system "mv $passarray  $MRFdir/$newdir";
if (-e $goodfile){
system "chmod 777 $goodfile";
unlink $passarray;
};

};

system "mv $ISOname $ISOdir/$newdir";
#system "mv $ISOEAName $ISOdir/$newdir/EAFiles";
system "mv $FGDC  $fgdcdir/$newdir";
system "mv  $passarray $outputdir/$newdir";
};
#};
print SAVEOUT "\n";
#---------------------------------------------------transforming to ISO Standard------------------------------------------------------------------------

#####-----------------------------------for the entity and attributes --------------------------------------------------------------------------------------


#-----------------------------------getting the total number of files------------------------------------------------------------------------------------------
print SAVEOUT "Now calculating the total number of files-------------------------------------------------------------------------------\n\n";
$total=$goodMif_counter+$badMIFfile_counter;
#$goodMif_counter
if ($goodMif_counter>0){
$perTrans=($goodMif_counter/$total)*100;
}else{
$perTrans=0;
};

if ($badMIFfile_counter>0){
$perBad=($badMIFfile_counter/$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 ------------------------------------------------------------------------------------------------------

$ReRunFile ="$errlogdir/$newdir/FailedFiles.txt";

if ($newflaglength > 5 or $mal == 0 or $invalidcounter >0 or $badMIFfile_counter >0 or $badFileCounter > 0){
open RERUN, ">$ReRunFile";
};


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


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

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

#print SAVEOUT "pre script!!!!!!!!!!!!!!!!!! \$goodISO = $goodISO\n";
if ($goodISO_counter>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 ($goodISO_counter == 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 $goodISO  (@goodISO){
print SAVEOUT "$goodcounter:  $goodISO\n";
print LOG "$goodcounter:  $goodISO\n";
$goodcounter++;
};


if ($badISO_counter>0){
if ($badISO_counter == 1){
print SAVEOUT "The following file was NOT transformed into an ISO 19115 format metadata file. Please see this file at $newerrdir\n";
print LOG "The following file was NOT transformed into an ISO 19115 format metadata file. Please see this file at $newerrdir:\n";
}else{
print SAVEOUT "The following files were NOT transformed into an ISO 19115 format metadata file. Please see this file at $newerrdir\n";
print LOG "The following files were NOT transformed into an ISO 19115 format metadata file. Please see this file at $newerrdir:\n"
};
$loopcounter=1;
foreach $badISO (@badISO){
print SAVEOUT "$loopcounter: $badISO\n";
print LOG "$loopcounter: $badISO\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++;
};

};

if ($badFileCounter > 0){

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


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




if ($badMIFfile_counter > 0){
print SAVEOUT "\$badMIFfile_counter = $badMIFfile_counter\n";
print SAVEOUT "The following files FAILED validation and did not have FGDC or ISO files created for them. These files have invalid XML. Please see the error files at $newerrdir \n";
print LOG "The following files FAILED validation and did not have FGDC or ISO files created for them. These files have invalid XML. Please see the error files at $newerrdir \n";
$badcounter=1;

foreach $badMIFfile (@badMIFfile){
print SAVEOUT "$badcounter: $badMIFfile\n";
print LOG "$badcounter: $badMIFfile\n";
system "mv $badMIFfile $newerrdir";
$dotLocation = index($badMIFfile,".");
$mainPartXML=substr($badMIFfile,0,$dotLocation);
print SAVEOUT "\$mainPartXML = $mainPartXML\n";
$newFile=$indir . "/" . $mainPartXML . ".mif";
print SAVEOUT "\$newFile = $newFile\n";
#$indir

system "cp $newFile $newerrdir";
$badcounter++;
};
#print SAVEOUT "\n";
};

close RERUN;
close LOG;

system "rm *.err";

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

