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


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


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

print "\$flagless = $flagless\n";
print "\$ProfileFlag = \"$ProfileFlag\n";

if ($ProfileFlag eq 'v'){
$vectorArray[$vectorArrayCounter]=$flagless;
print "\$vectorArray[$vectorArrayCounter] = $vectorArray[$vectorArrayCounter]\n";
$vectorArrayCounter++;
};

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

};

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


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


#my @csax = "java com.saxonica.Validate $indir/$finalarray /mt/apps/gpms/xsd/Vector.xsd";

foreach $vectorArray  (@vectorArray){
print SAVEOUT "now validating $vectorArray-----------------------------\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($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++;


#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.Validate $indir/$vectorArray /mt/apps/gpms/xsd/Vector.xsd";
my $mif = system(@csax);
print SAVEOUT "\$mif = $mif\n";

if ($mif == 0){
$passArray[$passArrayCounter]= $vectorArray;
$passArrayCounter++;
system "rm $errfile";
}elsif ($mif != 0){
$failArray[$failArrayCounter]=$vectorArray;
$failArrayCounter++;
system "cp $vectorArray $newerrdir";
system "mv $errfile $newerrdir";


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

};
}; 

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



#end Here
#start here
#foreach $failArray (@failArray){
# };#if ($sizeMRF >0){

foreach $passArray (@passArray){


print SAVEOUT "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
$dotLocation=index($passArray,".");
$mainPart=substr($passArray,0,$dotLocation);
$FgdcFile=$mainPart . ".xml";
$FgdcError =$mainPart . "_fgdc.err";
print "\$FgdcFile = $FgdcFile\n";

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

print SAVEOUT "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);
print SAVEOUT "\$fgdcout = $fgdcout\n";

if ($fgdcout == 0){
print SAVEOUT "now moving $FgdcFile to $fgdcdir/$newdir\n";
system "mv $FgdcFile $fgdcdir/$newdir";
system "rm $passArray";
}else{
system "mv $FgdcFile $newerrdir";
system "mv $FgdcError  $newerrdir"; 
};





#$completeISODir
$ISOname ="$mainPart.shp.iso.xml";
$ISONAMEerror="$mainPart.shp.iso.err";
#$EAmainpart=substr($mainpart,11);
#$ISOEAName = $year . "_" . $mainPart . ".shp.ea.iso.xml";
#$ISOEANameError = $ISOEAName . ".err";
open STDOUT,">$ISOname";
open STDERR,">$ISONAMEerror";


print SAVEOUT "\nnow 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
#$ISOEAName = $year . "_" . $mainPart . ".shp.ea.iso.xml";
#$ISOEANameError = $ISOEAName . ".err";

#one EA File for every theme
$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);

$ISOEAName = $preFirst . $preSecond . $postThird . ".shp.ea.iso.xml";


print SAVEOUT "\$preFirst = $preFirst\n";
print SAVEOUT "\$postFirst = $postFirst\n";
print SAVEOUT "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n";
print SAVEOUT "\$preSecond = $preSecond\n";
print SAVEOUT "\$postSecond = $postSecond\n";
print SAVEOUT "ccccccccccccccccccccccccccccccccccc\n";
print SAVEOUT "\$postThird = $postThird\n";
print SAVEOUT "ddddddddddddddddddddddddddddddddddddddddd\n";
print SAVEOUT "\$ISOEAName = $ISOEAName\n";

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

print SAVEOUT "\nnow 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);
print SAVEOUT "\$eaInd = $eaInd \n";

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



print SAVEOUT "----------------------------------------------------------------------------------------------------------------------\n";
};


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

$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, $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\n";


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\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 ($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 "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 $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 ($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);
};

