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

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

#-----------------------------------------------------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";
$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 "------------------------------------------\n";
#print "\$_ = $_\n";
#print "\$flagpos = $flagpos\n";
#print "\$flagless= $flagless\n";
#print "\$flag = $flag\n";
#print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";



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

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

$np_pos=index($postflag,"p");
#print "\$np_pos = $np_pos\n";

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


}elsif($lpos>=0){

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

}elsif($apos>=0){

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

}elsif($mpos>=0){

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

}elsif($vpos>=0){
#print "in the v\n";
#print "\$postflaglength = $postflaglength\n";
#if ($postflaglength > 0){

if ($np_pos >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";
&flagless;
};

}elsif($epos>=0){


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

}elsif($hpos>=0){

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


}elsif($tpos>=0){

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

#$noflagarray[$flag_array_counter]=$filenameflag;
};
#print "no flag 1\n";
$first_counter++;
} else{
#print "no flag 2\n"; 
#noflagarray[$flag_array_counter]=$filenameflag;
#rint "\$noflagarray[$flag_array_counter]=$noflagarray[$flag_array_counter]\n";
#$flag_array_counter++;

$noflagarray[$noflag_array_counter]=$flagless;
#print "\$noflagarray[$noflag_array_counter] =$noflagarray[$flag_array_counter]\n";
$noflag_array_counter++;


};

}elsif($slashpos > 0){

#for filenames with directories
#$slashfilename = $newdir . "/" . $infile;
#print "------------------------------------------\n";
#print "\$_ = $_\n";
my $basename = basename $_;
my $dirnameA = dirname $_;
#print "\$basename = $basename\n";
#print "\$dirnameA = $dirnameA\n";

$slash_directory_name[$slash_directory_name_counter]=$dirnameA;
$slash_directory_name_counter++;

$flagpos=rindex($_,".");
#print "\$flagpos = $flagpos\n";
$flagless=substr($_, 0,$flagpos+4);#was 3
#print "\$flagless= $flagless\n"; 
chomp($flagless);
$flag=substr($_,$flagpos+5,1);
#print "\$flag = $flag\n";
#rint "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\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");
#print "\$epos = $epos\n";
$hpos=index($flag,"h");
#print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
if ($ppos>=0 or $lpos>=0 or $apos>=0 or $mpos>=0 or $vpos>=0 or $tpos>=0 or $epos>=0 or $hpos>=0){
#print "in the greater than zer0\n"; 
#print "\$epos = $epos\n";
chomp($flagless);
$filename[$first_counter]="$flagless";
$flagarray[$first_counter]="$flag";



#posting flag
#$postflag = substr($_,$flagpos+7,2);
$postflag = substr($_,$flagpos+4,4);
#print "\$postflag = $postflag(slash)\n";
$posthash{$flagless}=$postflag;
$postflaglength=length($postflag);
#filename="$indir/$flagless";
$np_pos=index($postflag,"p");
#print "\$np_pos = $np_pos\n";



if ($ppos>=0){
#print "in the p\n";
#if ($postflaglength > 0){
if ($np_pos >0){
$plannedarray[$planned_counter]="$flagless";
$planned_counter++;
}else{
&flagless;
};

}elsif($lpos>=0){
#print "in the l\n";
#f ($postflaglength > 0){
if ($np_pos >0){
$legacyarray[$legacy_counter]="$flagless";
$legacy_counter++;
}else{
&flagless;
};

}elsif($apos>=0){
#print "in the a";
#if ($postflaglength > 0){
if ($np_pos >0){
$atlasarray[$atlas_counter]="$flagless";
$atlas_counter++;
}else{
&flagless;
};

}elsif($mpos>=0){
#print "in the m";
#f ($postflaglength > 0){
if ($np_pos >0){
$maparray[$map_counter]="$flagless";
$map_counter++;
}else{
&flagless;
};

}elsif($vpos>=0){
print "in the v";
#f ($postflaglength > 0){
if ($np_pos >0){
$vectorarray[$vector_counter] = "$flagless";
$vector_counter++;
}else{
#print "Here!!!!!!!!!!!!!!!!\n";
&flagless;
};

}elsif($epos>=0){
#print "in the e\n";
#print "\$postflaglength = $postflaglength\n";
#f ($postflaglength > 0){
if ($np_pos >0){
$edgesarray[$edges_counter] = "$flagless";
$edges_counter++;
}else{
#print "Here!!!!!!!!!!!!!!!!\n";
&flagless
};

}elsif($hpos>=0){
#print "in the h";
#if ($postflaglength > 0){
if ($np_pos >0){
$edgesenhancedarray[$edgesenhanced_counter] = "$flagless";
$edgesenhanced_counter++;
}else{
&flagless;
};

}elsif($tpos>=0){

#f ($postflaglength > 0){
if ($np_pos >0){
$tabarray[$tab_counter] = "$flagless";
$tab_counter++;
}else{
#print "no flag\n";
&flagless;
};
#print "no flag2\n";
#$noflagarray[$flag_array_counter]=$filenameflag;
};
#print "no flag3\n";
$first_counter++;
} else{
#print "no flag4\n";
$noflagarray[$noflag_array_counter]=$flagless;
#print "\$noflagarray[$flag_array_counter] =$noflagarray[$flag_array_counter]\n";
$noflag_array_counter++;
};
};
};


#----------------------------------------------------------Saxon-----------------------------------------------------------------------------------------
#$ENV{'PATH'} = "/opt/saxon_lib/jdk1.5.0_11/bin:/mt/apps/gpms/output/mrf$newerrdir:$ENV{'PATH'}";#goodone
#$ENV{'PATH'} ="/opt/oracle/app/oracle/product/11.2.0/client_1/jdk:/mt/apps/gpms/output/mrf$newerrdir:$ENV{'PATH'}";#goodone2
$ENV{'PATH'} = "/home/saxon_lib/jdk1.5.0_11/bin:/mt/apps/gpms/output/mrf$newerrdir:$ENV{'PATH'}";#goodone3

$cp = "CLASSPATH";#goodone
#$ENV{$cp} = "/opt/saxon_lib/jdk1.5.0_11/lib/:/opt/saxon_lib/saxon9/saxon9sa.jar:/opt/saxon_lib/saxon9/saxon9-jdom.jar:/opt/saxon_lib/saxon9/:/opt/saxon_lib/saxon9/saxon9.jar:$ENV{$cp}";
#$ENV{$cp} = "/opt/oracle/app/oracle/product/11.2.0/client_1/lib/
$ENV{$cp} = "/home/saxon_lib/jdk1.5.0_11/lib/:/home/saxon_lib/saxon9/saxon9sa.jar:/home/saxon_lib/saxon9/saxon9-jdom.jar:/home/saxon_lib/saxon9/:/opt/saxon_lib/saxon9/saxon9.jar:$ENV{$cp}";
#---------------------------------------------------------------------------------------------------------------------------

$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{
#print"in the module\n";
#print "\$flagless = $flagless\n";
#if(-e $flagless){
#flag_array_counter++;
#rint "no flag (vector/directory): $flag_array_counter\n";
$noPostFlagArray[$noPostFlagArray_counter]="$flagless";
#print "\$noPostFlagArray[$noPostFlagArray_counter] = $noPostFlagArray[$noPostFlagArray_counter]\n";
$noPostFlagArray_counter++;
#print"in the module\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 vector 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 edges 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 edgesEnhanced 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 tabular 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 planned 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 map 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");
#}elsif ($_ =~ /<Metadata_Date/){
#$metalength=length($_);
#print SAVEOUT "\$metalength=$metalength \n";


#if  ( $metalength < 43){
#printf MIFFINAL "<Metadata_Date>$metadata_date</Metadata_Date>"
#printf MIFFINAL "$_";
#}else{
#printf MIFFINAL "$_";
#};
 
   }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";
};

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

#f (files do
#$noflag_array_counter
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";
}; 
#-------------------------------------------------------------------------------------------------------

$ReRunFile ="$errlogdir/$newdir/FailedFiles";

if ($newflaglength > 5 or $mal == 0){
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";

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

$slash_directory_nameca =$slash_directory_namec-1; 

#print SAVEOUT "\$slash_directory_nameca = $slash_directory_nameca\n";
#print SAVEOUT "\$slash_directory_name[$slash_directory_namec] = $slash_directory_name[$slash_directory_namec]\n";
#print SAVEOUT "\$slash_directory_name[$slash_directory_nameca] = $slash_directory_name[$slash_directory_nameca]\n";
#print SAVEOUT "\$slash_directory_namec = $slash_directory_namec\n";
#:qprint SAVEOUT "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
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++;
#print SAVEOUT "In!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($slash_directory_namec == 0){
#print SAVEOUT "FIRST!!!!!!!!!!!!!!!\n";
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";
};

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

#$passcounter
#$postarraycounter
if ($postarraycounter ==1 ){
$postarraycounter_file = "file";
$postarraycounter_version ="a FGDC version";
}else{
$postarraycounter_file = "files";
$postarraycounter_version ="FGDC 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 format 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 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";
};

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

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 $postarraycounter_file passed validation and had $postarraycounter_version made for posting on the Census Metadata Server:\n";
print LOG "\nThe following $postarraycounter_file passed validation and had $postarraycounter_version made for posting on the Census Metadata 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 $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++;
};
};
};


#open STDERR, ">Error";
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";

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


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

$sizeReRun = -s $ReRunFile;
print SAVEOUT "\$sizeReRun  = $sizeReRun"; 
print SAVEOUT " ";
if (-s $ReRunFile ==0){
system "rm $errlogdir/$newdir/FailedFiles";
print SAVEOUT "\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";
};

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

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




