#!/usr/bin/perl

#------------------------------------------------------------------------------
#This PERL script makes one big list for all the files found under the user supplied
#directory.
#Written by Matthew McCready
#--------------------------------------------------------------------------

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

use lib "/home/m/mccre004/File-Recurse-0.11/blib/lib";
use File::Recurse;


sub usage() {
        print "Usage: $0 [-directory=<directory>]\n";
        print "Example:perl $0 -directory=/usr/directory\n";
        print " where /usr/directory is the directory where the .xml files to";
        print " be listed are located.\n";

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

print "runing version $0\n";

#Infile = $gOptions{listfile};
$directory=$gOptions{directory};
chdir $directory;


opendir INDIR, $directory;

foreach $file (readdir INDIR){
$totalFileCounter++;
$dotLocation=index($file,".");
$preDot= substr($file,0,$dotLocation);
$postDot=substr($file,$dotLocation);
$newISO= $preDot . "_newISO" . $postDot;
$listFile="ListOfNGDAFiles.txt";
print "\$otherConstraintsCounter = $otherConstraintsCounter\n";
print "-----------------------------------------------------------\n";
print "\$file = $file\n";
#print "\$preDot = $preDot\n";
#print "\$postDot = $postDot\n";
#print "\$newISO = $newISO\n";

open INFILE, "<$file";
open OUTFILE,">$newISO";
open LISTFILE,">>$listFile";

$lineCounter=0;
$yescounter=0;

$descriptiveKeywordsCounter=0;
$CI_CitationCounter=0;
$CharacterStringCounter=0;
$titleCounter=0;
$dateTypeCounter=0;
$CI_DateCounter=0;
$dateCounter=0;
$otherCitationDetailsCounbter=0;
$CI_DateTypeCounter=0;
$codeListValue=0;
$gcodateCounter=0;
$tcCounter=0;
$elseEnd=0;
$otherConstraintsCounter=0;
$distliab=0;
$otherVar="no";

while (<INFILE>){
$line =$_;
#$lineNumber++;
print "\$line = $line";
#print "\$lineNumber = $lineNumber\n";
#---------------------alternate title
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif ($rmAltTitle eq "yes"){
$rmAltTitle ="no";
}elsif ($line =~ /<gmd:descriptiveKeywords>/){
 $descriptiveKeywordsCounter++;
 if ($descriptiveKeywordsCounter == 3){
 }else{
   print OUTFILE "$line";
  };#if ($descriptiveKeywordsCounter == 3){

}elsif($descriptiveKeywordsCounter == 3){

if ($line =~ /<gmd:keyword>/){

if ($file ne $filePrev){
$ngdaArray[$listCounter]=$file;
$listCounter++;
$filePrev=$file;
};

}elsif($line =~ /gmd:MD_SpatialRepresentationTypeCode/){
print OUTFILE "$line";
#}elsif($line =~ /boundary information/){
}elsif ($line =~ /<gmd:MD_Keywords>/){
#}elsif ($line =~ /Statistical Boundaries<\/gco:CharacterString>/){
}elsif ($line =~ /<gco:CharacterString/){

if ($CharacterStringCounter <5){
$CharacterStringCounter ++;
}else{
print OUTFILE "$line";
};


#}elsif($line =~ />2010-02-01<\/gco:Date>/){
#}elsif($line =~ /codeList="http:\/\/www.isotc211.org\/2005\/resources\/Codelist\/gmxCodelists.xml#CI_DateTypeCode/){
}elsif($line =~/#MD_CharacterSetCode/){
print OUTFILE "$line";


#-----------------------------------------------gmd:otherConstraints-------------------------------
}elsif ($otherVar eq "yes"){
print "\$line = $line(here is the line!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!)\n";
print "Here seems to be the problem!!!!!!!!!!!!!!!!!!!!!\n";
$otherVar="no";
print "reset \$otherVar to $otherVar\n";
#print OUTFILE "$line";
if ($line =~ /<\/gmd:MD_LegalConstraints>/){
print OUTFILE "$line";
};


$otherVar="no";


#elsif($line =~ /< <gmd:MD_RestrictionCode/){

}elsif($line =~ /<gmd:MD_RestrictionCode/){
print OUTFILE "$line";
}elsif($line =~ /The boundary information in the TIGER/){
print OUTFILE "$line";

#--------------------------------------gmd:CI_Citation----------------------------------------

}elsif ($line =~ /<gmd:CI_Citation>/){
if ($CI_CitationCounter <1){
#print OUTFILE "<!-- Removed <gmd:CI_Citation> -->\n";
$CI_CitationCounter++;
}else{
print OUTFILE "$line";
};

}elsif($line =~ /<\/gmd:CI_Citation>/){
if ($CI_CitationCounter == 1){
print OUTFILE  "<!-- Removed <\/gmd:CI_Citation> -->\n";
$CI_CitationCounter++;
}else{
print OUTFILE "$line";
};
#--------------------->eng</gco:CharacterString>
}elsif($line=~ /eng<\/gco:CharacterString>/){
print OUTFILE "$line";
}elsif($line =~ /Distribution/){
  #print OUTFILE "<gmd:otherConstraints>";
  print OUTFILE "$line";


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

}elsif ($line =~ /<\/gco:CharacterString>/){
  print OUTFILE "\n <!--- dealing with the Character String (\$CharacterStringCounter = $CharacterStringCounter   --> \n";
 if ($CharacterStringCounter <5){
  print OUTFILE "<!--  Removed <\/gco:CharacterString> (\$CharacterStringCounter = $CharacterStringCounter)  -->\n";
  if($line =~ /Distribution/){
   #print OUTFILE "<gmd:otherConstraints>";
   print OUTFILE "$line";
  };
 }elsif($line =~ /Distribution/){
 # print OUTFILE "<gmd:otherConstraints>";
  print OUTFILE "$line";
}elsif($line =~ /boundary information/){

 }else{
  # print OUTFILE "<!--- in the Characterstring else  --> \n"; 
  if($line =~ /Distribution/){
  #print OUTFILE "<gmd:otherConstraints>";
   print OUTFILE "$line";
 }elsif($line =~ /boundary information/){
 }else{
   print OUTFILE "$line";
  };
 };

#print OUTFILE "\n<!--- finished with with the Character String (\$CharacterStringCounter = $CharacterStringCounter   --> \n";

}elsif($line =~ /<\/gmd:keyword>/){
#print OUTFILE "<!--  Removed <\/gmd:keyword> -->\n";
}elsif($line =~ /<gmd:keyword/){
#print OUTFILE "<!--  Removed <gmd:keyword -->\n";
#-----------------------------type---------------------------------
}elsif($line =~ /<gmd:type>/){
#print OUTFILE "<!--  Removed <gmd:type> -->\n";
}elsif($line =~ /<\/gmd:type>/){
#print OUTFILE "<!--  Removed </gmd:type> -->\n";

}elsif($line =~ /<gmd:MD_KeywordTypeCode/){
#print OUTFILE "<!--  Removed <gmd:MD_KeywordTypeCode -->\n";
}elsif($line =~ /codeListValue="theme"/){
#print OUTFILE  "<!--  Removed codeListValue=\"theme\"  -->\n";
}elsif($line =~ /codeSpace=\"005\"> theme <\/gmd:MD_KeywordTypeCode>/){
#print OUTFILE "<!-- codeSpace=\"005\"> -->\n";
}elsif($line =~ / <\/gmd:type>/){
#---------------------------title ---------------------------
}elsif($line =~ /<gmd:title>/){

if($titleCounter <1){
#print OUTFILE  "<!-- Removed gmd:title> -->\n";
$titleCounter++;
}else{
print OUTFILE "$line";
};

}elsif ($line =~ /<\/gmd:title>/){

if($titleCounter ==1){
#print OUTFILE "<!-- Removed <\/gmd:title>  -->\n";
$titleCounter++;
}else{
print OUTFILE "$line";
};

#---------------------alternate title
}elsif($lineNumber==80){
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($lineNumber==127){
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($lineNumber==128){
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
#elsif($line =~ /gco:CharacterString/){
#f ($rmTitle eq "yes"){
#rmTitle="no";
#;
}elsif ($rmAltTitle eq "yes"){
$rmAltTitle ="no";
#----------------------------gmd:date-----------------------
}elsif($line =~ /<gmd:date>/){

if ($dateCounter <2){#was 2
#print OUTFILE "<!-- \$line = $line -->\n";
print OUTFILE  "<!-- Removed <gmd:date>(\$dateCounter=$dateCounter -->\n";
$dateCounter++;



$twicedateCounter=($dateCounter*2);
}else{
print OUTFILE "$line";
};


}elsif($line =~ /<\/gmd:date>/){
print "found the end date tag!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "\$twicedateCounter = $twicedateCounter\n";
print "\$elseEnd = $elseEnd \n";
print "\$enddateCounter =$enddateCounter\n";
print OUTFILE "<!-- \$elseEnd = $elseEnd -->\n";
print OUTFILE "<!-- \$enddateCounter = $enddateCounter -->\n";
if ($enddateCounter==5 ){
$enddateCounter++;
print OUTFILE "<!-- in the counter section \$enddateCounter = $enddateCounter-->\n";
#print "\$enddateCounter $enddateCounter \n";
#print "\$dateCounter = $dateCounter\n";
#print OUTFILE "<!-- ============================================================================================================== -->\n";

}else{

if ($elseEnd == 0){
print OUTFILE  "<!-- Removed <\/gmd:date>(\$elseEnd = $elseEnd) -->";
$elseEnd++;#this one
}elsif($elseEnd == 1){
#print OUTFILE "<!--\$elseEnd = $elseEnd (in the elsif!!!!!!!!!!!!!!!!!!!!!!!) -->";
#print OUTFILE  "<!-- Removed <\/gmd:date>(\$elseEnd = $elseEnd) -->";
$elseEnd++;#this one
}elsif($elseEnd == 2){
#$elseEnd++;#this one
#print OUTFILE "$line";
#------------------4444444444444444444444444444444444444444444444
}elsif($elseEnd == 4){
print OUTFILE "$line";
#}elsif($elseEnd == 2){
#print OUTFILE "<!--\$elseEnd = $elseEnd (in the elsif!!!!!!!!!!!!!!!!!!!!!!!) -->";
#print OUTFILE  "<!-- Removed <\/gmd:date>(\$elseEnd = $elseEnd) -->";
#$elseEnd++;#this one

}else{
#print OUTFILE "$line";
print OUTFILE "<!--\$elseEnd = $elseEnd (in the else!!!!!!!!!!!!!!!!!!!!!!!) -->";
print OUTFILE "$line";
#print "not removing the date!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
#$elseEnd++;
#print OUTFILE "<!--  ============================================================================================================== --> \n";
};
$elseEnd++
};

#---->2010-02-01</gco:Date>

#}elsif($line =~ /2010-02-01<\/gco:Date>/){
#remove
#---------------------------------CI_Date-------------------------------------------------------
}elsif($line =~ /www.isotc211.org/){
#print OUTFILE  "<!-- dealing with tc211!!!!!!!!!!!!!!!!!!! -->\n";
#print OUTFILE  "<!-- \$tcCounter = $tcCounter -->\n";
#print OUTFILE  "<!-- \$line = $line -->\n";

if ($tcCounter<=1){#was1 was2
 #print "\$tcCounter = $tcCounter\n";
 #print OUTFILE  "<!-- removed tc211.org \$tcCounter = $tcCounter -->\n";
 #print OUTFILE  "<!-- \$tcCounter = $tcCounter -->\n";
 $tcCounter++;
}else{
#print OUTFILE  "<!-- left alone !!!!!!!!!!!!!!!!!!  -->\n";
print OUTFILE "$line";
};


}elsif($line =~ /<gmd:CI_Date>/){
#print" dealing with <gmd:CI_Date> \n";
if ($CI_DateCounter <1){
#print OUTFILE  "<!-- Removed <gmd:CI_Date> -->\n";
$CI_DateCounter++;
}else{
print OUTFILE "$line";
};


}elsif($line =~ /<\/gmd:CI_Date>/){
if ($CI_DateCounter <= 1){
#print OUTFILE "\$CI_DateCounter = $CI_DateCounter\n";
#print OUTFILE  "<!-- Removed <\/gmd:CI_Date> -->\n";
$CI_DateCounter++;
}else{
print OUTFILE "$line";
};




#--------------------------------
}elsif($line =~ /<gco:Date/){
#print "dealing with <gco:Date!!!!!!!!!!!!!!\n";
#print "\$gcodateCounter = $gcodateCounter\n";
if ($gcodateCounter <3){
#print OUTFILE  "<!-- Removed <gco:Date -->\n";
$gcodateCounter++;
}else{
print OUTFILE "$line";
};


#-----------------------------------gmd:dateType---------------------
}elsif($line =~ /<gmd:dateType>/){

if ($dateTypeCounter<1){
#print OUTFILE  "<!-- Removed <gmd:dateType> -->\n";
$dateTypeCounter++;
}else{
print OUTFILE "$line";
};


}elsif($line =~ /<\/gmd:dateType>/){
#print OUTFILE  "<!-- Removed <\/gmd:dateType -->\n";

if ($dateTypeCounter==1){
#print OUTFILE  "<!-- Removed </gmd:dateType> -->\n";
$dateTypeCounter++;
#}elsif($dateTypeCounter==2){
#$dateTypeCounter++;
}else{
#print OUTFILE "<!-- kept $line (\$dateTypeCounter = $dateTypeCounter -->\n";
print OUTFILE "$line";
};


#----------------------CI_DateTypeCode------------------------------------
}elsif($line =~ /<gmd:CI_DateTypeCode/){

print "dealing with gmd:CI_DateTypeCode \n";
print "\$CI_DateTypeCounter = $CI_DateCounter\n";
if ($CI_DateTypeCounter ==0){
print OUTFILE  "<!-- Removed <gmd:CI_DateTypeCode  -->\n";
$CI_DateTypeCounter++;
}else{
print OUTFILE "$line";
$CI_DateTypeCounter++;
};

}elsif($line =~ /<\/gmd:CI_DateTypeCode>/){
print "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n\n";
print "in the gmd:CI_DateTypeCode\n";
print "\$CI_DateTypeCounter = $CI_DateTypeCounter\n";
print "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA\n\n";
if ($CI_DateTypeCounter ==0){
print OUTFILE  "<!-- Removed <\/gmd:CI_DateTypeCode> -->\n";
print "<!-- Removed <\/gmd:CI_DateTypeCode> -->\n";
$CI_DateTypeCounter++;
}else{
print OUTFILE "$line";
$CI_DateTypeCounter++;
};
#---------------------------------codeListValue="revision"--------------

}elsif($line =~ /codeListValue="revision"/){

if ($codeListValue==0){
#print OUTFILE  "<!-- Removed codeListValue=\"revision\"  -->\n";
$codeListValue++;
}else{
print OUTFILE "$line";
};


}elsif($line =~ /codeSpace=\"003\"> revision/){
#print OUTFILE  "<!-- Removed codeSpace=\"003\">  -->\n";


#-----------------------------CI_Date------------------------------------------------------------------------

}elsif($line =~ /<\/gmd:CI_Date>/){
#print OUTFILE  "<!-- Removed <\/gmd:CI_Date> -->\n";
}elsif($line =~ /<\/gmd:CI_Date>/){
#print OUTFILE  "<!-- Removed <\/gmd:CI_Date> -->\n";

#-------------------other citation details-------------------------------------------------------------------
}elsif($line =~ /<gmd:otherCitationDetails>/){

if ($otherCitationDetailsCounbter==0){
#print OUTFILE  "<!-- Removed <gmd:otherCitationDetails> -->\n";
$otherCitationDetailsCounbter++;
}else{
print OUTFILE "$line";
};

}elsif($line =~ / <\/gmd:otherCitationDetails>/){
if($otherCitationDetailsCounbter==1){
#print OUTFILE  "<!-- Removed <\/gmd:otherCitationDetails> -->\n";
$otherCitationDetailsCounbter++;
}else{
print OUTFILE "$line";
};


#------------------------------------------------------------------------
}elsif($line =~ /<\/gmd:CI_Citation>/){
#print OUTFILE  "<!-- Removed <\/gmd:CI_Citation> -->\n";
}elsif($line =~ /<\/gmd:thesaurusName>/){
#print OUTFILE  "<!-- Removed <\/gmd:thesaurusName> -->\n";
}elsif($line =~ /<gmd:thesaurusName>/){
#print OUTFILE  "<!-- Removed </gmd:thesaurusName> -->\n";
}elsif($line =~ /<\/gmd:MD_Keywords>/){
#print OUTFILE  "<!-- Removed <\/gmd:MD_Keywords> -->\n";
}elsif($line =~ /<\/gmd:descriptiveKeywords/){
#print OUTFILE  "<!-- Removed <\/gmd:MD_Keywords> -->\n";

}else{
print OUTFILE "$line";


};#f ($line =~ /<gmd:keyword>/){
}else{
print OUTFILE "$line";
};#if ($line =~ /<gmd:descriptiveKeywords>/){
};#while (<INFILE>){
#print "now moving the file !!!!!!!!!!!!!!!!!!!!!!\n";
system "mv $newISO $file";
};#foreach $file (readdir INDIR){a

#sort @ngdaArray;


print LISTFILE "The following files need to have their NGDA tags removed:\n";

foreach $ngdaArray (@ngdaArray){
$ngdaCounter++;
print LISTFILE "$ngdaCounter:  $ngdaArray\n";

};

print " A total of $totalFileCounter files have had their NGDA tags removed\n\n ";

