#!/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: MRF2JSONSingle_Ver1  !!!!!!!!!!!!!!!!!!!!!!!!!!!\n";

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

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

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

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

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

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

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

if (-e $comboFile){

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

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

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


open INPUT, $comboFile;
#open JSON,   ">>GEOJSON.json";


$locXML=index($inputFileB,".xml");
$preMRF=substr($inputFileB,0,$locXML);
$MIF= $preMRF . ".mif.xml";
$err= $preMRF . ".mif.err";

open STDOUT,">$MIF";
open STDERR,">$err";
printf SAVEOUT " Please Wait, now transforming $comboFile to a MRF File ---------------------------------------------------------------------\n\n";

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

#----------------------------------------------updating the GIDs------------------------------------------------------------------------------------------------------------
if ($mif == 0){

open MIFFILE, "<$MIF";
system "rm $err";

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

$MIF2= "MIF2_" . $preMRF . ".mif.xml";
open MIFFINAL,">$MIF2";


$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 "$_";
};
};
}else{
print SAVEOUT "There is a problem with $comboFile\n";
};

#$MIF2

$MRF = $preMRF . ".mrf.xml";
$err= $preMRF . ".mrf.err";
open STDOUT,">$MRF";
open STDERR,">$err";
printf SAVEOUT " Please Wait, now validating $MIF2---------------------------------------------------------------------\n\n";

my @csax = "java com.saxonica.Transform -outval:recover $MIF2 $root/gpms/xsl/MIF2MRF.xsl";
my $final = system(@csax);

if ($final == 0){
#print SAVEOUT "$MIF2 is O.K\n";
system "rm $err";
system "rm $MIF2";
system "rm $MIF";


$JSONXML =$preMRF . ".json.xml";
$err= $preMRF . ".json.err";
open STDOUT,">$JSONXML";
open STDERR,">$err";
print SAVEOUT "Now turning $MRF into a JSON XML file------------------------------------------------------------\n\n";
my @csax = "java com.saxonica.Transform -outval:recover $MRF $root/gpms/mrf2PodVer1_1/MRF2PODVer1_1.xsl";
my $POD = system(@csax);


#------------------------------------------------Turning the JSON XML into a true JSON file------------------------------------------------------------------ 
if ($POD == 0){
system "rm $err";
system "rm $MRF";

open INFILE, "<$JSONXML";
open JSON,   ">>GEOJSON.json";

while (<INFILE>){

#print "\$_ = $_";
$line = $_;

if ($line =~/<?xml version="1.0" encoding="ISO-8859-1"?>/){
}elsif($line =~/encoding/){
#print "skipping <?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> 222222222222222222222222222222 \n";
}elsif($line =~/PODVer1_1/){

if ($started eq "yes"){
print JSON "},\n";
$started="no";
}else{
print JSON "{";
$started="yes";
};

#print JSON "\"accessLevel\": \"public\",";
#print JSON "\"bureauCode\": [ \"018:10\" ],";
}elsif($line =~/<type>/){
$righbracLoc=index($line,">")+1;
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-1;
$elText=substr($line,$righbracLoc,$length);
#print "\$elText = $elText\n";
#print OUTJSON "type: \"$elText\",";
#print OUTJSON "\"type\": \"$elText\",";
print JSON "\"\@type\": \"$elText\",";
#print OUTJSON "done!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";

#title--------------------------------------------------
}elsif($line =~/title/){
#print "in the title !!!!!!!!!!!!!!!!!!!!!!!\n";
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");

if ($line =~/TIGER/){
#print "dealing with TIGEr!!!!!!!!!!!!!!!!!!!!\n";
$slashLocA=index($line, "/");
$lengthA=$slashLocA-$righbracLoc+1;
$elTextA=substr($line,$righbracLoc+1,$lengthA-1);

$elTextB=substr($line,$slashLocA+1);
$slashLocB=index($elTextB, "/")-1;
$elTextC=substr($elTextB,0,$slashLocB);
$elText = $elTextA . $elTextC;

#print "dealing with TIGEr!!!!!!!!!!!!!!!!!!!!\n";
#print "\$elTextA = $elTextA (TIGER)\n";
#print "\$elTextB = $elTextB (TIGER)\n";
#print "\$elTextC = $elTextC(TIGER)\n";
}else{
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc;
$elText=substr($line,$righbracLoc+1,$length-2);
#print "\$elText = $elText (else)\n";
};

$elLength=$righbracLoc-$leftBracket-1;
$elementMain=substr($line,$leftBracket+1, $elLength);
#elLength=$righbracLoc-$leftBracket;
$newElement= "\"" . $elementMain .  "\":";
print JSON "$newElement \"$elText\",";

#keyword----------------------------------------------------------------------------------------------------
}elsif($line =~/keyword/){
#print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
if ($line =~/<keywordItem/){#keywordItem
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);
#print "inserting the keyword: $elText\n";
print JSON"\"$elText\",";
#elsif($line =~/\/keywordItem/){
#print "dealing with the end keywordItem tag \n";
#print JSON"],"
}elsif($line =~/\/keyword/){
print JSON "],";
#print "dealing with $line (end tag)\n"
}else{
print JSON "\"keyword\": [";
#print "dealing with $line (start  tag)\n";
};
#print "-----------------------------------------------------------------------\n";

#------------------------------------------------------description--------------------------------------------------------------------------------
}elsif($line =~/<description>/){
#print "dealing with the description!!!!!!!!!!!!!!!!!!!\n";
#print "\$line = $line\n";
$endTagLoc=index($line,"<\/");
#print "\$endTagLoc = $endTagLoc\n";

if ($endTagLoc <0){
$descComplete="no";
$lastbracLoc=index($line,">");
$descPart1 = substr($line,$lastbracLoc+1);

#print "\$descPart1 = $descPart1\n";
}else{
$lastbracLoc=index($line,">");
$descPart1 = substr($line,$lastbracLoc+1);
$firstBracLoc=index($descPart1,"<");
$finDesc=substr($descPart1,0,$firstBracLoc-1);
print JSON "\"description\": $finDesc,";
};

}elsif($descComplete eq "no"){
#print SAVEOUT "double Description----------\n";
#print "\$line = $line\n";
$lengthLine=length($line);
#print "\$lengthLine = $lengthLine\n";

if ($lengthLine>5){
$firstBracLoc=index($line,"<");
chomp($finDescA=substr($line,0,$firstBracLoc-1));
chomp($descPart1);
$descPart1=~s/\r\n//;
chomp($finDescA);

$quoteLoc=index($finDescA,"\"");
#print SAVEOUT "\$quoteLoc = $quoteLoc\n";

if ($quoteLoc <0){
$finDesc = $finDescA
}else{
$preQuote=substr($finDescA,0,$quoteLoc-1);
$postQuote1=substr( $finDescA,$quoteLoc+1);
$quoteLoc2=index($postQuote1,"\"");
$midLength=$quoteLoc2-$quoteLoc;
$middleQuote=substr($finDescA, $quoteLoc+1, $quoteLoc2);
$lastQuote=substr($postQuote1,$quoteLoc2+1);

#print SAVEOUT "\$preQuote = $preQuote\n\n";
#print SAVEOUT "\$middleQuote = $middleQuote\n\n";
#print SAVEOUT "\$lastQuote = $lastQuote\n\n";

$finDesc = $preQuote . $middleQuote . $lastQuote;
};


#chomp($completeDesc=$descPart1 . $finDesc);
#chomp($completeDesc=join $descPart1,$finDesc);
$completeDesc=join $descPart1,$finDesc;
#$completeDesc=$descPart1;
print JSON "\"description\":\" $completeDesc\",";
$descComplete="yes";
}elsif($line =~/<\/publisher>/){
#print OUTJSON "},";

foreach $subOrgArray(@subOrgArray){
print JSON "$subOrgArray";
};

print JSON "},";
#-----------------------------------------------------------------publisher------------------------------------------------------------------------------------
}elsif($line =~/<publisher>/){
print JSON "\"publisher\":{";
}elsif($line =~/name/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);
print JSON "\"name\": \"$elText\",";
#subOrganization----------------------------------------------------
}elsif($line =~/subOrganizationOf/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);
$elLength=$righbracLoc-$leftBracket-1;
$elementMain=substr($line,$leftBracket+1, $elLength);
#elLength=$righbracLoc-$leftBracket;
$newElement= "\"" . $elementMain .  "\":{";
$subOrgName="\"name\":";
print JSON "$newElement $subOrgName \"$elText\",";

$subOrgArray[$subOrgArrayCounter]="}";
$subOrgArrayCounter++;

#-language--------------------------------------------------------------------------------
}elsif($line =~/<language>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);

$elLength=$righbracLoc-$leftBracket-1;
$elementMain=substr($line,$leftBracket+1, $elLength);

#elLength=$righbracLoc-$leftBracket;
$newElement= "\"" . $elementMain .  "\":";
print JSON "$newElement [\"$elText\"],";

# -ContactPoint---------------------------------------------------------------------------
}elsif($line =~/<\/contactPoint>/){
print JSON "},";
}elsif($line =~/<contactPoint>/){
print JSON "\"contactPoint\": {"
}elsif($line =~/<fn>/){
$firstBrac=index($line,">");
$lastBarac=rindex($line,"<");
$length=$lastBarac-$firstBrac-1;

$newFN=substr($line,$firstBrac+1,$length);
#print "\$firstBrac = $firstBrac\n";
#print "\$lastBarac = $lastBarac\n";
#print "\$newFN = $newFN\n";
print JSON "\"fn\":\"$newFN\",";
}elsif($line =~/<hasEmail>/){
$firstBrac=index($line,">");
$lastBarac=rindex($line,"<");
$length=$lastBarac-$firstBrac-1;
$newEmail=substr($line,$firstBrac+1,$length);
print JSON "\"hasEmail\":\"$newEmail\"";

#temporal---------------------------------------------------------------------
}elsif($line =~/<begin>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$begin=substr($line,$righbracLoc+1,$length);
}elsif($line =~/<end>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$end=substr($line,$righbracLoc+1,$length);
}elsif($line =~/<\/temporal>/){
print JSON "\"$begin/$end\",";

}elsif($line =~/<temporal>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);

$elLength=$righbracLoc-$leftBracket-1;
$elementMain=substr($line,$leftBracket+1, $elLength);

#elLength=$righbracLoc-$leftBracket;
$newElement= "\"" . $elementMain .  "\":";
#print OUTJSON "$newElement \"$elText\",";
print JSON "$newElement";
#print "temporal!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($line =~/<modified>/){
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$newMod=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"modified\":\"$newMod\",";
#publisher--------------------------------------------------------------------
}elsif($line =~/<\/publisher>/){
#print OUTJSON "},";

foreach $subOrgArray(@subOrgArray){
print JSON "$subOrgArray";
};

print JSON "},";

}elsif($line =~/<publisher>/){
print JSON "\"publisher\":{";
}elsif($line =~/name/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);
print JSON "\"name\": \"$elText\",";
#subOrganization----------------------------------------------------
}elsif($line =~/subOrganizationOf/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$elText=substr($line,$righbracLoc+1,$length);
$elLength=$righbracLoc-$leftBracket-1;
$elementMain=substr($line,$leftBracket+1, $elLength);
#elLength=$righbracLoc-$leftBracket;
$newElement= "\"" . $elementMain .  "\":{";
$subOrgName="\"name\":";
print JSON "$newElement $subOrgName \"$elText\",";

$subOrgArray[$subOrgArrayCounter]="}";
$subOrgArrayCounter++;
}elsif($line =~/<identifier>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$newID=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"identifier\":\"$newID\",";
}elsif($line =~/<accessLevel>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$newID=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"accessLevel\":\"$newID\",";
}elsif($line =~/<bureauCode>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$newBureau=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"bureauCode\":\"$newBureau\",";
}elsif($line =~/<programCode>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$newProgram=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"programCode\":\"$newBureau\",";
#accrualPerdicity
}elsif($line =~/<accrualPeriodicity>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$Period=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"accrualPeriodicity\":\"$Period\",";
#spatial
}elsif($line =~/<spatial>/){
print JSON "\"spatial\":\"{";

#pairs------------------------------------------------------------------------------
}elsif($line =~/<pair1>/){

}elsif($line =~/<\/pair1>/){
print JSON "[[[$westBC,$northBC],";
}elsif($line =~/<pair2>/){
#skip
}elsif($line =~/<\/pair2>/){
print JSON "[$eastBC,$southBC],";
}elsif($line =~/<pair3>/){
#skip
}elsif($line =~/<\/pair3>/){
print JSON "[$eastBC,$northBC],";
}elsif($line =~/<pair4>/){
#skip
}elsif($line =~/<\/pair4>/){
print JSON "[$westBC,$southBC],";
}elsif($line =~/ <pair5>/){

}elsif($line =~/ <\/pair5>/){
print JSON "[$westBC,$northBC]]]}\",";
#end of pairs---------------------------------------------------------------------
}elsif($line=~/<Polygon>/){
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$spatialType=substr($line,$righbracLoc+1,$linelength-1);
#print "\$spatialType = $spatialType\n";
print JSON "\\\"type\\\":\\\"$spatialType\\\",";
}elsif($line=~/<coordinates>/){
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$coordinates=substr($line,$righbracLoc+1,$linelength-1);
#print "\$spatialType = $spatialType\n";
print JSON "\\\"$coordinates\\\":"
}elsif($line =~/<west>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$westBC=substr($line,$righbracLoc+1,$length);
}elsif($line =~/<north>/){
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$northBC=substr($line,$righbracLoc+1,$length);
}elsif($line =~/<south>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$southBC=substr($line,$righbracLoc+1,$length);
}elsif($line =~/<east>/){
$leftBracket=index($line,"<");
$righbracLoc=index($line,">");
$slashLoc=index($line, "/");
$length=$slashLoc-$righbracLoc-2;
$eastBC=substr($line,$righbracLoc+1,$length);
#print "\$eastBC = $eastBC\n";
#accrualPerdicity
#distribution
}elsif($line =~/<distribution>/){
print JSON "\"distribution\":[{";
}elsif($line =~/<downloadURL>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$URL=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"downloadURL\": \"$URL\",";
}elsif($line =~/<mediaType>/){#default
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$mediaType=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"mediaType\": \"$mediaType\",";
}elsif($line =~/<isPartOf>/){
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$isPartOf=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"isPartOf\": \"$isPartOf\",";

}elsif($line =~/<distTitle>/){
$righbracLoc=index($line,">");
$leftbracloc= rindex($line,"<");
$linelength=$leftbracloc-$righbracLoc;
$distTile=substr($line,$righbracLoc+1,$linelength-1);
print JSON "\"title\": \"$distTile\"}],";
#print JSON "\n";
};#if ($line =~/<?xml version="1.0" encoding="ISO-8859-1"?>/){
};#while (<INFILE>){
};#if ($POD == 0){




















};#$final == 0



}else{#$final == 0)(MIF2MRF.xsl)

};#$mif == 0



system "rm $JSONXML";
#my @csax = "java com.saxonica.Transform -outval:recover $MIF $root/gpms/mrf2PodVer1_1/MRF2PODVer1_1.xsl";
#my $final = system(@csax);




