#!/usr/bin/perl

use Getopt::Long;
use File::Copy;

$correctname=substr($0,0);

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 [-directory=<directory>-theme=<theme>]\n";
    print "Example:\n";
    print "perl $correctname -directory=directory -theme=theme \n";
       print "theme is one of the following shortcut theme words:
boundaries: Governmental Units, and Administrative and Statistical Boundaries
cultural: Cultural Resources
transportation: Transportation
governmental units: Governmental Units, and Administrative and Statistical Boundaries


\n";

};
%gOptions;
&GetOptions(\%gOptions, qw(theme=s directory=s) );
unless ($gOptions{theme} and $gOptions{directory} ) {
        usage();
        exit(1);
};


chomp($NGDATheme=$gOptions{theme});
chomp($InDir =$gOptions{directory});

#print "\$NGDATheme =  $NGDATheme\n";
#print "\$InDir = $InDir\n";


$outDir = "/mtdata001/geo/gpms/output/ISO";

if ($NGDATheme eq  "boundaries"){
$correctNGDATheme = "Governmental Units, and Administrative and Statistical Boundaries";
}elsif($NGDATheme eq  "Boundaries"){
$correctNGDATheme = "Governmental Units, and Administrative and Statistical Boundaries";
}elsif($NGDATheme eq  "cultural"){
$correctNGDATheme = "Cultural Resources";
}elsif($NGDATheme eq  "Cultural"){
$correctNGDATheme = "Cultural Resources";
}elsif($NGDATheme eq  "Transportation"){ 
$correctNGDATheme = "Transportation";
}elsif($NGDATheme eq  "transportation"){
$correctNGDATheme = "Transportation";
}elsif($NGDATheme eq  "governmental units"){
$correctNGDATheme = "Governmental Units, and Administrative and Statistical Boundaries";
};


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


#------------------------------------getting the date---------------------------------------------------------------------------------------------------------

my $now=`date`;
$mon = substr($now,4,3);
$day=substr($now,8,2);
$year =substr($now,24,4);
$hour = substr($now,11,2);
$min = substr($now,14,2);
my $daynum= dayNumbeFormat($day);
my $monName=monName($mon);
my $monnum =monNumber($mon);

if ($day<10){
$singleDay= substr($day,1,1);
$dayNum="0" . $singleDay;
}else{
$dayNum=$day;
};


$dateString = $year . "-" . $monnum . "-" . $dayNum;

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


opendir INDIR, $InDir or die "Cannot open $InDir $!";

foreach $file (readdir INDIR){
#print "\$file = $file\n";

$dotPos=rindex($file,".");
$firstDot=index($file,".");
$dashPos=rindex($file,"_");
$length=$firstDot-$dashPos-1;
#print "\$dotPos = $dotPos\n";
#print "\$dashPos = $dashPos\n";
#print "\$length = $length\n";

$ext=substr($file,$dotPos+1,3);

$gcoDateCounter=0;
$themeCounter=0;
$TemporalExtentCounter=0;

if ($ext eq "xml"){
$fileTheme=substr($file,$dashPos+1,$length);
$finalOutDir = $outDir . "/" . $fileTheme;
$outFile= $finalOutDir . "/" . $file;
$inFile =  $InDir  . "/" . $file;


system "mkdir $finalOutDir";
open INFILE, "<$inFile";
open OUTFILE, ">$outFile ";

print "\$ext = $ext\n";
print "\$fileTheme = $fileTheme\n";
print "\$finalOutDir =  $finalOutDir\n";
print "\$outFile = $outFile\n";
print "\$inFile = $inFile\n";

while (<INFILE>){

if ($_ =~ /gco:Date/){

 if ($gcoDateCounter <1 ){
 print OUTFILE "<gco:Date xmlns:gco=\"http://www.isotc211.org/2005/gco\">$dateString </gco:Date>";
 $gcoDateCounter++;
 }else{
 print OUTFILE "$_"
 };#if ($gcoDateCounter <1 ){

}elsif($_ =~ /<gmd:name\/>/){
 print OUTFILE "<gmd:name>";
 print OUTFILE "<gmd:MD_MediumNameCode codeList=\"http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_MediumNameCode\"\n";
 print OUTFILE "codeListValue=\"dvdRom\"\n "; 
 print OUTFILE "codeSpace=\"003\"> dvdRom </gmd:MD_MediumNameCode>";
 print OUTFILE "</gmd:name>";
 print OUTFILE "<gmd:mediumFormat>";
 print OUTFILE "<gmd:MD_MediumFormatCode codeList=\"http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_MediumFormatCode\"";
 print OUTFILE " codeListValue=\"iso9660\"";
 print OUTFILE " codeSpace=\"004\">";
 print OUTFILE "iso9660";
 print OUTFILE " </gmd:MD_MediumFormatCode>";
  print OUTFILE "</gmd:mediumFormat>";

}elsif($_ =~ /\<\/gmd:descriptiveKeywords>/){
print "\$themeCounter  = $themeCounter\n";

 if ($themeCounter<1){
  print OUTFILE "$_";
  $themeCounter++;
  print "\$themeCounter  = $themeCounter\n";
  print "------------------------------------------------\n";
 }else{
  print OUTFILE "$_";
  print OUTFILE "<gmd:descriptiveKeywords>";
  print OUTFILE "<gmd:MD_Keywords>";
  print OUTFILE " <gmd:keyword>";
  $GcoString = " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\"> " . $correctNGDATheme . "</gco:CharacterString>";
  print OUTFILE "$GcoString";
  print OUTFILE "</gmd:keyword>";

  print OUTFILE "<gmd:type>";
  print OUTFILE "<gmd:MD_KeywordTypeCode\n";
  print OUTFILE "codeList=\"http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_KeywordTypeCode\" \n";
  print OUTFILE "codeListValue=\"theme\" codeSpace=\"005\"> theme </gmd:MD_KeywordTypeCode> \n";
   print OUTFILE "</gmd:type> \n";
 
  print OUTFILE "<gmd:thesaurusName>\n";
  print OUTFILE "<gmd:CI_Citation>\n";
 
 print OUTFILE "<gmd:title>\n";
  print OUTFILE " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">NGDA Portfolio Themes</gco:CharacterString>\n";
  print OUTFILE " </gmd:title>\n";

  print OUTFILE " <gmd:date>\n";
  print OUTFILE "<gmd:CI_Date>\n";

  print OUTFILE "<gmd:date>\n";
  print OUTFILE "<gco:Date xmlns:gco=\"http://www.isotc211.org/2005/gco\">2010-02-01</gco:Date>";
  print OUTFILE "</gmd:date>\n";

  print OUTFILE "<gmd:dateType>\n";
  print OUTFILE "<gmd:CI_DateTypeCode\n";
  print OUTFILE "codeList=\"http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode\" \n";
  print OUTFILE "codeListValue=\"revision\" codeSpace=\"003\"> revision \n";
  print OUTFILE " </gmd:CI_DateTypeCode> \n";
  print OUTFILE "</gmd:dateType> \n";

 # print OUTFILE "<gmd:otherCitationDetails> \n";
 # print OUTFILE " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\" \n";
 # print OUTFILE ">http://www.fgdc.gov/initiatives/resources/2013-2-1-ngda-data-themes-fgdc-sc-revised.pdf</gco:CharacterString> \n";
 # print OUTFILE " </gmd:otherCitationDetails> \n";
  print OUTFILE "</gmd:CI_Date> \n";
  print OUTFILE "</gmd:date> \n";

 print OUTFILE "<gmd:otherCitationDetails> \n";
  print OUTFILE " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\" \n";
  print OUTFILE ">http://www.fgdc.gov/initiatives/resources/2013-2-1-ngda-data-themes-fgdc-sc-revised.pdf</gco:CharacterString> \n";
  print OUTFILE " </gmd:otherCitationDetails> \n";

  print OUTFILE "</gmd:CI_Citation> \n";
  print OUTFILE " </gmd:thesaurusName> \n";
# print OUTFILE "</gmd:type> \n";
  print OUTFILE "</gmd:MD_Keywords> \n";
  print OUTFILE " </gmd:descriptiveKeywords> \n";

};

}elsif ($_ =~ /codeList=\"http:\/\/www.tc211.org\/ISO19139\/resources\/codeList.xml#MD_MaintenanceFrequencyCode\"/){

if ($_ =~ /gmd:MD_MaintenanceFrequencyCode/){
 print OUTFILE "<gmd:MD_MaintenanceFrequencyCode codeList=\"http:\/\/www.isotc211.org\/2005\/resources\/Codelist\/gmxCodelists.xml#MD_MaintenanceFrequencyCode\"\n"; 
}else{
 print OUTFILE "codeList=\"http:\/\/www.isotc211.org\/2005\/resources\/Codelist\/gmxCodelists.xml#MD_MaintenanceFrequencyCode\"\n";
};

}elsif($_ =~/gmd:EX_TemporalExtent/){

if ($TemporalExtentCounter<1){
print OUTFILE "<gmd:EX_TemporalExtent id=\"boundingTemporalExtent\">\n";
$TemporalExtentCounter++;
#elsif($_ =~/"publication "/){
#qLoc=index($_,
}else{
print OUTFILE "$_";
};

}elsif($_ =~/unpublished material/){
print OUTFILE "<gmd:date xmlns:gco=\"http://www.isotc211.org/2005/gco\" gco:nilReason=\"withheld\"/>\n";
}elsif($_ =~/"publication "/){
$qLoc=index($_,"\"");
$firstPart=substr($_,0,$qLoc);
$newString= $firstPart . "\"publication\"";
print OUTFILE "$newString\n"; 
}elsif($_ =~/"dataset "/){
$qLoc=index($_,"\"");
$firstPart=substr($_,0,$qLoc);
$newString= $firstPart . "\"dataset\">";
#print OUTFILE "<!-- replacing the dataset -->";

print OUTFILE "$newString\n";
}elsif($_ =~/.\/resources\/codeList.xml#CI_DateTypeCode/){
$qLoc=index($_,"\"")+1;
$lastqLoc=rindex($_,"#");
$firstPart=substr($_,0,$qLoc);

$codeValue=index($_,"codeListValue");

if ($codeValue >0){
$lastPartA=substr($_,$lastqLoc);


$lastqLocPartA=rindex($_,"\"");
#$lastPart=substr($lastPartA,$lastqLocPartA);
$lastPart=substr($lastPartA,$lastqLoc);
$newString= $firstPart . "http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode\"" . "  codeListValue=\"publication\"" . $lastPart;
print OUTFILE "<!-- replacing the DateTypeCode (if) -->";
print OUTFILE "$newString\n";
}else{
#print OUTFILE "<!-- replacing the DateTypeCode (else) -->";
#print OUTFILE "<!-- \$_ = $_  -->";
#print OUTFILE "<!-- \$firstPart = $firstPart -->";
$newString= $firstPart . "http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#CI_DateTypeCode\"" . $lastPart;
print OUTFILE "$newString\n";
#print OUTFILE "<!-- done !!!!!!!!!!! -->";
};
}elsif($_ =~/http:\/\/www.isotc211.org\/2005\/#MD_ScopeCode/){
$qLoc=index($_,"\"")+1;
$lastqLoc=rindex($_,"#");
$firstPart=substr($_,0,$qLoc);
$lastqLoc=rindex($_,"#")+13;
$lastPart=substr($_,$lastqLoc);
$newString= $firstPart . "http://www.isotc211.org/2005/resources/Codelist/gmxCodelists.xml#MD_ScopeCode" . $lastPart;
print OUTFILE "$newString\n";
}elsif($_ =~/sepereating/){
$sepLoc=index($_,"sep")-1;
$preSep=substr($_,0,$sepLoc);
$newString=$preSep . " separating the stylesheets into sections. ";
print OUTFILE "$newString\n";
}elsif($_ =~/deliveried/){
$preDeLoc=index($_,"del");
$preDe=substr($_,0,$preDeLoc);
$postIedLoc=index($_,"ied")+3;
$postIed=substr($_,$postIedLoc);
$newString = $preDe . "delivered" . $postIed;
print OUTFILE "$newString\n";
}elsif($_ =~/Catolog/){
$preCatLoc=index($_,"Cat")-1;
$preCat=substr($_,0,$preCatLoc);
$postLogLoc=index($_,"log")+3;
$postLog=substr($_,$postLogLoc);
$newString= $preCat . " catalog" . $postLog;
print OUTFILE "$newString\n";
}elsif($_ =~/descriptions.Coordinates/){
$preonLoc=index($_,"s.C");
$partOne=substr($_,0,$preonLoc+1);
$partTwo=substr($_,$preonLoc+2);
$newString=$partOne . ". " . $partTwo;
print OUTFILE "$newString\n";
}elsif($_ =~/pointofContact/){
$pointLoc=index($_,"pointofContact")-1;
$postLoc=index($_,"act");
$prePoint=substr($_,0,$pointLoc);
$postPoint=substr($_,$postLoc+3);
$newString=$prePoint . "\"pointOfContact" . $postPoint;
#print OUTFILE "<!-- \$prePoint=$prePoint -->\n";
#print OUTFILE "<!-- \$postPoint = $postPoint-->\n";
print OUTFILE "$newString\n";
}else{ 
print OUTFILE "$_"
};#if ($_ =~ /gco:Date/){
};#while (<INFILE>){

};#if ($ext eq "xml"){

};#foreach $file (readdir INDIR){
