#!/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 Cwd;
use File::Basename;

sub usage() {
    print "Usage: perl $correctname [-listfile=<listfile>][-directory=<directory>\n";
    print "Example:\n";
    print "perl $correctname -directory=directory -file=filename.xml \n";
    print "where -directory is the directory containing the files and\n";
    print "filename.xml is the name of the file to bevalidated and transformed.\n";
};

%gOptions;
&GetOptions(\%gOptions, qw(directory=s  file=s));
unless ($gOptions{directory} and $gOptions{file}) {
        usage();
        exit(1);
};
 
print "This program is working!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "version: FGDC2ISOSingle_TVBatch_MRFVer11 try1!!!!!!!!!!!!!!!!!!!!!!!!\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";




#-------------------------------------------------------------getting the computer and creating the work files--------------------------------------
#$computer= substr($directory,0,10);
$computerA=substr($directory,1);
$firstSlash=index($computerA,"/")+1;
$computer= substr($directory,0,$firstSlash);


$workDirectoryLoc="/mtdata003";#was001
$seriesInfoDirectory="/mtdata001/geo/gpms/output/ISOParent";

print "\$computer = $computer\n";

if ($computer eq "/mtdata001"){
#print "001";
$workDirectory= $workDirectoryLoc . "/work/gpms";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
#$seriesInfoDirectory="mtdata001/work/gpms/log/SeriesInfo";
$otherComputer="yes";

if (-e $workDirectory){
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
#system "mkdir -p $seriesInfoDirectory or $!";
#system "chmod 777 $seriesInfoDirectory" or die "$!";

};

if (-e $badDirectory){
}else{
system "mkdir -p $badDirectory";
system "chmod 777 $badDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";

};


}elsif ($computer eq "/mtdata002"){
print "002";
$workDirectory= $workDirectoryLoc . "/work/gpms";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
#$seriesInfoDirectory=$workDirectoryLoc . "/work/gpms/log/SeriesInfo";
$otherComputer="yes";
if (-e $workDirectory){
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";

};

if (-e $badDirectory){
}else{
system "mkdir -p $badDirectory";
system "chmod 777 $badDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";

};


}elsif ($computer eq "/mtdata003"){
print "003";
$workDirectory= $workDirectoryLoc . "/work/gpms";
#$seriesInfoDirectory=$workDirectoryLoc . "/work/gpms/log/SeriesInfo";
#print "\$seriesInfoDirectory = $seriesInfoDirectory\n";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
$otherComputer="yes";
if (-e $workDirectory){
system "chmod 777 $workDirectory";
system "mkdir -p $workDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";
};
if (-e $badDirectory){
}else{
system "mkdir -p $badDirectory";
system "chmod 777 $badDirectory";
};


}elsif ($computer eq "/testmtdata003"){
print "003";
print "\$directory = $directory\n";
$workDirectory= $workDirectoryLoc . "/work/gpms";
#$seriesInfoDirectory=$workDirectoryLoc . "/work/gpms/log/SeriesInfo";
#print "\$seriesInfoDirectory = $seriesInfoDirectory\n";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
$otherComputer="yes";
if (-e $workDirectory){
system "chmod 777 $workDirectory";
system "mkdir -p $workDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
#system "chmod 777 $seriesInfoDirectory";
#system "mkdir -p $seriesInfoDirectory";
};
if (-e $badDirectory){
}else{
system "mkdir -p $badDirectory";
system "chmod 777 $badDirectory";
};


#/home

}elsif ($computer eq "/home"){
$otherComputer="no";

}else{
$otherComputer="yes";
print "in the defalut!!!!!!!!!!!!!!\n";
#badDirectory ="/mtdata001/geo/gpms/log"

$workDirectory= $workDirectoryLoc . "/work/gpms";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
$otherComputer="yes";
if (-e $workDirectory){
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
#print SAVEOUT "\$? = $?\n";
if ($? == -1){
print "caonnot make $workDirectory $!";
exit(2);
}elsif ($? == 256){
print "cannot make $workDirectory $!\n\n";
exit(2);
}else{

};






};

print "Making the error directory!!!!!!!!!!!!!!!!!!!! ($badDirectory)\n";
if (-e $badDirectory){
}else{
system "mkdir $badDirectory";
system "chmod 777 $badDirectory";
};



};



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

if (-e $comboFile){
print "$comboFile exists!!!!!!!!!!!!!!!!!!\n";
}else{
print "The file $comboFile could not be found!!!!!!!!!!!!!!!\n";
exit(1)
}


#-----------------------------------------------------------------------------------------------------------------------------------------------------
#$badDirectory ="/mtdata001/geo/gpms/log";
#------------------------------------------------making the error directory based on the file---------------------------------------------------------
$firstDotComboFile=index($comboFile,".");
$lastSlash=rindex($comboFile,"/")+1;

$length=$firstDotComboFile-$lastSlash;

$subDir=substr($comboFile,$lastSlash,$length);
#print "\$subDir = $subDir\n";



$newSubDir= $badDirectory . "/" . $subDir;
#system "mkdir -p $newSubDir";
#system "chmod 777 $newSubDir";
#print "\$newSubDir =  $newSubDir\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{
#print SAVEOUT "In the subroutine!!!!!!!!!!!!!!!!!!!!!!\n";
$mif=$_[0];
#print SAVEOUT "\$mif = $mif\n";
if ($mif==0){
#system "rm $unorderedError";

if (-e $unorderedError){
system "rm $unorderedError";
if ($? == -1){
  print SAVEOUT "problem with rm $unorderedError (rm #1)\n";
 }else{
 #print SAVEOUT "NO problem with rm $unorderedError (rm #1)\n";
};
};

if (-e $backup){
system "rm $backup";
if ($? == -1){
  print SAVEOUT "problem with rm $backup (rm #2)\n";
 }else{
  #print SAVEOUT "NO problem with rm $backup (rm #2)\n";
};

};



}else{
print SAVEOUT "in the default!!!!!!!!!!!!!!!!!!!        yesssssssssssssssssssssssssssssssssssssss\n";
#badDirectory = $directory . "/" . "badFile";
#$badDirectory= "/mtdata001/geo/gpms/ErrorFiles";
#$badDirectory="/mtdata003/geo/shpgen/tigerline/fe_2014/ErrorFiles";
#mkdir $badDirectory;

#mkdir $newSubDir, 0777 or warn "Cannot make $newSubDir $1\n";

&makeNewSub;
system "chmod 777 $unorderedError";
if ($? == -1){
   print SAVEOUT "problem with chmod $unorderedError  (chmod #1)\n";
  }else{
#   print SAVEOUT "No problem with chmod $unorderedError (chmod #1)\n";
  };

system "chmod 777 $backup";
if ($? == -1){
   print SAVEOUT "problem with chmod $backup (chmod #2)\n";
}else{
#   print SAVEOUT "No problem with chmod $backup (chmod #2)\n";
};

system "mv $unorderedError $newSubDir";
if ($? == -1){
  print SAVEOUT "problem with mv $unorderedError to $newSubDir (mv #1)\n";
}else{
  print SAVEOUT "NO problem with mv $unorderedError to $newSubDir (mv #1)\n";
};

system "mv $backup $newSubDir";
if ($? == -1){
  print SAVEOUT "problem with mv $backup to $newSubDir (mv #2)\n";
}else{
  print SAVEOUT "NO problem with mv $backup to $newSubDir mv #2)\n";
};

print SAVEOUT "\nErrors have been detected in $inputFile. Please see the error files in $newSubDir!\n\n";
exit(2);
};

};

sub mif_copy{
if (-e $mifFile){
system "cp $mifFile $newmifFile";

#print SAVEOUT "\$? = $?\n\n\n\n";
if ($? == -1){
  print SAVEOUT "problem with cp $mifFile to $newmifFile (cp #1)\n";
  exit (2);
}else{
  #print SAVEOUT "NO problem with $mifFile to $newmifFile (cp #1)\n";
};

}else{
};
};

sub mif_copy1 {
$comboSlashLoc=rindex($comboFile, "/")+1;
$rawFile=substr($comboFile,$comboSlashLoc); 
$backup = $workDirectory . "/" . $rawFile ."_bck";
#print "\$backup = $backup\n";
system "cp $comboFile $backup";
#print SAVEOUT "\$? = $?\n\n\n\n";

if ($? == -1){
  print SAVEOUT "problem with cp $comboFile to $backup (cp #1)\n";
  exit (2);
}elsif ($? == 256){
print SAVEOUT "problem with cp $comboFile to $backup (cp #1)\n";
  exit (2);


}else{
  #print SAVEOUT "NO problem with $comboFile to $backup (cp #1)\n";
};


};

#print "\$otherComputer = $otherComputer\n";

sub makeNewSub{
print SAVEOUT "making the subdirectory: $newSubDir!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";
system "mkdir -p $newSubDir" or warn $!;
system "chmod 777 $newSubDir"
};

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


if ($otherComputer eq "yes"){
$unordered = $workDirectory . "/" . "MIF_" . $inputFileB;
$unorderedError = $workDirectory . "/" . $unordered . ".err";
}else{
$unordered = "MIF_" . $inputFileB;
$unorderedError = $unordered . ".err";
};


#print SAVEOUT "\$unordered = $unordered\n";
#print SAVEOUT "\$unorderedError = $unorderedError\n";

open STDOUT,">$unordered";
open STDERR, ">$unorderedError";

if ($otherComputer eq "yes"){
$unordered = $workDirectory . "/" . "MIF_" . $inputFileB;
system "chmod 777 $unordered";
$unorderedError = $workDirectory . "/" . $unordered . ".err";
}else{
$unordered = "MIF_" . $inputFileB;
$unorderedError = $unordered . ".err";
};

print SAVEOUT"======================================================================\n";
print SAVEOUT "\$unordered = $unordered\n";
print SAVEOUT "\$unorderedError = $unorderedError\n";
print SAVEOUT"======================================================================\n";


$xsltChenck = $root . "/gpms/FGDC2MRF/FGDC2mif_edges.xsl";
if(-e $xsltChenck){
if (-e $comboFile){
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);
}else{
print SAVEOUT "$comboFile!!!!!!!!!!!!!!!!!!!\n";
};
}else{
print SAVEOUT "$root/gpms/FGDC2MRF/FGDC2mif_edges.xsl can not be found!!!!!!!!!!!!!!!!!!!\n";
};


print SAVEOUT "\$mif = $mif \n";
&mif_is_zero($mif);

#close STDOUT;
#close STDERR;
}elsif($profile eq "t"){
&mif_copy1;

#unordered = "MIF_" . $inputFileB;
#unorderedError = $unordered . ".err";

if ($otherComputer eq "yes"){
$unordered = $workDirectory . "/" . "MIF_" . $inputFileB;
$unorderedError = $workDirectory . "/" . $unordered . ".err";
}else{
$unordered = "MIF_" . $inputFileB;
$unorderedError = $unordered . ".err";
};


#print SAVEOUT "\$unorderedError = $unorderedError\n";

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

#&mif_is_zero($mif);

}elsif($profile eq "v"){
&mif_copy1;

if ($otherComputer eq "yes"){
#print SAVEOUT "\$otherComputer = $otherComputer\n";
$unordered = $workDirectory . "/" . "MIF_" . $inputFileB;
#$unorderedError = $workDirectory . "/" . $unordered . ".err";
$unorderedError=$unordered . ".err";
#system "chmod 777 $unordered";
}else{
$unordered = "MIF_" . $inputFileB;
$unorderedError = $unordered . ".err";
};

#$unordered = "MIF_" . $inputFileB;
#$unorderedError = $unordered . ".err";
print SAVEOUT"======================================================================\n";
print SAVEOUT "\$unordered = $unordered\n";
print SAVEOUT "\$unorderedError = $unorderedError\n";
print SAVEOUT"======================================================================\n";



open STDOUT,">$unordered";
open STDERR, ">$unorderedError";

#print SAVEOUT"======================================================================\n";
#print SAVEOUT "\$unordered = $unordered\n";
#print SAVEOUT"======================================================================\n";

$xsltChenck = $root . "/gpms/FGDC2MRF/FGDC2mif_vector.xsl";
if(-e $xsltChenck){
if (-e $comboFile){
printf SAVEOUT "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);
print SAVEOUT "\$mif = $mif\n";
}else{
print SAVEOUT "$comboFile!!!!!!!!!!!!!!!!!!!\n";
};
}else{
print SAVEOUT "$root/gpms/FGDC2MRF/FGDC2mif_vector.xsl can not be found!!!!!!!!!!!!!!!!!!!\n";
};


system "chmod 777 $unordered";

print SAVEOUT "\n\n\n";
print SAVEOUT "\$mif = $mif\n";
&mif_is_zero($mif);
print SAVEOUT "done with the mif_is_zero subroutine\n";
print SAVEOUT "\n\n\n";


};

#close STDOUT;
#close STDERR;

#----------------------------------------------updating the GIDs------------------------------------------------------------------------------------------------------------
open MIFFILE, "<$unordered";
if ($? == -1){
print SAVEOUT "problem opening $unordered\n";
};

#$workDirectory . "/" .
print SAVEOUT "updating the GIDS!!!!!!!!!!!!!!!!\n";
print SAVEOUT "\$otherComputer = $otherComputer\n";

#if ($otherComputer eq "yes"){
#$MIF = $workDirectory . "/" . "MIF2_" . $inputFileB;
$MIF=$workDirectory . "/" . "MIF2_" . $inputFileB;
print SAVEOUT "\$MIF = $MIF  other\n";
print SAVEOUT "\$unordered = $unordered\n";
#}else{
#$MIF =$workDirectory . "/" . "MIF2_" . $unordered;
#print SAVEOUT "\$MIF = $MIF  else\n";
#print SAVEOUT "\$unordered = $unordered\n";

#};

system "chmod 777 $MIF";
print SAVEOUT "\$MIF = $MIF\n";
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 "$_";
};
#close MIFFILE;
#close MIFFINAL; 
};
close MIFFILE;
close MIFFINAL;

#---------------------------------------------------------------------------------------validating the file and transforming itinot MRF-----------------------------------------------------------------------------------------
close MIFFINAL;


if ($otherComputer eq "yes"){
$TMP =$TMP_ . "MIF2_" . $inputFileB;
$err=$newSubDir . "/" . "TMP_" . "MIF2_" .$inputFileB . ".err";

print SAVEOUT "\$err = $err\n";
}else{
$TMP="TMP_" . $MIF;
$err="TMP_" . $MIF . ".err";
$err=$newSubDir . "/" . "TMP_" . "MIF2_" .$inputFileB . ".err";
};

print SAVEOUT "checking to see if $MIFexists!!!!!!!!!!!!!!!!!!!!!!!!!!\n\n";

if (-e $MIF){
system "chmod 777 $MIF";
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\n\n\n\n";

if ($final > 0){
open SAMPLEERR, $err;
while (<SAMPLEERR>){
#print SAVEOUT "$_\n";
};
};

if ($final == 0){
close $TMP;
print SAVEOUT "in the final!!!!!!!!!!!!!!!!\n ($TMP)\n";
print SAVEOUT "\n\nNow creating the FGDC, the ISO 19115-2 and the Enity and Attribute Files from $TMP!!!!!!\n\n (Here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)\n";
#system "rmdir $newSubDir";
if (-e $err){
system "rm $err";
if ($? == -1){
  print SAVEOUT "problem with rm $err (rm #3)\n";
 }else{
  #print SAVEOUT "NO problem with rm $err (rm #3)\n";
};
};



if (-e $mifarray){
system "rm $mifarray";
if ($? == -1){
  print SAVEOUT "problem with rm $mifarray (rm #4)\n";
 }else{
  #print SAVEOUT "NO problem with rm $mifarray (rm #4)\n";
};
};

#$passed[$passcounter]=$MRF;
#$passcounter++;
$LastDotLoc=rindex($MIF,".");
$MainMRF=substr($MIF,0,$LastDotLoc);

if ($otherComputer eq "yes"){
$MRF=$MainMRF . ".mrf.xml";
#print SAVEOUT "\$MainMRF = $MainMRF\n";
#print SAVEOUT "\$MRF =     $MRF\n";
}else{
$MRF=$MainMRF . ".mrf.xml";
};

print SAVEOUT "\$TMP = $TMP\n";
print SAVEOUT "\$directory = $directory\n";
$current_Dir=cwd;
$newTMP= $current_Dir . "/" . $TMP;
system "chmod 777 $newTMP";
print SAVEOUT "\$newTMP = $newTMP\n";

if (-e $TMP){
if (-e $directory){
#system "cp $newTMP  $directory" or warn $!, $?;
};
};

open MRFOUTPUT, ">$MRF";
system "chmod 777 $TMP";
#ystem "chmod 777 $unordered";
open TMPFILE, "<$TMP" or warn $!;

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>

close MRFOUTPUT;
#chmod 0777, "$MRF";
#system "chmod 777 $MRF ";
#system "chmod 777 $MRF";
#----------------------------------------------------------------------------------transforming the file from MRF to FGDC---------------------------------------------------------------------------------------------------------------
print SAVEOUT "Now transforming $MRF to the FGDC output format\n";
system "chmod 777 $MRF";
#$fgdcoutcounter++;

#mrfLocation=index($MRF,".mrf")-9;
#print SAVEOUT "\$mrfLocation = $mrfLocation\n";
$mainFGDC=substr($MRF,9,$mrfLocation);


#if ($otherComputer eq "yes"){
#$TMP=$workDirectory . "/" . "TMP_" . $MIF;
#$err=$workDirectory . "/" . "TMP_" . $MIF . ".err";
#}else{
#$TMP="TMP_" . $MIF;
#$err="TMP_" . $MIF . ".err";
#};


if ($otherComputer eq "yes"){
$lastSlash =rindex($MRF, "/")+1;
$mainFGDC=substr($MRF,$lastSlash);
$mrfLocation=index($mainFGDC,".mrf");
$mainFGDC2=substr($mainFGDC,5,$mrfLocation-5);
$mainFGDC3=substr($mainFGDC2,1);
$FGDC =$workDirectory . "/" . $mainFGDC2 . ".xml";
$FGDCError =$workDirectory . "/" . $mainFGDC2 . ".fgdc.err";

print SAVEOUT "\n\n\n";
print SAVEOUT "\$MRF = $MRF\n";
print SAVEOUT "\$mainFGDC = $mainFGDC\n";
print SAVEOUT "\$mainFGDC2 = $mainFGDC2\n";
print SAVEOUT "\$mainFGDC3 = $mainFGDC3\n";
print SAVEOUT "\$FGDC = $FGDC\n";
print SAVEOUT "\n\n\n";

}else{
$mrfLocation=index($MRF,".mrf")-9;
$mainFGDC=substr($MRF,9,$mrfLocation);
$FGDC = $mainFGDC . ".xml";
$FGDCError = $mainFGDC . ".fgdc.err";
};



#$FGDC = $mainFGDC . ".xml";
open STDOUT,">$FGDC";
open STDERR,">$FGDCError";

if (-e $MRF){
#print SAVEOUT "now transforming $MRF to an FGDC file!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
my @csax = "java com.saxonica.Transform -outval:recover $MRF $root/gpms/xsl/xsl4mrf2fgdc/MRF2FGDC.xsl";
my $fgdcout = system(@csax);
#print  SAVEOUT "\$fgdcout = $fgdcout\n\n\n\n";
}else{
print SAVEOUT "$MRF could not be found\n\n";
exit(2);
};

#print  SAVEOUT "\$fgdcout = $fgdcout\n\n\n\n";
system "chmod 777 $FGDC";
if ($? == -1){
   print SAVEOUT "problem with chmod $FGDC (chmod #3)\n";
  }else{
   #print SAVEOUT "No problem with chmod $FGDC (chmod #3)\n";
  };

 
###############mrf
#print SAVEOUT "\nBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n";
#print SAVEOUT "\$workDirectory = $workDirectory\n";
#system "cp $MRF $workDirectory";
#system "cp $TMP workDirectory";
#print SAVEOUT "BBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBBB\n";
###########


if ($fgdcout == 0){
system "rm $FGDCError";
}else{
system "chmod 777 $FGDCError";
print SAVOUT "\$FGDCError = $FGDCError\n";
&makeNewSub;
system "mv $FGDCError $newSubDir";
print SAVEOUT "Problem with transforming from a MRF File to a FGDC file\n\n";
exit(1);  
};
#------------ 
#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);

if ($otherComputer eq "yes"){
$lastSlash=rindex($FGDC,"/");
$newFGDCA=substr($FGDC,$lastSlash);
$lastDot=rindex($newFGDCA,".");
$newFGDC=substr($newFGDCA,1,$lastDot);

$iso_var= $workDirectory . "/" .  $newFGDC . "iso.xml";
$iso_Var_Error =  $workDirectory . "/" .  $newFGDC . "iso.err";

}else{
$lastDot=rindex($FGDC,".");
$newFGDC=substr($FGDC,2,$lastDot);
$iso_var=  $newFGDC . ".iso.xml";
$iso_Var_Error =  $newFGDC . ".iso.err";
};

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

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

system "chmod 777 $iso_var";
if ($? == -1){
   print SAVEOUT "problem with chmod $iso_var (chmod #4)\n";
  }else{
   #print SAVEOUT "No problem with chmod $iso_var (chmod #4)\n";
  };


#print SAVEOUT "\$ISOmif $ISOmif \n";
#print SAVEOUT "\$ISOmif = $ISOmif\n";
if ($ISOmif == 0){
#print SAVEOUT "removing the  iso error file\n";

system "rm $iso_Var_Error";
if ($? == -1){
  print SAVEOUT "problem with rm $iso_Var_Error (rm #5)\n";
 }else{
  #print SAVEOUT "NO problem with rm $iso_var.err (rm #5)\n";
};

}else{
system "mv $iso_Var_Error $newSubDir";
print SAVEOUT "Problem with transforming from a MRF File to an ISO file\n\n\n";
exit(1);
};

#---------------------------------------------------------transforming the file into the Series Collection File-----------------------------------------------------------------------------------------

$firstUndeLoc=index($newFGDC,'_');
$SeriesFileType=substr($newFGDC,1,$firstUndeLoc-1);

$postFirstUnderScore=substr($newFGDC,$firstUndeLoc+1);
#print SAVEOUT"\$postFirstUnderScore = $postFirstUnderScore\n";

$locSecondUnderScore=index($postFirstUnderScore,"_");
$year=substr($postFirstUnderScore,0,$locSecondUnderScore);

$lastUnderLoc=rindex($newFGDC,'_');
$seriesTheme=substr($newFGDC,$lastUnderLoc);

#$seriesInfoDirectory
$iso_var_Series=$seriesInfoDirectory . "/" . "SeriesCollection_"  . $SeriesFileType . "_" . $year . $seriesTheme . ".iso.xml";
$iso_var_Series_Error=$seriesInfoDirectory . "/" . "SeriesCollection_"  . $SeriesFileType . "_" . $year . $seriesTheme . ".err";

#print SAVEOUT "\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n";
#print SAVEOUT "\$iso_var_Series = $iso_var_Series\n";
#print SAVEOUT "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n";

if (-e $iso_var_Series){
$SeriesInfoMade='no';
}else{
print SAVEOUT "Making $iso_var_Series!!!!!!!!!!!!!!!!!!!!!!!!\n";

open STDOUT,">$iso_var_Series";
open STDERR, ">$iso_var_Series_Error";

print SAVEOUT "Now transforming $MRF into ISO Series Information File-----------------\n";
my @csaxsi = "java com.saxonica.Transform -outval:recover $MRF  /mt/apps/gpms/mrf2ISOParent/MRF2ISOParent.xsl";
my $mifsi = system(@csaxsi);

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

if ($mifsi == 0){
system "rm $iso_var_Series_Error";
system "chmod 777 $iso_var_Series";
$SeriesInfoMade='yes';
}else{
system "mv $iso_var_Series $newSubDir";
print SAVEOUT "Problem with creating an ISO Series Information File\n";
};

};

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

print SAVEOUT "Now transforming $MRF to the ISO 19110 (EA) output format\n";
if ($otherComputer eq "yes"){
#$lastDot=index($newFGDC,".");
#$newFGDCEA = substr($newFGDC,0,$lastDot);
#print SAVEOUT "\$newFGDC = $newFGDC\n";
#print SAVEOUT "\$newFGDCEA = $newFGDCEA\n"; 

#$iso_var_ea=$workDirectory . "/" . $newFGDCEA . ".ea.iso.xml";
$iso_var_ea=$workDirectory . "/" . $newFGDC . "ea.iso.xml";
$iso_var_ea_error =$workDirectory . "/" .  $iso_var_ea . ".err";
}else{
$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";
if ($? == -1){
  print SAVEOUT "problem with rm $iso_var_ea_error (rm #6)\n";
 }else{
  #print SAVEOUT "NO problem with rm $iso_var.err (rm #6)\n";
};
}else{
system "mv $iso_var_ea_error $newSubDir";
print SAVEOUT "Problem with transforming from a MRF File to an ISO 19110 n(Entity and Attribute  file";
exit(1);

};

system "chmod 777 $iso_var_ea";
if ($? == -1){
   print SAVEOUT "problem with chmod $iso_var_ea (chmod #5)\n";
  }else{
   #print SAVEOUT "No problem with chmod $iso_var_ea (chmod #5)\n";
  };


#print SAVEOUT "\$FGDC = $FGDC\n";
if (-e $FGDC){
#$workDirectoryLoc
print SAVEOUT "Now moving $FGDC to $directory\n\n";
system "mv -f $FGDC $directory";
#print SAVEOUT "\$? = $?\n";
if ($? == -1){
  print SAVEOUT "problem with mv $FGDC to $directory (mv #3)\n";
}elsif ($? == 256){
print SAVEOUT "problem with mv $FGDC to $directory (mv #3) ($!)\n";
 }else{
  #print SAVEOUT "NO problem with mv $FGDC to $directory (mv #3)\n";
 };

};

if (-e $iso_var){
system "mv $iso_var $directory";
if ($? == -1){
  print SAVEOUT "problem with mv $iso_var to $directory (mv #4)\n";
 }else{
  #print SAVEOUT "NO problem with mv $iso_var to $directory (mv #4)\n";
 };

};

if (-e $iso_var_ea){
system "mv  $iso_var_ea $directory";
if ($? == -1){
  print SAVEOUT "problem with mv $iso_var_ea to $directory  (mv #5)\n";
 }else{
  #print SAVEOUT "NO problem with mv $iso_var to $directory (mv #5)\n";
 };

};

#print SAVEOUT "\$MRF = $MRF\n";

if (-e $MRF){
system "rm $MRF"; 
if ($? == -1){
  print SAVEOUT "problem with rm $MRF (rm #7)\n";
 }else{
  #print SAVEOUT "NO problem with rm $MRF (rm #7)\n";
 };

};

if (-e $unordered){
system "rm $unordered";
if ($? == -1){
  print SAVEOUT "problem with rm $unordered (rm #8)\n";
 }else{
  #print SAVEOUT "NO problem with rm $unordered (rm #8)\n";
 };

};

if (-e $MIF){
system "rm $MIF";
if ($? == -1){
  print SAVEOUT "problem with rm $MIF (rm #9)\n";
 }else{
  #print SAVEOUT "NO problem with rm $MIF (rm #9)\n";
 };

};

if (-e $TMP){
system "rm $TMP";
system "rm $directory/$TMP";
if ($? == -1){
 print SAVEOUT "problem with rm $TMP (rm #10)\n"; 

}else{
  #print SAVEOUT "NO problem with rm $TMP (rm #10)\n";
 };

};

print SAVEOUT "\nThe $inputFile has been sucessfully transformed into a  FGDC output file, an ISO 19915-2 file and an Entity and Attribute File.\n\n ";

if ($SeriesInfoMade eq 'yes'){
print SAVEOUT "\nThe $inputFile has also had a Series Information File made for it in the $seriesInfoDirectory directory.\n\n";
};

}elsif($final==2){
print SAVEOUT "$MIF cannot be found";

exit(2);

}else{

&makeNewSub;
#$badDirectory = $directory . "/" . "badFile";
#badDirectory = "/mtdata001/geo/gpms/ErrorFiles";
#$badDirectory="/mtdata003/geo/shpgen/tigerline/fe_2014/ErrorFiles";
print SAVEOUT "\nErrors have been detected in $inputFile. Please see the error files in $newSubDir!\n\n";
#print STDERR "Errors have been detected in $inputFile. Please see the error files in $badDirectory!\n";
#mkdir $newSubDir;
system "mkdir -p $newSubDir";
system "chmod 777 $newSubDir";
#---------------------------------chmod $mif -----------------------------------------------------------
system "chmod 777 $MIF";

if ($? == -1){
print SAVEOUT "problem with chmod $MIF (chmod #6)\n";
}else{
#print SAVEOUT "No problem with chmod $MIF (chmod #6)\n";
};

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

#----------------------see if the error file exists----------------------------------------------------------
if (-e $err){

 #system "chmod 777 $err";
 if ($? == -1){
   print SAVEOUT "problem with chmod $err (chmod #7)\n";
  }else{
 #  print SAVEOUT "No problem with chmod $err\n";
  };

# system "mv $err $newSubDir";
 if ($? == -1){
  print SAVEOUT "problem with mv $err to  $newSubDir  (mv #6)\n";
 }else{
  #print SAVEOUT "NO problem with mv $err to $badDirectory (mv #6)\n";
 };
 
#system "mv $MIF $newSubDir";
if ($? == -1){
  print SAVEOUT "problem with mv $MIF to  $badDirectory (mv #7)\n";
 }else{
  #print SAVEOUT "NO problem with mv $MIF to $badDirectory (mv #7)\n";
 };


 };

#------------------------------------------------------------------------------------------------------------------------------
#-----------------------see if the original (input MIF) exists----------------------------------------------------------------
if (-e $unordered){

system "chmod 777 $unordered";
 if ($? == -1){
   print SAVEOUT "problem with chmod $unordered (chmod #8)\n";
  }else{
  # print SAVEOUT "No problem with chmod $unordered (chmod #8)\n";
  };

system "mv $unordered $newSubDir";
if ($? == -1){
  print SAVEOUT "problem with mv $unordered to $newSubDir (chmod #9)\n";
 }else{
  #print SAVEOUT "NO problem with mv $unordered to $badDirectory (chmod #9)\n";
 };

};

#_________________________________________________________________________________________________________________________
#----------------------------see if the $TMP file exist-------------------------------------------------------------------
if (-e $TMP){

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

if ($sizeTMP >0){
#print SAVEOUT "In the if!!!!!!!!!!!!!!!\n";
system "chmod 777 $TMP";
if ($? == -1){
   print SAVEOUT "problem with chmod $TMP (chmod #10)\n";
  }else{
  # print SAVEOUT "No problem with chmod $TMP (chmod #10) \n";
  };

#print SAVEOUT  "moving $TMP to $newSubDir\n";
system "mv $TMP $newSubDir";
if ($? == -1){
  print SAVEOUT "problem with mv $TMP to $newSubDir (mv #8)\n";
 }else{
 # print SAVEOUT "NO problem with mv $TMP to $newSubDir (mv #8)\n";
 };
}else{

system "rm $TMP";
if ($? == -1){
  print SAVEOUT "problem with rm $TMP (rm #11)\n";
 }else{
  #print SAVEOUT "NO problem with rm $TMP (rm #11)\n";
 };


};


};

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

exit(2);
};


#}else{
#print SAVEOUT "\nThe file could not be found!!!!!!!!!!!!!!!!!!\n\n";
#$final=2;
};

