#!/usr/bin/perl

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

use lib "/home/mccre004/GPMSscripts/module/DateTrans/lib";
use DateTrans qw(monName monNumber timeFormat AmPmFormat dayNumbeFormat);

use lib "/home/mccre004/GPMSscripts/module/DateTrans/lib";
use  xsdxsltcheck qw(xsdcheck xsltcheck FGDC2MRFcheck xsl4mrf2fgdccheck);

sub usage() {
    print "Usage: perl $0 [-listfile=<listfile>][-directory=<directory>][-product=<product>]\n";
    print "Example:\n";
    print "perl $0 -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";
}
%gOptions;
&GetOptions(\%gOptions, qw(listfile=s directory=s  product=s));
unless ($gOptions{listfile} and $gOptions{directory} and $gOptions{product}) {
        usage();
        exit(1);
};

#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

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

#-----------------------------------------------------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--------------------------------------------------------------------------------------------
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){
}else{
print "The file $combolist does not contain the list of files to be validated! \n";
print "Please make sure the file, $Infile,  contains a list of files to be validated.\n";
exit(1);
};
open (SAVEOUT,">&STDOUT");

&xsdcheck;
&xsltcheck;
&FGDC2MRFcheck;
&xsl4mrf2fgdccheck;
#------------------------------------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 $monnum =monNumber($mon);
my $Month_Number = 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;
system "mkdir $rootout/gpms/output/mrf/$newdir";
system "mkdir $fgdcdir/$newdir";
$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;
#print "reading $combolist (reading list)\n";
open INPUTFILE, "$combolist";
while(<INPUTFILE>){

$slashpos= rindex($_, "/");
#print "getting slash position--------------\n";
if ($slashpos < 0){


$filenameflag=$_;
$noflagarray[$flag_array_counter]=$filenameflag;

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

#print "\$flag = $flag(No Slash)\n-----------\n";
#print "----------------------------\n";
$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+7,2);
#print "\$postflag = $postflag(no slash)\n";
$posthash{$flagless}=$postflag;
$postflaglength=length($postflag);
$filename="$indir/$flagless";

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 ($postflaglength > 0){
$plannedarray[$planned_counter]="$indir/$flagless";
$planned_counter++;
}else{
$flag_array_counter++
};


}elsif($lpos>=0){

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

}elsif($apos>=0){

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

}elsif($mpos>=0){

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

}elsif($vpos>=0){

if ($postflaglength > 0){
$vectorarray[$vector_counter] = "$indir/$flagless";
$vector_counter++;
}else{
#print "here \n";
$flag_array_counter++;
print "no flag: vector/input $flag_array_counter(vector)\n";
};

}elsif($epos>=0){

if ($postflaglength > 0){
$edgesarray[$edges_counter] = "$indir/$flagless";
$edges_counter++;
}else{
#print "\n";
$flag_array_counter++;
print "no flagno flag edges/input):: $flag_array_counter\n";
};

}elsif($hpos>=0){

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


}elsif($tpos>=0){

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

$first_counter++;
} else{
$flag_array_counter++;
};

}elsif($slashpos > 0){

#for filenames with directories
$slashfilename = $newdir . "/" . $infile;
#print "\$_ = $_\n";

$flagpos=rindex($_,".");
$flagless=substr($_, 0,$flagpos+4);
#print "\$flagless = $flagless(directory)\n";
#print "------------------------------------------------\n";
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";
#print "\$filename[$first_counter] = $filename[$first_counter]\n";

if ($ppos>=0){

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

}elsif($lpos>=0){

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

}elsif($apos>=0){

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

}elsif($mpos>=0){

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

}elsif($vpos>=0){

if ($postflaglength > 0){
$vectorarray[$vector_counter] = "$flagless";
$vector_counter++;
}else{
&flagless;
};

}elsif($epos>=0){
#print "\$flagless = $flagless\n";
if ($postflaglength > 0){
$edgesarray[$edges_counter] = "$flagless";
$edges_counter++;
}else{
&flagless
};

}elsif($hpos>=0){

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

}elsif($tpos>=0){

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


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

$countingtotal=1;


print SAVEOUT"\n Now transforming the files\n";
#------------------------------module 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{
if(-e $flagless){
$flag_array_counter++;
print "no flag (vector/directory): $flag_array_counter\n";
};
};
#-------------------------------------------------------------------------------------------------

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 \nvector format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $vectorarray  $root/gpms/FGDC2MRF/FGDC2mif_vector.xsl";
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 \nedges format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $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 \nedgesEnhanced format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $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 \ntabular format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $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 \nplanned format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $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 -vw $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 -vw $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 i\nmap format. \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -vw $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 -vw $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++;

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";
$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 -vw $passed $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);

if ($fgdcout == 0){
system "mv $passed $rootout/gpms/output/mrf/$newdir";
system "mv $FGDC $rootout/gpms/output/fgdc/$newdir";
$postarray[$postarraycounter] = $passed;
$postarraycounter++;
system "rm $FGDC.err";
}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";
};


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

$ReRunFile ="$errlogdir/$newdir/FailedFiles";
open RERUN, ">$ReRunFile";

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";
printf LOG "These 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";

print SAVEOUT "\nOf the $total files which were transformed from FGDC format to a MRF format, $mifarraycounter files were transformed sucessfully and";
print SAVEOUT " $badxmlcounter $badxmlfilename2 could not be transformed because of errors in their xml. Of the $mifarraycounter files\n";
print SAVEOUT " that were transformed, $passcounter passed validation and $invalidcounter failed validation.";
print SAVEOUT " Of the $passcounter files that passed \n validation, $postarraycounter $postarraycounterverb";
print SAVEOUT " transformed into the FGDC format and $nopostcounter $nopostcounterverb not transformed.";

print LOG "\nOf the $total files which were transformed from FGDC format to a MRF format, $mifarraycounter files were transformed sucessfully and";
print LOG " $badxmlcounter $badxmlfilename2 could not be transformed because of errors in their xml. Of the $mifarraycounter";
print LOG " files that were transformed, $passcounter passed validation and $invalidcounter failed validation.";
print LOG " Of the $passcounter files that passed \nvalidation, $postarraycounter $postarraycounterverb";
print LOG " transformed into the FGDC format and $nopostcounter $nopostcounterverb not transformed.";

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

if ($flag_array_counter>0){
print SAVEOUT "$flag_array_counter $flag_array_counter_filename could not be processed because they are missing a\n";
print SAVEOUT "profile flag and a FGDC posting flag.\n";
print LOG "$flag_array_counter files could not be processed because they are missing a\n";
print LOG "profile flag and a FGDC posting flag.\n";
$noflagcounter=1;
print SAVEOUT"\n";
print LOG "\n";

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 files do 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 ($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++;
};
};

if ($passcounter >0){

if ($postarraycounter>0){
print SAVEOUT "\nThe following files passed validation and had FGDC versions made for posting on the Census \nMetadata Server:\n";
print LOG "\nThe following files passed validation and had FGDC versions made for posting on the Census \nMetadata Server:\n";

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

$passcounterb=1;

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


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

foreach $invalid (@invalid){
print SAVEOUT "$invalidcounterb:$invalid\n";
print LOG "$invalidcounterb:$invalid\n";

eval{
print STDERR "$invalid \n";
};

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

};


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


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

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

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




