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

if ($line =~ /<gmd:descriptiveKeywords>/){
 $descriptiveKeywordsCounter++;
 if ($descriptiveKeywordsCounter == 3){
   #print OUTFILE "<!-- Removed <gmd:descriptiveKeywords> -->\n";
   #$listCounter++;
   #print LISTFILE "$listCounter: $file\n";
 }else{
   print OUTFILE "$line";
   #$descriptiveKeywordsCounter++;
  # print "\$descriptiveKeywordsCounter = $descriptiveKeywordsCounter\n";
  };#if ($descriptiveKeywordsCounter == 3){

}elsif($descriptiveKeywordsCounter == 3){

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

if ($file ne $filePrev){
$ngdaArray[$listCounter]=$file;
$listCounter++;
#print LISTFILE "$listCounter: $file\n";
$filePrev=$file;
};

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

if ($CharacterStringCounter <5){
#print OUTFILE "<!-- Removed <gco:CharacterString-->\n";
$CharacterStringCounter ++;
}else{
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";
#print OUTFILE "<!-- Something should go here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! -->\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:otherConstraints/){
if ($line =~ /\/ /){

if ($otherConstraintsCounter == 1){
print OUTFILE "$line";
print OUTFILE "<!--  end tag -->\n";
};

#print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
#print OUTFILE "<!--  end tag -->\n";



}else{
print "==================================================================================\n";
print OUTFILE "$line";
print "\$line = $line\n";
$otherVar="yes";
print "\$otherVar = $otherVar\n";


$otherConstraintsCounter++;

#if ($otherConstraintsCounter == 1){

#print OUTFILE "$line";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";

#print OUTFILE "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Access Constraints: None</gco:CharacterString>";
#rint OUTFILE "</gmd:otherConstraints>";

#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "$line";
#print OUTFILE "<!-- end of the first one -->\n";

#elsif($otherConstraintsCounter == 2){
#print OUTFILE "$line";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";

#rint OUTFILE " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Use Constraints: The TIGER/Line Shapefile products are not copyrighted however TIGER/Line and Census TIGER are registered trademarks of the U.S. Census Bureau.  These products are free to use in a product or publication, however acknowledgement must be given to the U.S. Census Bureau as the source.</gco:CharacterString>";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#rint OUTFILE "<!-- end of the second one -->";
#elsif($otherConstraintsCounter == 3){

#}elsif($otherConstraintsCounter > 3){
#rint OUTFILE "$line";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "<gmd:otherConstraints>";
#print OUTFILE " <gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Distribution Liability: No warranty, expressed or implied is made with regard to the accuracy of these data, and no liability is assumed by the U.S. Government in general or the U.S. Census Bureau in specific as to the spatial or attribute accuracy of the data.  The act of distribution shall not constitute any such warranty and no responsibility is assumed by the U.S. government in the use of these files.  The boundary information in the TIGER/Line Shapefiles is for statistical data collection and tabulation purposes only; their depiction and designation for statistical purposes do not constitute a determination of jurisdictional authority or rights of ownership or entitlement and they are not legal land descriptions.</gco:CharacterString>";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter The end!!!!!!!!!!!!!!!!! -->\n";
#print OUTFILE "\n";
#$distliab=1;
#};


};


#}elsif ($otherVar eq "yes"){
#print OUTFILE "$line";
#$otherVar="no"  


#}elsif ($line =~ /Constraints:/){
#print "In the constraints decision\n";
#print "\$line = $line\n"; 
#print OUTFILE "<!-- contraints if  -->\n";
#print OUTFILE "$line";

#}elsif ($line =~ /Liability/){
#print OUTFILE "<!--\$distliab = $distliab  -->\n";
#if ( $distliab == 0){
#print "In the Liability decision\n";
#print "\$line = $line\n";
#print OUTFILE "<!-- liability if (\$distliab = $distliab)   -->\n";
#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";
};

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

}elsif ($line =~ /<\/gco:CharacterString>/){

if ($CharacterStringCounter <5){
#print OUTFILE "<!--  Removed <\/gco:CharacterString>  -->\n";
}else{
print OUTFILE "$line";
};

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



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

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



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


}elsif($line =~ /<\/gmd:date>/){
#print "found the end date tag!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
#print "\$twicedateCounter = $twicedateCounter\n";

if ($enddateCounter<1 ){
$enddateCounter++;
#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){
#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++
};
#---------------------------------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){
 #print "\$tcCounter = $tcCounter\n";
 #print OUTFILE  "<!-- removed tc211.org-->\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 <1){
#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++;
}else{
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";
};

}elsif($line =~ /<\/gmd:CI_DateTypeCode>/){
if ($CI_DateTypeCounter ==1){
#print OUTFILE  "<!-- Removed <\/gmd:CI_DateTypeCode> -->\n";
$CI_DateTypeCounter++;
}else{
print OUTFILE "$line";
};
#---------------------------------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 ";

