#!/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;

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: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>2010-02-01</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: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";
};

}else{ 
print OUTFILE "$_"
};#if ($_ =~ /gco:Date/){
};#while (<INFILE>){

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

};#foreach $file (readdir INDIR){
