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;
#------------------------------------------------------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){
#print "output directory check";
}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=@_;
#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 FGDCPostingFlag{
$InputFile=@_;
#open INPUTFileHandle, "<$InputFile";
#print SAVEOUT "end of sub-----------------------------xxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
return "p";


#while (<INPUTFileHandle>){
#if($_ =~ /Title_13_Restrictions/){

#if ($_=~/Yes/){
#return "p";
#}else{
#return "np";
#};

#};
#};

};

sub FGDCPostingFlagB{
$aml=@_;
if ($aml =~ /edges/){
#printnt OUTFILE "$aml e p\n";
return "pa";
}elsif($aml =~ /dbf/){
#print OUTFILE "$aml t p\n";
return "pb";
}else{
#print OUTFILE "$aml v p\n";
return "pc";
};

};

sub FGDCPostingFlagC{
$aml=@_;
open INPUT, "$aml";
if ($aml =~ /edges/){
#printnt OUTFILE "$aml e p\n";
return "$aml";
}elsif($aml =~ /dbf/){
#print OUTFILE "$aml t p\n";
return "$aml";
}else{
#print OUTFILE "$aml v p\n";
return "$aml";
};

};


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

$CompleteInputFile= $inputDirectory . "/" . $InputFile;
return $CompleteInputFile;

};

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


#-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
#---------------------------------------------------------Validating the mif file---------------------------------------------------------------------------------------------------------

$valcounter=0;
$filecounter =1;

#foreach $filename (@filename){

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.

#printf SAVEOUT "$filecounter: Now validating $finalarray\n";
$filecounter++;
my @csax = "java com.saxonica.Transform -outval:recover $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";

#close STDERR; #new one


if ($mif > 0) {
#print SAVEOUT "In the MIF > 0 section ( $finalarray)\n";
open ERRHANDLE, "$errfile ";

while (<ERRHANDLE>){

if($_=~/does not exist/){
#print SAVEOUT "does not exist\n";
$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;
};

#print SAVEOUT "----------------------------------------------------------------\n";
#print SAVEOUT "In the existing section \n";
$DashLocation=index($TMP, "-")+1;
$mainpartTMP=substr($TMP,$DashLocation);
#print SAVEOUT "\$mainpartTMP = $mainpartTMP\n";

$sizeTMP= -s $TMP;
#print SAVEOUT "\$TMP = $TMP\n";
#print SAVEOUT  "\$sizeTMP = $sizeTMP\n";
$sizeErr= -s $errfile;
#print SAVEOUT "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n"

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

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


}else{
#print SAVEOUT "In the good mif section!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
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){
#print SAVEOUT " npos = 0\n";
$nopassarray[$no_pass_counter]=$goodfile[$goodfile_counter];
$no_pass_counter++;
#print SAVEOUT "in the posting array\n";
#$MRFPASS[$MRFPASSCounter]=$MRF;

#print SAVEOUT "\$MRFP:ASS[$MRFPASSCounter] = $MRFPASS[$MRFPASSCounter]\n";
#$MRFPASSCounter++;
}elsif ($npos<0){
#print SAVEOUT " npos < 0\n";
$passarray[$pass_counter]=$goodfile[$goodfile_counter];
$passmif[$pass_counter]=$finalarray;
#print SAVEOUT "\$passarray[$pass_counter] = $passarray[$pass_counter]\n";

$passMRF[$pass_counter]=$MRF;

$pass_counter++;

};



$goodfile_counter++;
unlink $errfile;

 while(<TMPFILE>){
#print SAVEOUT " reading the TMP file\n";
  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
#$MRFPASS[$MRFPASSCounter]=$MRF;
#print SAVEOUT "\$MRFPASS[$MRFPASSCounter] = $MRFPASS[$MRFPASSCounter]\n";
#$MRFPASSCounter++;

 };#while<TMPFILE>
close MRFOUTPUT;
close TMPFILE;

#$MRFPASS[$MRFPASSCounter]=$MRF;
#print SAVEOUT "\$MRFPASS[$MRFPASSCounter] = $MRFPASS[$MRFPASSCounter]\n";
#$MRFPASSCounter++; 

open TMPFILE, "<$TMP";


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

printf SAVEOUT "\n";
close STDOUT;

#---------------------
#print SAVEOUT "In the checking loop\n";
#$MRFPASSLoopNumber=1;
#foreach $MRFPASS (@MRFPASS){
#print SAVEOUT "$MRFPASSLoopNumber: $MRFPASS\n";
#$MRFPASSLoopNumber++;
#$MRFDirectory="$outputdir/$newdir\n";
#print SAVEOUT "\$MRFDirectory = $MRFDirectory\n";
#system "cp $MRFPASS $outputdir/$newdir"; 
#};

#print SAVEOUT "Part2 loop\n";
$MRFPASSLoopNumber=1; 
foreach $passarray (@passarray){
#print SAVEOUT "$MRFPASSLoopNumber: $passarray\n"; 
#$MRFPASSLoopNumber++;


$fgdcdotloc = index($passarray, ".");
$mainpart=substr($passarray,0,$fgdcdotloc);
$FGDCname = "$mainpart.xml";
#print SAVEOUT "\$FGDCname = $FGDCname\n";

#open STDOUT,">$FGDCdir/$newdir/$FGDCname";
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"){
#print LOG "\n $FGDCdir/$newdir/$FGDCname   in the first chmod\n\n";
system "chmod 777 $FGDCdir/$newdir/$FGDCname";
};

if (-e $passarray){
#};
#print LOG "$FGDCdir/$newdir/$FGDCname   in the second chmod\n\n";
#system "cp $passarray  $MRFdir/$newdir";
system "mv $passarray  $MRFdir/$newdir";
#system "mv $fgdcdir/$newdir";
if (-e $goodfile){
system "chmod 777 $goodfile";
unlink $passarray;
};
};

system "mv $FGDCname  $fgdcdir/$newdir"; 
system "mv  $passarray $outputdir/$newdir";
};

#print SAVEOUT "\nPart3: The mif files\n";
#$MRFPASSLoopNumber=1;

#foreach $passmif (@passmif){
#print SAVEOUT "$MRFPASSLoopNumber: $passmif\n";
#$MRFPASSLoopNumber++;
#};

print SAVEOUT "\n";
#---------------------------------------------------transforming to ISO Standard------------------------------------------------------------------------
print SAVEOUT "Now Transforming the files to ISO 19115 format-----------------------------------------------------------------------------\n";

#foreach $finalarray  (@finalarray){
foreach $passmif (@passmif){

$passmifCounter++;
print SAVEOUT "now transforming $passmif to the ISO 19115-2 format\n";
#/mt/apps/gpms/IsoSchema


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

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"-------------------------------------------------------------------------------------------------------------------------------\n";
print SAVEOUT "\nNow making the Entity and Attribute Files ------------------------------------------------------------------------------------\n";

#foreach $finalarray  (@finalarray){
foreach $passmif (@passmif){
#print SAVEOUT "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n\n";
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);
#print SAVEOUT "\$FileTheme = $FileTheme\n";
$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;
#print SAVEOUT "\$FileTheme = $FileTheme\n";
$ThemeArrayCounter++;
}elsif($gzLocation >= 0){
$FirstDashLocation=index($passmif,"_")+1;
$FirstPart=substr($passmif,11,3);
$FileTheme= substr($passmif,11,3);
$ThemeArray[$ThemeArrayCounter]=$FileTheme;
#print SAVEOUT "\$FileTheme = $FileTheme\n";
$ThemeArrayCounter++;

#print SAVEOUT "\$FirstPart(gz) = $FirstPart\n"; 
}else{
#$newerrdir/$logname
#UnIdFileName="
open UNIDTHEME, ">>$newerrdir/$logname/UnknownThemes.txt";
#open UNIDTHEME, ">>UnknownThemes.txt";
$UnIdThemeArray[$UnIdThemes]=$passmif;
$UnIdThemes++;
print UNIDTHEME "$passmif\n";
#UnIdThemeArray[

close UNIDTHEME;
};

$CurrentTheme=$FileTheme;
@ThemeArray =sort @ThemeArray;
#push @ThemeArray, 'EOA';


#print SAVEOUT "\n------------------------------------------------------------------------------\n";
#print SAVEOUT "\$CurrentTheme = $CurrentTheme\n";

#print SAVEOUT "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
#print SAVEOUT "pre Theme Loop \n";
foreach $ThemeArray (@ThemeArray){

#print SAVEOUT "\nXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
#print SAVEOUT "\$ThemeArray = $ThemeArray\n";

if ($ThemeArray eq $CurrentTheme){
#print SAVEOUT "in the first if (\$ThemeArray = $ThemeArray) and (\$CurrentTheme = $CurrentTheme)  \n";
if ($ThemeArrayCounter <=1 or $ThemePrev ne $FileTheme){

#print SAVEOUT "in theSecond if\n";
$FinalArrayPeriodLocation=rindex($passmif,".")+1;
$FinalArrayExtention=substr($passmif,$FinalArrayPeriodLocation);

if($FinalArrayExtention eq "mif"){
#print SAVEOUT "in the third if \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 "\$iso_var_ea = $iso_var_ea\n";
#print SAVEOUT "\$iso_var_ea2 = $iso_var_ea2\n";
}else{
#print SAVEOUT "in the third else \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);
#print SAVEOUT"\$mifea = $mifea\n";
$sizeOfEAFile = -s $iso_var_ea;
#print SAVEOUT "\$sizeOfEAFile = $sizeOfEAFile\n";
 
if ($mifea == 0 and $sizeOfEAFile>43){
#print SAVEOUT "EA File Created \n";
$FinalEA[$numOfEAFiles]=$passmif;
$EAFilesArray[$numOfEAFiles]=$iso_var_ea;
$numOfEAFiles ++;
system "rm $iso_var_ea_error";
close STDOUT;
}elsif($sizeOfEAFile <= 43 and $mifea == 0){
#print SAVEOUT "No EA File (No EA infor)\n";
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++;
};
#print SAVEOUT "-----------------------------------------------------------\n";
$ThemePrev=$FileTheme;

#$ThemePrev=$ThemeArray; 
}else{
#print SAVEOUT "The themes Match!!!!!!!!!!!!!!!\n";
};#if ($ThemeArrayCounter <=1 or $ThemePrev ne $FileThemee){
}; #if ($ThemeArray eq $CurrentTheme){
#pop @ThemeArray;
};#foreach
};#foreach

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

#print SAVEOUT " in the second EA loop\n";
foreach $FinalEA(@FinalEA){
#print SAVEOUT "\$FinalEA = $FinalEA\n";
$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){
#print SAVEOUT "\$EAFilesArray = $EAFilesArray\n";

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

#print SAVEOUT "\nIn the Setting up the Failed files section\n";
if ($newflaglength > 5 or $mal == 0 or $invalidcounter >0 or $badMIFfile >0 or $badFileCounter > 0){
open RERUN, ">$ReRunFile";
#print SAVEOUT "opening the FailedFiles.txt file\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, $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";

#print SAVEOUT "This log details the transformation of $total the FGDC files in the $Infile listfile to the ISO 19115 format. \n ";
#print SAVEOUT "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";

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

#print SAVEOUT "\$OriginalMif = $OriginalMif\n";
#print SAVEOUT "\$ProfileFlag = $ProfileFlag\n";
#$ProfileFlag=&TIGERFileType($OriginalMif);
#print SAVEOUT "\$ProfileFlag = $ProfileFlag\n";

$postingFlag= &PostingFlagD($OriginalMif, $indir);
print SAVEOUT "\$postingFlag = $postingFlag";
 
print RERUN " $OriginalMif $ProfileFlag\n";
$badcounter++;
};
print SAVEOUT "\n";
};


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

