#!/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>\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_MRFVer5\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="/mtdata001";

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

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

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

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


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

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


}elsif ($computer eq "/mtdata003"){
print "003";
$workDirectory= $workDirectoryLoc . "/work/gpms";
$badDirectory = $workDirectoryLoc . "/work/gpms/log";
$otherComputer="yes";
if (-e $workDirectory){
}else{
system "mkdir -p $workDirectory";
system "chmod 777 $workDirectory";
};
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";

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

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


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


};
#----------------------------------------------------------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;
$unorderedError = $workDirectory . "/" . $unordered . ".err";
}else{
$unordered = "MIF_" . $inputFileB;
$unorderedError = $unordered . ".err";
};

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 "\$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++;
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_copy1;

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

#$unordered = "MIF_" . $inputFileB;
#$unorderedError = $unordered . ".err";
#print SAVEOUT "\$unordered = $unordered\n";
#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 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);


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


#print SAVEOUT "updating the GIDS!!!!!!!!!!!!!!!!\n";

if ($otherComputer eq "yes"){
$MIF = $workDirectory . "/" . "MIF2_" . $inputFileB;
#print SAVEOUT "\$MIF = $MIF\n";
#print SAVEOUT "\$unordered = $unordered\n";
}else{
$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-----------------------------------------------------------------------------------------
close MIFFINAL;


if ($otherComputer eq "yes"){
$TMP=$workDirectory . "/" . "TMP_" ."MIF2_" . $inputFileB;
#print SAVEOUT "\$TMP = $TMP\n";
$err=$newSubDir . "/" . "TMP_" . "MIF2_" .$inputFileB . ".err";

#print "\$inputFileB = $inputFileB\n";
#print "\$err = $err\n";
}else{
$TMP="TMP_" . $MIF;
$err="TMP_" . $MIF . ".err";
};

#system "chmod 777 $MRF ";

#if ($? == -1){
#print SAVEOUT "problem with $MRF\n";
#};

#system "chmod 777 $MIF";
#system "chmod 777 $err";

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


if (-e $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";
#}else{
#$final=2;
#};


if ($final == 0){
#print SAVEOUT "in the final!!!!!!!!!!!!!!!! ($TMP)\n";
print SAVEOUT "\nNow creating the FGDC, the ISO 19115-2 and the Enity and Attribute Files!!!!!!\n";

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


#unlink $mifarray or warn "could not unlink $mifarray $!";
#print SAVEOUT "\$! =  $!\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";
};


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>

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";
#$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,0,$mrfLocation);
$mainFGDC3=substr($mainFGDC2,5);
$FGDC =$workDirectory . "/" . $mainFGDC3 . ".xml";
$FGDCError =$workDirectory . "/" . $mainFGDC3 . ".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 . ".err";
};



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

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

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


if ($fgdcout == 0){
system "rm $FGDCError";
}else{
system "mv $FGDC.err $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,"/")+1;
$newFGDCA=substr($FGDC,$lastSlash);
$lastDot=rindex($newFGDCA,".");
$newFGDC=substr($newFGDCA,0,$lastDot);

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

#print SAVEOUT "\n\n\n";
#print SAVEOUT "\$lastSlash = $lastSlash\n";
#print SAVEOUT "\$FGDC = $FGDC\n";
#print SAVEOUT "\$newFGDCA = $newFGDCA\n";
#print SAVEOUT "\$newFGDC = $newFGDC\n";
#print SAVEOUT "\$iso_var = $iso_var\n";
#print SAVEOUT "\$iso_Var_Error = $iso_Var_Error\n";
#print SAVEOUT "\n\n\n";



}else{
$lastDot=rindex($FGDC,".");
$newFGDC=substr($FGDC,0,$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";
exit(1);
};





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

print SAVEOUT "Now transforming $MRF to the ISO 19110 (EA) output format\n";
if ($otherComputer eq "yes"){
$lastDot=rindex($newFGDC,".");
$newFGDCEA = substr($newFGDC,0,$lastDot);
$iso_var_ea=$workDirectory . "/" . $newFGDCEA . ".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";
if ($? == -1){
 print SAVEOUT "problem with rm $unordered (rm #10)\n"; 
}else{
  #print SAVEOUT "NO problem with rm $MIF (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 ";

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

exit(2);

}else{

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

