#!/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 -directory=/dir/subdir -file=something.xml\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(directory=s  file=s));
unless ( $gOptions{directory} and $gOptions{file}) {
        usage();
        exit(1);
};

 
print "This program is working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

#chomp($inputFile=$gOptions{file});
#---------------------------------------------------------------constansts----------------------------------------------------------------------
$root = "/mt/apps";
#          /mtdata/geo/shpgen/tigerline/fe_2007/
$rootout="/mtdata/geo";

#--------------------------------------------------------------dealing with the directory-------------------------------------------------------------
chomp($directory=$gOptions{directory});
print "\$directory = $directory\n";

#---------------------------------------------------------dealing with the input file----------------------------------------------------------------
chomp($inputFile=$gOptions{file});
print "\$inputFile = $inputFile\n";

if ($inputFile =~ /\//){
$lastSlash=rindex($inputFile,"/");
$inputFileB=substr($inputFile,$lastSlash);
}else{
$inputFileB = $inputFile;
};

print "\$inputFileB = $inputFileB\n";

#-----------------------------------------------combining the files----------------------------------------------------------------------------------
$comboFile= $directory . "/" . $inputFileB;

print "\$comboFile = $comboFile\n";
#-----------------------------------------------Notes Section------------------------------------------------------------------------------------------


#-determing the file type

$dotPosition=rindex($inputFile,".");
$fileType = substr($inputFile,$dotPosition+1,3);
print "\$fileType = $fileType\n";
if($fileType eq "xml"){
print "determing the profile!!!!!!!!!!!!!!!!!\n";
if ($inputFile =~ /edges/){
$profile = "e";
}elsif($inputFile =~ /dbf/){
$profile = "t";
}else{
$profile = "v";
};
};

print "\$profile = $profile\n";

#-----------------------------------redirecting the output---------------------------------------------------------------------------------------------------
open (SAVEOUT,">&STDOUT");
#----------------------------------------------------------Saxon-----------------------------------------------------------------------------------------
$ENV{'PATH'} = "/apps/saxon/jdk1.5.0_24/bin::/apps/saxon/saxon9ee.jar::/apps/saxon:$rootout/mrf/$newerrdir:$ENV{'PATH'}";
$cp = "CLASSPATH";#goodone
$ENV{$cp} = "/apps/saxon/jdk1.5.0_24/lib/:/apps/saxon/saxon9ee.jar:/apps/saxon/saxon9ee.jar $CLASSPATH:/usr/share/java/saxon.jar";

#---------------------------------------------------------------------------------------------------------------------------
#------------------------------subroutine section--------------------------------------------------
sub mif_is_zero{
$mif=$_[0];
if ($mif==0){
system "rm $unordered.err";
system "rm $newmifFile";
}else{
print SAVEOUT "in the else of the subroutine!!!!!!!!!!!!!!!!!!!!!!\n";
$badDir =$directory  . "/". "badFile";
system "mkdir $badDir";
system "mv $newmifFile  $badDir";
system "mv $unorderedError $badDir";
exit(2);
};
};

sub mif_copy{
#if (-e $mifFile){
$newmifFile =$comboFile . "-bck";
system "cp $comboFile $newmifFile";

#}else{
#};
};
#----------------------------------------------------------transforming the files from FGDC 2 mif-----------------------------------------------------------------------------
if ($profile eq "e"){
&mif_copy;

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

print SAVEOUT "\$mif = $mif\n";

&mif_is_zero($mif);

close STDOUT;
close STDERR;
}elsif($profile eq "t"){
&mif_copy;
$unordered = "MIF_" . $inputFileB;
$unorderedError= $unordered . ".err";

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

&mif_is_zero($mif);

}elsif($profile eq "v"){
&mif_copy;
$unordered = "MIF_" . $inputFileB;
$unorderedError= $unordered . ".err";
open STDOUT,">$unordered";
open STDERR, ">$unorderedError";
printf SAVEOUT "$countingtotal: Please wait, now converting $comboFile  from the FGDC format to the MRF vector format \n";
$countingtotal++;
my @csax = "java com.saxonica.Transform -outval:recover $comboFile  $root/gpms/FGDC2MRF/FGDC2mif_vector.xsl";#was -vw
my $mif = system(@csax);
&mif_is_zero($mif);
print SAVEOUT "\$mif = $mif\n";
};

#----------------------------------------------updating the GIDs------------------------------------------------------------------------------------------------------------
open MIFFILE, "<$unordered";
$MIF = "MIF2_" . $unordered;
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;
$unrepCounter=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="/){

if ($unrepCounter <1){
   $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");
printf MIFFINAL ("$finalstring");
printf MIFFINAL "<!-- first unrepresentable_Domain -->";
$unrepCounter=0;
};


   }elsif($finalstring =~ /<Unrepresentable_Domain gid1="/){

printf MIFFINAL ("<!-- before the  first Unrepresentable_Domain -->\n");
if ($unrepCounter <1){
$domcounter++;

   $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");
 printf MIFFINAL ("<!-- first Unrepresentable_Domain -->");
$unrepCounter=0;
};



    }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="/){
 printf MIFFINAL ("<!-- before the  second Unrepresentable_Domain -->\n");
 if ($unrepCounter <1){
$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");
 #printf MIFFINAL ("$finalstring");
   printf MIFFINAL ("<!-- second Unrepresentable_Domain -->\n");
$unrepCounter=0;
};
}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 "$_";
};
};

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

print SAVEOUT "\$final = $final\n";

if ($final == 0){
print SAVEOUT "in the final!!!!!!!!!!!!!!!! ($MIF)\n";
system "rm $err";
#unlink $mifarray or warn "could not unlink $mifarray $!";
#print SAVEOUT "\$! =  $!\n";
system "rm $mifarray";

#$passed[$passcounter]=$MRF;
#$passcounter++;
$LastDotLoc=rindex($MIF,".");
$MainMRF=substr($MIF,0,$LastDotLoc);
$MRF=$MainMRF . ".mrf.xml";
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>
#----------------------------------------------------------------------------------transforming the file from MRF to FGDC---------------------------------------------------------------------------------------------------------------
print SAVEOUT " Now transforming $MRF to the FGDC output format\n";
#$fgdcoutcounter++;

$mrfLocation=index($MRF,".mrf")-9;
print SAVEOUT "\$mrfLocation = $mrfLocation\n";
$mainFGDC=substr($MRF,9,$mrfLocation);
$FGDC = $mainFGDC . ".xml";
open STDOUT,">$FGDC";
open STDERR,">$FGDC.err";

my @csax = "java com.saxonica.Transform -outval:recover $MRF $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);

if ($fgdcout == 0){
system "rm $FGDC.err";

};
#------------ 
#else{
#final not Zero
#};



#--------------------------------------------------------------------------transforming the file into ISO 19115-2 -------------------------------------------------------------------------------
print SAVEOUT "Now transforming $MRF to the ISO 19115-2 output format\n";
$fgdcoutcounter++;
#$lastDot=rindex($FGDC,".");
#$newFGDC=substr($FGDC,0,$lastDot);
$iso_var=  $newFGDC . ".iso.xml";;

$iso_var= $mainFGDC . ".iso.xml";
open STDOUT,">$iso_var";
open STDERR, ">$iso_var.err";

@csax = "java com.saxonica.Transform -outval:recover $MRF  $root/gpms/mrf2ISO/mrf2ISOMaster.xsl";
my $ISOmif = system(@csax);

#print SAVEOUT "\$ISOmif $ISOmif \n";
#print SAVEOUT "\$ISOmif = $ISOmif\n";
if ($ISOmif == 0){
system "rm $iso_var.err";
};





#------------------------------------------------------------transforming the file into ISO 19110--------------------------------------------------------------------------------------------------------

print SAVEOUT "Now transforming $MRF to the ISO 19110 (EA) output format\n";
$iso_var_ea= $mainFGDC . ".ea.iso.xml";
$iso_var_ea_error = $iso_var_ea . ".err";

open STDOUT,">$iso_var_ea";
open STDERR, ">$iso_var_ea_error";
my @csaxea = "java com.saxonica.Transform -outval:recover $MRF /mt/apps/gpms/mrf2ea/mrf2eamaster.xsl";#Sec52iso.xslt
my $mifea = system(@csaxea);

if ($mifea == 0){
system "rm $iso_var_ea_error";
};

system "mv $FGDC $directory";
system "mv $iso_var $directory";
system "mv  $iso_var_ea $directory";


#system "rm $MRF"; 
#system "rm $unordered";
#system "rm $MIF";
#system "rm $TMP";

}else{
print SAVEOUT "final not Zero\n";
};

