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

$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-------------------------------------------------------------------------------------------
#Written by Matthew McCready
#This script validates a  MIF file and, if valid, transforms it into an FGDC, ISO 19115, 19139 and 19110 file. It does this in may parts

#First, it validates the mif file against the GPMS. If the file is valied, it is then transformed into a FGDC and ISO 19115-2 file. The existance of Entity 
#and attribute information is them checked for. If present and a theme can be deduced from the file's tile, an ISO 19110 file for each theme is created.

#---------------------------------------------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;
$numOfEAFiles=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{
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=@_;
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";
};


sub CompleteInputFile{
$InputFile= @_[0];
$inputDirectory = @_[1];
$CompleteInputFile= $inputDirectory . "/" . $InputFile;
return $CompleteInputFile;
};


#----------------------------------------------------------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";
#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------Validating the mif file---------------------------------------------------------------------------------------------------------

$valcounter=0;
$filecounter =1;


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";
my $mif = system(@csax);

#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) {
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;
$badMIFfile_counter++;
$badMIFfilePrev=$errfile;
}elsif($errfile ne $badMIFfilePrev){
$badMIFfile[$badMIFfile_counter] = $errfile;
$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);
$FGDCname = "$mainpart.xml";
open STDOUT,">$FGDCname";
my @csax = "java com.saxonica.Transform -outval:recover $passarray $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $mif = system(@csax);

if (-e "$FGDCdir/$newdir/$FGDCname"){
system "chmod 777 $FGDCdir/$newdir/$FGDCname";
};

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

system "mv $FGDCname  $fgdcdir/$newdir"; 
system "mv  $passarray $outputdir/$newdir";
};
print SAVEOUT "\n";
#---------------------------------------------------transforming to ISO Standard------------------------------------------------------------------------
print SAVEOUT "Now Transforming the files to ISO 19115 format-----------------------------------------------------------------------------\n";

foreach $passmif (@passmif){

$passmifCounter++;
print SAVEOUT "now transforming $passmif to the ISO 19115-2 format\n";


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


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

my @csax = "java com.saxonica.Transform -outval:recover  $indir/$passmif  $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";
$goodISO[$goodISOcounter]=$passmif;
$goodISOcounter++;
}else{
system "mv $iso_var $newerrdir";
system "mv $iso_var.err $newerrdir";
$badFile[$badFileCounter] =$passmif;
$badFileCounter++; 
};

} ;


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

print SAVEOUT "\nNow making the Entity and Attribute Files ------------------------------------------------------------------------------------\n";

foreach $passmif (@passmif){
#print SAVEOUT "Now making the Entity and Attribute Files $passmif\n";
$TigerLocation =index($passmif,"tl");
$CensusLocation=index($passmif,"DC");
$gzLocation=index($passmif,"gz");

if ($TigerLocation > 0){
$lastDash=rindex($passmif,"_")+1;
$firstDot=index($passmif,".");
$length=$firstDot-$lastDash;
$FileTheme=substr($passmif,$lastDash,$length);
$ThemeArray[$ThemeArrayCounter]=$FileTheme;
$ThemeArrayCounter++;
}elsif($CensusLocation >=0){
$FirstDashLocation=index($passmif,"_")+1;
$LastDashLocation=rindex($passmif,"_");
$length=$LastDashLocation-$FirstDashLocation;
$FileTheme=substr($passmif,$FirstDashLocation,$length);
$ThemeArray[$ThemeArrayCounter]=$FileTheme;
$ThemeArrayCounter++;
}elsif($gzLocation >= 0){
$FirstDashLocation=index($passmif,"_")+1;
$FirstPart=substr($passmif,11,3);
$FileTheme= substr($passmif,11,3);
$ThemeArray[$ThemeArrayCounter]=$FileTheme;
$ThemeArrayCounter++;

}else{
open UNIDTHEME, ">>$newerrdir/$logname/UnknownThemes.txt";
$UnIdThemeArray[$UnIdThemes]=$passmif;
$UnIdThemes++;
print UNIDTHEME "$passmif\n";
close UNIDTHEME;
};

$CurrentTheme=$FileTheme;
@ThemeArray =sort @ThemeArray;
foreach $ThemeArray (@ThemeArray){

if ($ThemeArray eq $CurrentTheme){
if ($ThemeArrayCounter <=1 or $ThemePrev ne $FileTheme){
$FinalArrayPeriodLocation=rindex($passmif,".")+1;
$FinalArrayExtention=substr($passmif,$FinalArrayPeriodLocation);

if($FinalArrayExtention eq "mif"){
print SAVEOUT "in the mif section!!!!!!!!!!!!!!!!!!!\n";
$mainPart=substr($passmif,0,$FinalArrayPeriodLocation);
$iso_var_ea= "ISO_EA1_" . $mainPart . "xml";
$iso_var_ea_error = "ISO_EA1_" .  $mainPart .  "err";
$iso_var_ea2= "ISO_EA_" . $mainPart . "xml";
print SAVEOUT "Now making the Entity and Attribute Files $iso_var_ea\n";
}else{
print SAVEOUT "in the else section!!!!!!!!!!!!!!!!!!\n";
$iso_var_ea= "ISO_EA1_" . $passmif;
$iso_var_ea_error = "ISO_EA1_" . $passmif . ".err";
$iso_var_ea2="ISO_EA_" . $passmif;
};



open STDOUT,">$iso_var_ea";
open STDERR, ">$iso_var_ea_error";
my @csaxea = "java com.saxonica.Transform -outval:recover  $indir/$passmif  $root/gpms/MIF2ISO/EAFile.xsl";#Sec52iso.xslt
my $mifea = system(@csaxea);
$sizeOfEAFile = -s $iso_var_ea;
if ($mifea == 0 and $sizeOfEAFile>43){
$FinalEA[$numOfEAFiles]=$passmif;
$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{ 
print SAVEOUT "Bad Transformation\n";
system "mv $iso_var_er $newerrdir";
system "mv $iso_var_ea_error $newerrdir";
$badFile[$badFileCounter] =$passmif;
$badFileCounter++;
};
$ThemePrev=$FileTheme;
}else{
};#if ($ThemeArrayCounter <=1 or $ThemePrev ne $FileThemee){
}; #if ($ThemeArray eq $CurrentTheme){
};#foreach
};#foreach

close STDOUT;
close STDERR;
close UNIDTHEME;
#--------------------------------------------------------------------------------------------------------------------------------------------------------------
# This second EA loop reads in every MIF File with an Entity and Attribute section and stores the Codeset_Name and Enumerated domain values in the CodesetLine and EnumeratedDomainValue arrays respectively. 
# This loop then reads in every file in the EAFilesArray and when it gets to the Put Codeset_Name here or Put Enumerated_Domain_Value here strings, it inserts then appropriate value from the 
# CodesetLine or EnumeratedDomainValue array. It then moves the corrected EA file to the EA directory, which is under the output/ISO/"productname" directory.

foreach $FinalEA(@FinalEA){
$CodesetNumber=1;
$EnumeratedDomainValueNumber=1;
$MIFLocationA=index($FinalEA,"MIF_");
$MIFLocation=index($FinalEA,"MIF_")+4;

if($MIFLocationA >0){
$FirstFile=substr($FinalEA,$MIFLocation);
$MIFYesNo="Yes";
}else{
$FirstFile = $FinalEA;
$MIFYesNo="No";
};



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;

foreach $EAFilesArray (@EAFilesArray){

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

if($MIFYesNo eq "Yes"){
$secondFile=substr($EAFilesArray,$MIFLocationEA);
}else{
$MIFLocationD=index($EAFilesArray,"MIF");

if ($MIFLocationD <0){
$secondFile =substr($EAFilesArray,8);

};

};

$NewISOFile= "ISO_EA_" . $secondFile;

$FirstFilePeriod=index($FirstFile,".");
$FirstFileMain=substr($FirstFile,0,$FirstFilePeriod);
$secondFilePeriod=index($secondFile,".");
$secondFileMain=substr($secondFile,0,$secondFilePeriod);


if ($FirstFileMain eq $secondFileMain){
open OLDISO, "<$EAFilesArray";
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
};

};

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


};

};

close OLDEA;
close OLDISO;
close NEWISO;
#----------------------------------removing the first EA File ----------------------------------------------

#foreach $EAFilesArray (@EAFilesArray){
#system "rm $EAFilesArray";
#};
#----------------------------------------------------------------------------------------------
#-----------------------------------getting the total number of files------------------------------------------------------------------------------------------
print SAVEOUT "Now calculating the total number of files-------------------------------------------------------------------------------\n\n";
$total=$goodISOcounter+$badMIFfile_counter;

if ($goodISOcounter>0){
$perTrans=($goodISOcounter/$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 >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, $goodISOcounter ,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, $goodISOcounter ,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 ----------------------------------------------------------------------------------------------------------------------

if ( $goodISOcounter >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 $goodISO  (@goodISO){
print SAVEOUT "$goodcounter:  $goodISO\n";
print LOG "$goodcounter:  $goodISO\n";
$goodcounter++;
};

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 "The following files FAILED validation and did not have FGDC or ISO files created for them. 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. 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,".");
$MainPart=substr($badMIFfile,0,$dotLocation);


$OriginalMif= $MainPart . ".mif";
#$postingFlag = &PostingFlagD($OriginalMif, $indir);
#print SAVEOUT "\$postingFlag = $postingFlag";


$CompleteFile=&CompleteInputFile ($OriginalMif, $indir);
system "cp $CompleteFile $newerrdir";
#print SAVEOUT "\$CompleteFile = $CompleteFile---------------\n";

open FH, "$CompleteFile";
#print SAVEOUT "\$! = $!"; 
while (<FH>){
#print SAVEOUT "Now reading file\n";
if ($_ =~ /Title_13_Restrictions/){
if ($_ =~ /Yes/){
$postingVar="p";
}else{
$postingVar= "np";
};
}elsif ($_ =~ /MIF/){
$postingVar= "np";
};
};

print RERUN " $OriginalMif $postingVar\n";
$badcounter++;
};
print SAVEOUT "\n";
};

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

