#!/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-------------------------------------------------------------------------------------------
#written by Matthew McCready on 12/7/2007 .
# This program takes an input FGDC metadata file, transfroms it to a mif file, and then transforms/validates it into
# a MRF file. It then moves the file to the appropriate directory. 

#This script uses the following xslt sheets to transform a fgdc file to a mif file: 01_idinfo_planned.xsl, 01_idinfo.xsl, 02_dataqual_map.xsl,
#02_dataqual_tab.xsl, 02_dataqual.xsl, 03_spdoinfo.xsl, 04_spref.xsl, 05_eainfo.xsl, 06_distinfo.xsl, 07_metainfo.xsl, FGDC2mif_atlas.xsl
#FGDC2mif_legacy.xsl, FGDC2mif_map.xsl, FGDC2mif_planned.xsl, FGDC2mif_tabular.xsl, FGDC2mif_vector.xsl, FGDC2mif.xsl

#The script uses the following xslt sheets to transform a mif file into a mrf file: DataQualityInformation.xsl, DistributionInformation.xsl,
#EntityAndAttributeInformation.xsl, IdentificationInformation.xsl, MetadataReferenceInformation.xsl, MIF2MRF.xsl, SpatialDataOrganizationInformation.xsl
#SpatialReferenceInformation.xsl

#The script uses the following xsd sheets to validate the MRF files: Atlas.xsd, CitationInformation.xsd, ContactInformation.xsd, DataQualityInformation.xsd,
#DistributionInformation.xsd, EntityAndAttributeInformation.xsd, IdentificationInformation.xsd, Legacy.xsd, Map.xsd, MetadataReferenceInformation.xsd,
#MiscTypesAndGroups.xsd, Planned.xsd, Repository.xsd, SpatialDataOrganizationInformation.xsd, SpatialReferenceInformation.xsd, Tabular.xsd and
#Vector.xsd

#The script uses the following xslt sheets to transform a MRF sheet into an FGDC output file: 01_Identification_Information.xsl,
# 02_Data_Quality_Information.xsl, 03_Spatial_Data_Organization_Information.xsl, 04_Spatial_Reference_Information.xsl,
#05_Entity_And_Attribute_Information.xsl, 06_Distribution_Information.xsl, 07_Metadata_Reference_Information.xsl and
# MRF2FGDC.xsl

#The script use the DateTrans.pm module to correctly format the Month Name, the Month Number, the time and the day of the month number. The nonName subroutine 
#takes the month outpuut from the Unix date command and converts to the full month name. The monNumber subroutine takes the month output from the Unix date
#command and converts it to a two digit number. The timeFormat subroutine takes the time output from the Unix date command and transforms it into the normal
#12 hour time format. The AmPmFormat module takes the time output from the Unix date command and puts an "am" or "pm" at the end. The dayNumberFormat module
#transforms the day output from the Unix date command and tranfroms it into a two digit number.   

#5/31/2012 The script has been modified to work with the new servers, new JDK and new version of Saxon. New code to generate the EA files was also added.

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

#$indir="$rootout/gpms/input/fgdc";
$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";
$newdir=$Branch . $dateString;
$ISOdir="$rootout/gpms/output/ISO";
#-----------------------------------------------------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){
#print "The file $combolist has files\n";
#}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);
#};

#$outputdir
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 $monnum = monName($mon);
my $monName=monName($mon);
my $monnum =monNumber($mon);
#my $Month_Number = monNumber($mon);

$dateString =$year . $monnum . $daynum . $hour . $min;

#rint "\$dateString = $dateString\n";
#print "\$year = $year\n"; 
#print "\$monnum = $monnum\n";
#print "\$day = $day\n";
#print "\$daynum = $daynum\n";
#print "\$hour = $hour\n";
#print "\$min = $min\n";

$time = $hour . ":" . $min;
$oldhour = $hour;
my $newhour =timeFormat($hour);
my $ampm =AmPmFormat($oldhour);


$newtime = $newhour . ":" . $min . " $ampm";
#-----------------------------------------making the directories------------------------------------------------
$newdir=$Branch . $dateString;
system "mkdir $rootout/gpms/output/mrf/$newdir";
system "mkdir $fgdcdir/$newdir";
$ISODir= "$ISOdir/$newdir";
system "mkdir $ISOdir/$newdir";
$ISOEADir="$ISOdir/$newdir/EAFiles";
system "mkdir $ISOEADir";

$newerrdir ="$errlogdir/$newdir";
system "mkdir $newerrdir";
chmod 0777, "$outputdir/$newdir";
chmod 0777, "$newerrdir";
chmod 0777, "$fgdcdir/$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;

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

$ppos=index($flag,"p");
$lpos=index($flag,"l");
$apos=index($flag,"a");
$mpos=index($flag,"m");
$vpos=index($flag,"v");
$tpos=index($flag,"t");
$epos=index($flag,"e");
$hpos=index($flag,"h");

#posting flag
$postflag = substr($_,$flagpos+4,4);#was 7,2
$posthash{$flagless}=$postflag;
$postflaglength=length($postflag);
$filename="$indir/$flagless";

$np_pos=index($postflag,"p");

if ($ppos>=0 or $lpos>=0 or $apos>=0 or $mpos>=0 or $vpos>=0 or $tpos>=0 or $epos>=0 or $hpos>=0){
chomp($flagless);
$filename[$first_counter]="$indir/$flagless";
$flagarray[$first_counter]="$flag";

if ($ppos>=0){

if ($np_pos >0){
$plannedarray[$planned_counter]="$indir/$flagless";
$planned_counter++;
}else{
$flag_array_counter++;
&flagless;
};


}elsif($lpos>=0){

if ($np_pos >0){
$legacyarray[$legacy_counter]="$indir/$flagless";
$legacy_counter++;
}else{
$flag_array_counter++;
&flagless;
};

}elsif($apos>=0){

if ($np_pos >0){
$atlasarray[$atlas_counter]="$indir/$flagless";
$atlas_counter++;
}else{
$flag_array_counter++;
&flagless;
};

}elsif($mpos>=0){

if ($np_pos >0){
$maparray[$map_counter]="$indir/$flagless";
$map_counter++;
}else{
$flag_array_counter++;
&flagless;
};

}elsif($vpos>=0){

if ($np_pos >0){
$vectorarray[$vector_counter] = "$indir/$flagless";
$vector_counter++;
}else{
$flag_array_counter++;
&flagless;
};

}elsif($epos>=0){


if ($np_pos >0){
$edgesarray[$edges_counter] = "$indir/$flagless";
$edges_counter++;
}else{
$flag_array_counter++;
&flagless;
};

}elsif($hpos>=0){

if ($np_pos >0){
$edgesenhancedarray[$edgesenhanced_counter] = "$indir/$flagless";
$edgesenhanced_counter++;
}else{
$flag_array_counter++;
&flagless;
};


}elsif($tpos>=0){

if ($np_pos >0){
$tabarray[$tab_counter] = "$indir/$flagless";
$tab_counter++;
}else{
$flag_array_counter++;
&flagless;
};

};
$first_counter++;
} else{

$noflagarray[$noflag_array_counter]=$flagless;
$noflag_array_counter++;


};

}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
chomp($flagless);
$flag=substr($_,$flagpos+5,1);
$ppos=index($flag,"p");
$lpos=index($flag,"l");
$apos=index($flag,"a");
$mpos=index($flag,"m");
$vpos=index($flag,"v");
$tpos=index($flag,"t");
$epos=index($flag,"e");
$hpos=index($flag,"h");
if ($ppos>=0 or $lpos>=0 or $apos>=0 or $mpos>=0 or $vpos>=0 or $tpos>=0 or $epos>=0 or $hpos>=0){
chomp($flagless);
$filename[$first_counter]="$flagless";
$flagarray[$first_counter]="$flag";



#posting flag
$postflag = substr($_,$flagpos+4,4);
$posthash{$flagless}=$postflag;
$postflaglength=length($postflag);
$np_pos=index($postflag,"p");



if ($ppos>=0){
if ($np_pos >0){
$plannedarray[$planned_counter]="$flagless";
$planned_counter++;
}else{
&flagless;
};

}elsif($lpos>=0){
if ($np_pos >0){
$legacyarray[$legacy_counter]="$flagless";
$legacy_counter++;
}else{
&flagless;
};

}elsif($apos>=0){
if ($np_pos >0){
$atlasarray[$atlas_counter]="$flagless";
$atlas_counter++;
}else{
&flagless;
};

}elsif($mpos>=0){
if ($np_pos >0){
$maparray[$map_counter]="$flagless";
$map_counter++;
}else{
&flagless;
};

}elsif($vpos>=0){
print "in the v";
if ($np_pos >0){
$vectorarray[$vector_counter] = "$flagless";
$vector_counter++;
}else{
&flagless;
};

}elsif($epos>=0){
if ($np_pos >0){
$edgesarray[$edges_counter] = "$flagless";
$edges_counter++;
}else{
&flagless
};

}elsif($hpos>=0){
if ($np_pos >0){
$edgesenhancedarray[$edgesenhanced_counter] = "$flagless";
$edgesenhanced_counter++;
}else{
&flagless;
};

}elsif($tpos>=0){

if ($np_pos >0){
$tabarray[$tab_counter] = "$flagless";
$tab_counter++;
}else{
&flagless;
};
};
$first_counter++;
} else{
$noflagarray[$noflag_array_counter]=$flagless;
$noflag_array_counter++;
};
};
};


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

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

$countingtotal=1;

print SAVEOUT"\n Now transforming the files--------------------------------------------------------------------------------------------------------------------\n";
#------------------------------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++;
};
#-------------------------------------------------------------------------------------------------

foreach $vectorarray (@vectorarray){
if (-e $vectorarray ){
chomp($vectorarray);
$fileslash= rindex($vectorarray, "/");
$mifFile=substr($vectorarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF vector format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $vectorarray  $root/gpms/FGDC2MRF/FGDC2mif_vector.xsl";#was -vw
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
system "cp $newmifFile $mifFile";
}else{
$noexista = $vectorarray;
&no_exist;
};
};



foreach $edgesarray (@edgesarray){
if (-e  $edgesarray){
chomp($edgesarray);
$fileslash= rindex($edgesarray, "/");
$mifFile=substr($edgesarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF edges format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $edgesarray  $root/gpms/FGDC2MRF/FGDC2mif_edges.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $edgesarray;
&no_exist;
};

};

#       $edgesenhancedarray
foreach $edgesenhancedarray (@edgesenhancedarray){
if (-e $edgesenhancedarray){
chomp($edgesenhancedarray);
$fileslash= rindex($edgesenhancedarray, "/");
$mifFile=substr($edgesenhancedarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF edgesEnhanced format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $edgesenhancedarray  $root/gpms/FGDC2MRF/FGDC2mif_edgesEnhanced.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $edgesenhancedarray;
&no_exist;
};
};



foreach $tabarray (@tabarray){
if (-e $tabarray){
chomp($tabarray);
$fileslash= rindex($tabarray, "/");
$mifFile=substr($tabarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF tabular format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $tabarray  $root/gpms/FGDC2MRF/FGDC2mif_tabular.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $tabarray;
&no_exist;
};
};

foreach $plannedarray (@plannedarray){
if (-e $plannedarray){
chomp($plannedarray);
$fileslash= rindex($plannedarray, "/");
$mifFile=substr($plannedarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF planned format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $plannedarray  $root/gpms/FGDC2MRF/FGDC2mif_planned.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $plannedarray;
&no_exist;
};
};


foreach $legacyarray (@legacyarray){
if (-e $legacyarray){
chomp($legacyarray);
$fileslash= rindex($legacyarray, "/");
$mifFile=substr($legacyarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF legacy format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $legacyarray  $root/gpms/FGDC2MRF/FGDC2mif_legacy.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $legacyarray;
&no_exist;
};
};

foreach $atlasarray (@atlasarray){
if (-e $atlasarray){
chomp($atlasarray);
$fileslash= rindex($atlasarray, "/");
$mifFile=substr($atlasarray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF atlas format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $atlasarray  $root/gpms/FGDC2MRF/FGDC2mif_atlas.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $atlasarray;
&no_exist;
};
};


foreach $maparray (@maparray){
if (-e $maparray){
chomp($maparray);
$fileslash= rindex($maparray, "/");
$mifFile=substr($maparray,$fileslash+1);
$newmifFile = $mifFile . "_org";

&mif_copy;

$origal_array[$origal_array_counter]=$newmifFile;
$origal_array_counter++;
$unordered = "MIF_$mifFile";
open STDOUT,">$unordered";
open STDERR, ">$unordered.err";
printf SAVEOUT "$countingtotal: Please wait, now converting $mifFile from the FGDC format to the MRF map format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $maparray  $root/gpms/FGDC2MRF/FGDC2mif_map.xsl";
my $mif = system(@csax);

&mif_is_zero;

close STDOUT;
close STDERR;
}else{
$noexista = $maparray;
&no_exist;
};

};

printf SAVEOUT"\n Now validating the files---------------------------------------------------------------------------------------------------------------------------------\n";
$validate=1;
foreach $mifarray (@mifarray){

$TMP = "TMP_$mifarray";
$outformmrf = substr($mifarray,4);
$MRF = "$outformmrf.mrf";
$MIF="MIF2_$mifarray";
$err= "$MIF.err";

open MIFFILE, "<$mifarray";
open MIFFINAL,">$MIF";

$gidsourcecounter=0;
$gidsourcecounter2=0;
$sdtscounter=0;
$entcounter=0;
$attcounter=0;
$domcounter=0;
$overcounter=0;
$horzcounter=0;
$obliquecounter=0;
$obliquelongcounter=0;
$digopt=1;
$offcounter=0;
$networkcounter=0;

while(<MIFFILE>){
if($_ =~ /Originator section="source" gid1=/){
   $gidsourcecounter++;
   $gidsourcecounter2++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $secondstring . $gidsourcecounter2 . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /%/){
   $perloc =index($_, "%");
   $per1= substr($_,0,$perloc);
   $per2 =substr($_,$perloc+1);
   $finalstring =$per1 . "%%" . $per2;
   $perloc2 = index($per2, "%");
      
   if ($perloc2 < 0){
   if($finalstring =~ /<Enumerated_Domain_Value_Definition gid1="/){
    
   
   $gidlocation =index($finalstring, "gid1");
   $gid2location =index($finalstring, "gid2");
   $gid3location =index($finalstring, "gid3");
   $dashloc=index($finalstring, ">");
   $originalstring= substr($finalstring,0,$gidlocation+6);
   $secondstring=substr($finalstring,$gid2location-2,8);
   $thirdstring=substr($finalstring,$gid3location-2,8);
   $fourthstring = substr($finalstring,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter_Enumerated_Domain . $fourthstring;
   printf MIFFINAL ("$finalstring");


   }elsif($finalstring =~ /<Unrepresentable_Domain gid1="/){
   $domcounter++;
   $gidlocation =index($finalstring, "gid1");
   $gid2location =index($finalstring, "gid2");
   $gid3location =index($finalstring, "gid3");
   $dashloc=index($finalstring, ">");
   $originalstring= substr($finalstring,0,$gidlocation+6);
   $secondstring=substr($finalstring,$gid2location-2,8);
   $thirdstring=substr($finalstring,$gid3location-2,8);
   $fourthstring = substr($finalstring,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
    }else{
   printf MIFFINAL ("$finalstring");
   };
   }elsif ($perloc2>0){
    $per3 = substr($per2,0,$perloc2);
    $per4 =substr($per2,$perloc2+1);
    $finalstring2 =$per1 . "%%" . $per3 . "%%" . $per4;
    printf MIFFINAL ("$finalstring2");
   };

   }elsif($_ =~ /<Oblique_Line_Latitude gid1="/){
   $obliquecounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $obliquecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Oblique_Line_Longitude gid1="/){
    $obliquelongcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $obliquelongcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");

   }elsif($_ =~ /<Horizontal_Positional_Accuracy_Value gid1=/){
   $horzcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $horzcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Horizontal_Positional_Accuracy_Explanation gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $horzcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Publication_Date section="source" gid1=/){
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Title section="source" gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Type_of_Source_Media gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Beginning_Date section="source" gid1/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Ending_Date section="source" gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Source_Currentness_Reference section="source" gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /Source_Citation_Abbreviation section="source" gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Source_Contribution section="source" gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $gidsourcecounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<SDTS_Point_and_Vector_Object_Type gid1=/){
    $sdtscounter++;
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $sdtscounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
     }elsif($_ =~ /<Point_and_Vector_Object_Count gid1=/){
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $sdtscounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Entity_Type_Label gid1=/){
    $entcounter++;
    $attcounter=0;
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Entity_Type_Definition gid1="/){
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Entity_Type_Definition_Source gid1="/){
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Attribute_Label gid1="1"/){
   $attcounter++;
   $domcounter=0;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Attribute_Definition gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Attribute_Definition_Source gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Enumerated_Domain_Value gid1="/){
   $domcounter++;
   $domcounter_Enumerated_Domain=$domcounter;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Enumerated_Domain_Value_Definition gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter_Enumerated_Domain . $fourthstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Enumerated_Domain_Value_Definition_Source gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Range_Domain_Minimum gid1="/){
   $domcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Range_Domain_Maximum gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Attribute_Units_of_Measure gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
    printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Codeset_Name gid1="/){
   $domcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Codeset_Source gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
    printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Unrepresentable_Domain gid1="/){
   $domcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $entcounter . $secondstring . $attcounter . $thirdstring . $domcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Entity_and_Attribute_Overview gid1=/){
   $overcounter++;
    $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $overcounter . $thirdstring;
    printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Entity_and_Attribute_Detail_Citation gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $overcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
  }elsif($_ =~ /<Format_Name gid1="/){
   $offcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<File_Decompression_Technique gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Transfer_Size gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Network_Resource_Name gid1="/){
   $networkcounter++;
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $gid3location =index($_, "gid3");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring=substr($_,$gid3location-2,8);
   $fourthstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring . $networkcounter . $fourthstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Offline_Media gid1=/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
    }elsif($_ =~ /<Recording_Format /){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
   $originalstring= substr($_,0,$gidlocation+6);
   $secondstring=substr($_,$gid2location-2,8);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring . $digopt . $secondstring . $offcounter . $thirdstring;
   printf MIFFINAL ("$finalstring");
   }elsif($_ =~ /<Fees gid1="/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $digoptorg=$digopt;
   $finalstring= $originalstring .  $digopt . $thirdstring;
   $digopt++;
   $offcounter=0;
   $networkcounter=0;
   printf MIFFINAL ("$finalstring");
  }elsif($_ =~ /<Ordering_Instructions/){
   $gidlocation =index($_, "gid1");
   $gid2location =index($_, "gid2");
   $dashloc=index($_, ">");
    $originalstring= substr($_,0,$gidlocation+6);
   $thirdstring = substr($_,$dashloc-1);
   $finalstring= $originalstring .  $digoptorg . $thirdstring;
   printf MIFFINAL ("$finalstring");
 
   }else{
   printf MIFFINAL "$_";
};


};
close MIFFINAL;
close MIFFILE;

open STDOUT,">$TMP";
open STDERR,">$err";
printf SAVEOUT "$validate: Please Wait, now validating $mifarray\n";
$validate++;
my @csax = "java com.saxonica.Transform -outval:recover $MIF $root/gpms/xsl/MIF2MRF.xsl";
my $final = system(@csax);

if ($final == 0){
system "rm $err";

$passed[$passcounter]=$MRF;
$passcounter++;

open MRFOUTPUT, ">$MRF";
open TMPFILE, "<$TMP";

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;/){
                           $amploc1=index($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>

$rmmif[$removemifcounter]=$mifarray;
$removemifcounter++;

system "rm $TMP";
system "rm $MIF";

}else{
$invalid[$invalidcounter]=$TMP;
$invalidcounter++;

$FGDCINFile=substr($TMP,8);
#print SAVEOUT "\$FGDCINFile = $FGDCINFile\n";
system "cp $indir/$FGDCINFile  $errlogdir/$newdir";

if (-s $mifarray ){
system "mv $mifarray $errlogdir/$newdir";
}else{
system "rm $mifarray";
};

if (-s $MIF){
system "mv $MIF $errlogdir/$newdir";
}else{
system "rm $MIF";
};

system "mv $err $errlogdir/$newdir";

if (-s $TMP){
system "mv $TMP $errlogdir/$newdir";
}else{
system "rm $TMP";
};

};

};

foreach $passed (@passed){
$fgdcdotloc=rindex($passed,"."); 
$indicate =substr($passed,0,$fgdcdotloc);
$passvar = $posthash{$indicate};
$npos=index($passvar,"n");
system "rm $mifarray";

if ($flag_array_counter > 0 or $npos <0){

if ($fgdcoutcountera  ==0){
print SAVEOUT "\nNow converting the MRF files to the FGDC xml format so that they can be posted on the GOS and FGDC portals------------------------\n\n";
$fgdcoutcountera++;
$fgdcoutcounter =1;
};

};

if ($npos <0){
$FGDC="$indicate";
open STDOUT,">$FGDC";
open STDERR,">$FGDC.err";
print SAVEOUT "$fgdcoutcounter: Now transforming $passed to the FGDC output format\n";
#$fgdcoutcounter++;
my @csax = "java com.saxonica.Transform -outval:recover $passed $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);

if ($fgdcout == 0){
system "mv $passed $rootout/gpms/output/mrf/$newdir";
$postarray[$postarraycounter] = $passed;
$postarraycounter++;
system "rm $FGDC.err";


print SAVEOUT "$fgdcoutcounter: Now transforming $passed to the ISO 19115-2 output format\n";
$fgdcoutcounter++;
$iso_var= "ISO_" . $FGDC;

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

my @csax = "java com.saxonica.Transform -outval:recover $FGDC  $root/gpms/FGDC2ISO/csdgm2iso19115-2_master.xslt";
my $ISOmif = system(@csax);

#print SAVEOUT "\$ISOmif $ISOmif \n";
#print SAVEOUT "\$ISOmif = $ISOmif\n";
if ($ISOmif == 0){
$NewISOFileArray[$NewISOFileArrayCounter] = $iso_var;
$NewISOFileArrayCounter++;
system "rm $iso_var.err";
system "mv $FGDC $rootout/gpms/output/fgdc/$newdir";

}else{
print SAVEOUT "Here for the errors!!!!!!!!!\n";
system "mv $iso_var $newerrdir";
system "mv $iso_var.err $newerrdir";
$badFile[$badFileCounter] =$passed;
$badFileCounter++;
};




}else{

if (-e $passed){
print SAVEOUT "Trouble with $FGDC\n";
}else{
print SAVEOUT "The file $passed could not be found. \nThis is because $passed is a duplicate and a previous version of $passed has already been transformed.\n"; 

};

};

}else{
$nopostarray[$nopostcounter]=$passed;
$nopostcounter++;
system "mv $passed $rootout/gpms/output/mrf/$newdir";
};

};

#existance loop


if ($badxmlcounter >0){

foreach $badxml (@badxml){
open ERRREAD, "<$badxml.err";
while(<ERRREAD>){
if($_ =~ /does not exist/){
$noexistarray[$existcounter]=$badxml;
$existcounter++;
$badxmlcounter=$badxmlcounter-1;
system "mv $badxml.err $errlogdir/$newdir";

if (-s $badxml){
system "mv $badxml $errlogdir/$newdir";
}else{
system "rm $badxml";
};

}else{
if($malformed[$mal-1] ne $badxml){
$malformed[$mal]=$badxml;
$mal++;
system "mv $badxml.err $errlogdir/$newdir";
system "rm $badxml";
};
};
};
close ERRREAD;
};
};



foreach $rmmif (@rmmif){
system "rm $rmmif";
};





#-----------------------------------------opening the file to inserted an updated value for gml:TimePeriod --------------------------------------------------------------
print SAVEOUT "\n";
foreach $NewISOFileArray (@NewISOFileArray){
$gmlUpdateVariable =1;
print SAVEOUT "Now correcting $NewISOFileArray ------------------\n";
system "chmod 777 $NewISOFileArray"; 

$iso_varDotLocation=index($NewISOFileArray,".");
$iso_var_pre=substr($NewISOFileArray,0, $iso_varDotLocation);
$new_iso_var= $iso_var_pre . "_corrected.shp.xml";
open ISOFILE, "<$NewISOFileArray";
my (@lines) = <ISOFILE>; 
open NEWISOFILE, ">$new_iso_var";


foreach $line (@lines){ # loop thru list
if($line =~ /gml:TimePeriod xmlns:gml/){
$gmlIDLocation=index($line,"gml:id");
$gmlPreId = substr($line,0,$gmlIDLocation-1);
$PostgmlID=substr($line,$gmlIDLocation);
$BracketLocation=index($PostgmlID,">")-1;
$gmlID=substr($PostgmlID,0,$BracketLocation);
$newGMLID = $gmlID . $gmlUpdateVariable;
$newLine= $gmlPreId . " " . $newGMLID . "\">";
print NEWISOFILE "$newLine";
$gmlUpdateVariable++;
}elsif($line =~ /gmi:MI_Metadata/){
print NEWISOFILE "$line";

}else{
print NEWISOFILE "$line";


};
close ISOFILE;
};

system "rm $NewISOFileArray";
system "cp $new_iso_var $NewISOFileArray";
system "mv $NewISOFileArray $ISOdir/$newdir";
system "rm $new_iso_var";
};
#---------------------------------------making the Entity and Attribute Files-----------------------------------------------------------------------------------------
print SAVEOUT "\n";
foreach $passed (@passed){

#----------------------------------------determing the filetype---------------------------------------------------------------------------------------------
$lastDot=rindex($passed,".");
$lastPassed=substr($passed,0,$lastDot);

$tigerOrgz =substr($lastPassed,0,2);
$PosBegDash=index($lastPassed,"_")+1;
$FileYear=substr($lastPassed,$PosBegDash,4);

if ($tigerOrgz eq "tl"){
$posofDot=index($lastPassed,".");
$posofDash=rindex($lastPassed,"_")+1;
$fileTypeLength= $posofDot-$posofDash;
$filetype=substr($lastPassed,$posofDash,$fileTypeLength);
$iso_var_ea= "ISO_EA_" . $FileYear . "_" . $filetype . ".xml";
$iso_var_ea_error = "ISO_EA_" . $FileYear . "_" . $filetype . ".err";
}elsif($tigerOrgz eq "gz"){

$LocPostYear=$PosBegDash+5;
$postYear=substr($lastPassed,$LocPostYear);
$gzFirstDash=index($lastPassed,"_")+1;
$FileYear=substr($lastPassed,$gzFirstDash,4);
$postYear= substr($lastPassed,$gzFirstDash);
$gzSecondDash=index($postYear,"_")+1;
$postYear2= substr($postYear,$gzSecondDash);
$gzThirdDash=index($postYear2,"_")+1;

$filetype=substr($postYear2,$gzThirdDash,3);
$iso_var_ea= "ISO_EA_" . $FileYear . "_" . $filetype . ".xml";
$iso_var_ea_error = "ISO_EA_" . $FileYear . "_" . $filetype . ".err";
}elsif($tigerOrgz eq "DC"){
$FirstDashLocation=index($lastPassed,"_")+1;
$LastDashLocation=rindex($lastPassed,"_");
$FileTypeLength=$LastDashLocation-$FirstDashLocation;
$Theme=substr($lastPassed,$FirstDashLocation,$FileTypeLength);
$iso_var_ea= "ISO_EA_" . $Theme . ".xml";
$iso_var_ea_error = "ISO_EA_" . $Theme . ".err";

}else{
open UNIDTHEME, ">>$newerrdir/$logname/UnknownThemes.txt";
$UnIdThemeArray[$UnIdThemes]=$lastPassed;
$UnIdThemes++;
print UNIDTHEME "$lastPassed\n";
close UNIDTHEME;
};
#------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
$testDir = $ISOEADir . "/" . $iso_var_ea;

if (-e $testDir){
system "rm $iso_var_ea_error";
}else{
print SAVEOUT "now making the Entity and Attribute file $iso_var_ea-------------------------------\n";


open STDOUT,">$iso_var_ea";
open STDERR, ">$iso_var_ea_error";
my @csaxea = "java com.saxonica.Transform -outval:recover  $indir/$lastPassed  $root/gpms/IsoSchema/Entity-n-Attributes-to-Feature-Catalog.xslt";#Sec52iso.xslt
my $mifea = system(@csaxea);

if ($mifea == 0){
open EAFILE, "<$iso_var_ea";
while (<EAFILE>){
$line=$_;


$FinalEAFile ="$iso_var_ea/$ISOEADir";
if (-e $FinalEAFile){
print SAVEOUT "File Exists!!!!!!!!!!!!!!\n";
}else{
if ($LastFinalEAFile  ne $FinalEAFile){
$EAArray[$EAFileCounter]= $iso_var_ea;
$EAFileCounter++;
$LastFinalEAFile = $FinalEAFile;
};
};#while



system "mv $iso_var_ea $ISOEADir";
system "rm $iso_var_ea_error";
};
}else{
print SAVEOUT "moving the error files\n";
system "mv $iso_var_ea $newerrdir";
system "mv $iso_var_ea_error $newerrdir";

};
};

};


$counterbadb=1;
$counterexistb=1;
$passcounterb=1;
$invalidcounterb=1;
$total= $mifarraycounter + $mal+ $existcounter;
$newtotal = $total+$flag_array_counter;
close STDERR;
#-------------------------------origial section------------------------------------
#$origal_array
foreach $origal_array(@origal_array){
$len_origal_array=length($origal_array)-4;
$new_org=substr($origal_array,0,$len_origal_array);
system "cp $origal_array $new_org\n";
system "cp $new_org $indir";
system "rm $origal_array";
};

#------------------------------grammer section------------------------------------
if ($existcounter == 1){
$existfilename ="file";
}elsif ($existcounter>1){
$existfilename ="files";
};

if ($badxmlcounter == 1){
$badxmlfilename = "This file";
$badxmlfilename2 = "file";
}elsif ($badxmlcounter > 1 or $badxmlcounter==0){
$badxmlfilename = "These files";
$badxmlfilename2 = "files";
};

if ($passcounter == 1){
$passfilename ="file";
}else{
$passfilename ="files";
};

if ($invalidcounter == 1){
$invalidfilename = "file";
}elsif ($invalidcounter >1){
$invalidfilename = "files";
};

if ($flag_array_counter == 1){
$flag_array_counter_filename ="file";
}elsif ($flag_array_counter > 1){
$flag_array_counter_filename ="files";
};

if ($mal == 1){
$malfilename = "file";
}elsif($mal > 1){
$malfilename = "files";
};


if ($nopostcounter ==1){
$nopostcounterverb = "was";
}elsif ($nopostcounter >1){
$nopostcounterverb = "were";
}else{
$nopostcounterverb = "were";
};

if ($postarraycounter ==1){
$postarraycounterverb ="was";
}elsif ($postarraycounter >1){
$postarraycounterverb = "were";
}else{
$postarraycounterverb = "were";
};

if ($noPostFlagArray_counter == 1){
$noPostFlagArrayverb = "file does";
$noPostFlagArrayverbTwo = "it is";
$noPostFlagArrayFilename = "file";
}else{
$noPostFlagArrayverb = "files do";
$noPostFlagArrayverbTwo = "they are";
$noPostFlagArrayFilename = "files";
};

if ($noflag_array_counter == 1){
$noflagVerb = "file does";
$noflagVerbTwo  = "it is";
$noflag_array_counter_filename ="file";
}else{
$noflagVerb = "files do";
$noflagVerbTwo  = "they are";
$noflag_array_counter_filename ="files";
}; 

#-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){
open RERUN, ">$ReRunFile";
print SAVEOUT "opening the FailedFiles.txt file\n";
};


#-------------------------Setting up the LOG FILE---------------------------------------------
open LOG, ">$errlogdir/$newdir/$newdir.log";
printf LOG "This log details the transformation and validation results of the $newtotal fgdc input files contained in $combolist.\n";


# -------------------------------Listing unfound Files-----------
if ($slash_directory_name_counter >0){
printf LOG "\nThese files can be found in the following directories:\n";
printf LOG "1. $indir (This is the directory given at the command line)\n";
$slash_directory_nameb =2;
@slash_directory_name=sort(@slash_directory_name); 

foreach $slash_directory_name (@slash_directory_name){

$slash_directory_nameca =$slash_directory_namec-1; 

if ($slash_directory_name[$slash_directory_namec] ne $slash_directory_name[$slash_directory_nameca] and $slash_directory_name[$slash_directory_namec] ne $indir){
printf LOG "$slash_directory_nameb: $slash_directory_name\n";
$slash_directory_nameb++;
$slash_directory_namec++;
}elsif($slash_directory_namec == 0){
printf LOG "$slash_directory_nameb: $slash_directory_name\n";
$slash_directory_nameb++;
$slash_directory_namec++;
};
};

printf LOG "\n$0 was run on $monnum/$daynum/$year at $time ($newtime) for the $Branch product.\n\n";
}else{
printf LOG "\nThese files can be found in the $indir directory.\n";
printf LOG "It was run on $monnum/$daynum/$year at $time ($newtime) for the $Branch product.\n\n";
};

#-----------------------------------Grammar Section------------------------------------------------------------------------

if ($total == 1 ){
$total_filenoun = "file";
$total_fileverb = "was";
}else{
$total_filenoun = "files";
$total_fileverb = "were";
};


if ($mifarraycounter == 1 ){
$mifarraycounte_filenoun = "file";
$mifarraycounte_fileverb = "was";
}else{
$mifarraycounte_filenoun = "files";
$mifarraycounte_fileverb = "were";
};

if ($passcounter == 1 ){
$passcounter_filenoun = "file";
$passcounter_fileverb = "was";
}else{
$passcounter_filenoun = "files";
$passcounter_fileverb = "were";
};

if ($postarraycounter ==1 ){
$postarraycounter_file = "file";
$postarraycounter_version ="a FGDC version and an ISO 19115-2 version";
}else{
$postarraycounter_file = "files";
$postarraycounter_version ="FGDC and ISO 19115-2 versions";
};

if ($nopostarraycounter ==1 ){ 
$nopostarraycounter_file = "file";
$nopostarraycounter_version ="a FGDC version" ;
}else{ 
$nopostarraycounter_file = "files"; 
$nopostarraycounter_version ="FGDC versions";
}; 
#--------------------------------------------indicator section---------------------------------------------------------------------------------------

print SAVEOUT "\nOf the $total $total_filenoun which $total_fileverb transformed from FGDC format to a MRF format, $mifarraycounter $mifarraycounte_filenoun $mifarraycounte_fileverb transformed sucessfully and\n";
print SAVEOUT " $badxmlcounter $badxmlfilename2 could not be transformed because of errors in their xml. Of the $mifarraycounter\n";
print SAVEOUT "$mifarraycounte_filenoun  that $mifarraycounte_fileverb transformed, $passcounter passed validation and $invalidcounter failed validation.";
print SAVEOUT " Of the $passcounter $passcounter_filenoun that passed validation, $postarraycounter $postarraycounterverb\n";
print SAVEOUT "transformed into the FGDC and ISO 19115-2 formats and $nopostcounter $nopostcounterverb not transformed.";


print LOG "\nOf the $total $total_filenoun which $total_fileverb transformed from FGDC format to a MRF format, $mifarraycounter $mifarraycounte_filenoun $mifarraycounte_fileverb transformed sucessfully and\n";
print LOG " $badxmlcounter $badxmlfilename2 could not be transformed because of errors in their xml. Of the $mifarraycounter\n";
print LOG "$mifarraycounte_filenoun  that $mifarraycounte_fileverb transformed, $passcounter passed validation and $invalidcounter failed validation.";
print LOG " Of the $passcounter $passcounter_filenoun that passed validation, $postarraycounter $postarraycounterverb\n";
print LOG "transformed into the FGDC and ISO 19115-2 formats and $nopostcounter $nopostcounterverb not transformed.";

if($EAFileCounter>0){
print SAVEOUT " For these ISO 19115-2 files, $EAFileCounter Entity and Attribute files were made.\n";
print LOG "  For these ISO 19115-2 files, $EAFileCounter Entity and Attribute files were made.\n";

};



if ($existcounter >0){

if ($flag_array_counter == 0){
print SAVEOUT " Also $existcounter $existfilename could not be found.\n";
print LOG " Also $existcounter $existfilename could not be found.\n";
}elsif ($flag_array_counter>0){
print SAVEOUT " Also $existcounter $existfilename could not be found and ";
print LOG " Also $existcounter $existfilename could not be found and ";
};

}else{
print SAVEOUT"\n";
print LOG "\n";
};

# ------------------------------posting flag----------------------------------------------------------------------------------------
if ($noflag_array_counter>0){
print SAVEOUT "$flag_array_counter $noflag_array_counter_filename could not be processed because the $noflagVerb have a";
print SAVEOUT " profile flag and a FGDC posting flag.\n";
print LOG "$flag_array_counter $noflag_array_counter_filename could not be processed because the $noflagVerb have a"; 
print LOG " profile flag and a FGDC posting flag.\n";
$noflagcounter=1;

if ($noPostFlagArray_counter<=0){
print SAVEOUT"\n";
print LOG "\n";
};

};

if ($noPostFlagArray_counter>0){
print SAVEOUT "$noPostFlagArray_counter $noPostFlagArrayFilename  could not be processed because the $noPostFlagArrayverb not have a FGDC posting flag.\n";
print LOG "$noPostFlagArray_counter $noPostFlagArrayFilename  could not be processed because the $noPostFlagArrayverb not have a FGDC posting flag.\n";
print SAVEOUT"\n";
print LOG "\n";
};

if ($noflag_array_counter>0){
print SAVEOUT "The following files do not have a profile flag and/or a posting flag. Please put a space and a p for planned files, a l for legacy files,\n";
print SAVEOUT "an a for atlas files, a m for map files, a v for vector files or a t for tabular files.";
print SAVEOUT  " Also please put another space and a p for post or np for not post\n";

print LOG "The following $noflagVerb not have a profile flag and/or a FGDC posting flag. Please put a space and a p for planned files, a l for legacy files,\n";
print LOG "an a for atlas files, a m for map files, ";
print LOG "a v for vector files  or a t for tabular files. Also please put another space and a p for post or np for not post.\n";

foreach $noflagarray (@noflagarray){
chomp ($noflagarray);
$noflagarraylen =length($noflagarray);

$dotloc = index($noflagarray, ".");
$newflag = substr($noflagarray, $dotloc+4);
$newflaglength = length($newflag);

if ($newflaglength < 5){
print SAVEOUT "$noflagcounter: $noflagarray\n";
print LOG "$noflagcounter: $noflagarray\n";
print RERUN  "$noflagarray\n";
$noflagcounter++;
};
};

};

if ($noPostFlagArray_counter>0){ 
print SAVEOUT "\nThe following $noPostFlagArrayverb not have a posting flag. Please put a space after the profile flag and a p for post or np for not post.\n"; 
print LOG "\nThe following $noPostFlagArrayverb not have a posting flag. Please put a space after the profile flag and a p for post or np for not post.\n";
$noPostFlagArray_counterb=1;

for $noPostFlagArray (@noPostFlagArray){
print SAVEOUT "$noPostFlagArray_counterb:  $noPostFlagArray\n";
print LOG "$noPostFlagArray_counterb:  $noPostFlagArray\n";
$noPostFlagArray_counterb++;
};
};
#------------------------------end of posting flag----------------------------------------------------------------------------------------------------------------


#---------------------------------------------errors-in XML---------------------------------------------------------------------------------------------------------
if ($mal >0){
print SAVEOUT "\n\n$mal $malfilename could not be transformed because of errors in their xml. Please see the error files and intermediate files (MIF, MIF2 and TMP) at\n";
print SAVEOUT "$errlogdir/$newdir:\n";
print LOG "\n$mal could not be transformed because of errors in their xml. Please see the error files and intermediate files (MIF, MIF2 and TMP) at\n";
print LOG "$errlogdir/$newdir:\n";
foreach $malformed (@malformed){
print SAVEOUT "$counterbadb: $malformed\n";
print LOG "$counterbadb: $malformed\n";
$maloriginal =substr($malformed,4);
print RERUN "$maloriginal\n";
$counterbadb++;
};
};

if ($existcounter >0){
print SAVEOUT "\nThe following $existfilename could not be found.\n";
print LOG "\nThe following $existfilename could not be found.\n";

foreach $noexistarray (@noexistarray){
print SAVEOUT "$counterexistb: $noexistarray\n";
print LOG "$counterexistb: $noexistarray\n";
$counterexistb++;
};
};
#--------------------------------------------------end of errors-in XML---------------------------------------------------------------------------------------------


#--------------------------------------------------passed Validation -----------------------------------------------------------------------------------------
if ($passcounter >0){

if ($postarraycounter>0){
print SAVEOUT "\nThe following $postarraycounter_file passed validation and had $postarraycounter_version made for posting on the Census Metadata Server.\n";
print SAVEOUT "Please see them at $fgdcdir/$newdir(FGDC Files) and $ISOdir/$newdir(ISO Files)\n";

print LOG "\nThe following $postarraycounter_file passed validation and had $postarraycounter_version made for posting on the Census Metadata Server.\n";
print LOG  "Please see them at $fgdcdir/$newdir(FGDC Files) and $ISOdir/$newdir(ISO Files):\n";

foreach $postarray (@postarray){
print SAVEOUT "$passcounterb:$postarray\n";
print LOG "$passcounterb: $postarray\n";
$passcounterb++;
};
};

$passcounterb=1;

if ($nopostcounter>0){
print SAVEOUT "\nThe following $nopostarraycounter_file passed validation but did not have $nopostarraycounter_version made for posting on the Census Metadata Server:\n";
print LOG "\nThe following $nopostarraycounter_file passed validation but did not have $nopostarraycounter_file made for posting on the Census Metadata Server:\n";
foreach $nopostarray (@nopostarray){
print SAVEOUT "$passcounterb:$nopostarray\n";
print LOG "$passcounterb: $nopostarray\n";
$passcounterb++;
};
};
print SAVEOUT"\n";
print LOG "\n";
};
#---------------------------------------------end of passed validation------------------------------------------------------------------------------------

#----------------------------------------------Entity and Attribute Files--------------------------------------------------------------------------------
if($EAFileCounter>0){
print SAVEOUT "The following Entity and Attribute Files were made. Please see them at $ISOEADir:\n";
print LOG "The following Entity and Attribute Files were made Please see them at $ISOEADir:\n";
$NewEAFileCounter=1;

foreach $EAArray (@EAArray){
print SAVEOUT "$NewEAFileCounter: $EAArray\n";
print LOG "$NewEAFileCounter: $EAArray\n";
$NewEAFileCounter++;
};
print SAVEOUT"\n";
print LOG "\n";
};
#-----------------------------------end of Entity and Attribute Files------------------------------------------------------------------------------------

#-------------------------------------------missing Themes------------------------------------------------------------------------------------------
if ($UnIdThemes >0){
print SAVEOUT "The following filetypes could not be identified. Please see the UnknownThemes.txt file at $errlogdir/$newdir:\n";
print LOG "The following filetypes could not be identified. Please see the UnknownThemes.txt file at $errlogdir/$newdir:\n";

$UnIdThemeArrayCounter=1;
foreach $UnIdThemeArray (@UnIdThemeArray){
print SAVEOUT "$UnIdThemeArrayCounter: $UnIdThemeArray\n";
print LOG "$UnIdThemeArrayCounter: $UnIdThemeArray\n";
$UnIdThemeArrayCounter++;
};
print SAVEOUT"\n";
print LOG "\n";
};
#---------------------------------------end of missing themes-----------------------------------------------------------------------------------------------

#---------------------------------------------failed validation------------------------------------------------------------------------------------------
if ($invalidcounter >0){
print SAVEOUT "\nThe following $invalidfilename failed validation. Please see the error files and intermediate files (MIF, MIF2 and TMP) at $errlogdir/$newdir:\n";
print LOG "\nThe following $invalidfilename failed validation. Please see the error files and  intermediate files (MIF, MIF2 and TMP) at $errlogdir/$newdir: \n";

#open FAILEDFILESHANDLE, ">$errlogdir/$newdir/FailedFiles";


foreach $invalid (@invalid){
print SAVEOUT "$invalidcounterb:$invalid\n";
print LOG "$invalidcounterb:$invalid\n";
#print FAILEDFILESHANDLE "$invalid\n";
#print RERUN "$invalid\n";
eval{
print STDERR "$invalid \n";
};

$invalidoriginal = substr($invalid,8);
print RERUN "$invalidoriginal\n";
$invalidcounterb++;
};

};

#----------------------------end of failed validation-----------------------------------------------------------------------------------------------------------

print SAVEOUT "\nPlease see the log file at $errlogdir/$newdir/$newdir.log.\n";

system "chmod 777 $ReRunFile";
#print SAVEOUT "\$ReRunFile = $ReRunFile\n";
#sizeReRun= (-s $ReRunFile);

$sizeReRun = -s $ReRunFile;
#print SAVEOUT "\$sizeReRun = $sizeReRun\n";

print SAVEOUT " ";
if (-s $ReRunFile ==0){
#if ($newflaglength > 5 or $mal == 0 or $invalidcounter >0){
system "rm $errlogdir/$newdir/FailedFiles";
print SAVEOUT "\n";
#print SAVEOUT "Also, please see $errlogdir/$newdir/FailedFiles.txt, which contains a list of the files that did not pass validation,\n";
#print SAVEOUT "had bad xml or did not have a profile and/or posting flag.\n\n";
}else{
print SAVEOUT "Also, please see $errlogdir/$newdir/FailedFiles, which contains a list of the files that did not pass validation,\n";
print SAVEOUT "had bad xml or did not have a profile and/or posting flag.\n\n";
#system "rm $errlogdir/$newdir/FailedFiles";
#print SAVEOUT "\n";
};

$ErrInvalid = $invalidcounter +$existcounter+$mal +1;

if ($passcounter == 0){
print SAVEOUT "In the if-then\n";
system "rmdir $fgdcdir/$newdir";
system "rmdir  $rootout/gpms/output/mrf/$newdir";
};

if ($invalidcounter>0 or $existcounter>0 or $mal>0){
exit(2);
}else{
exit(0);
};




