#!/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 =$_;
print "\$line = $line\n";
if ($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/){




#-----------------------------------------------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:otherConstraints/){
 #print OUTFILE "<!-- Working on the gmd:otherConstraints -->\n";
 #if ($line =~ /\/ /){
  #print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter (first otherContraints)   -->\n";

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


 #}else{
 #print "==================================================================================\n";
 #$lenLine =length($line);
 #print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter (here otherConstraints else)   -->\n";
 #print OUTFILE "<!-- \$line = $line -->\n";
 #print OUTFILE "<!-- \$lenLine = $lenLine -->\n";

 #if ($lenLine >40){
 # print OUTFILE "<!-- \$lenLine = $lenLine -->\n";
 # print OUTFILE "<gmd:otherConstraints>";
 # print OUTFILE "$line";
 # print OUTFILE "</gmd:otherConstraints>";

  #print "\$line = $line\n";
  #$otherVar="yes";
  #print "\$otherVar = $otherVar\n";
 #};




#if ($otherConstraintsCounter == 1){
#print OUTFILE "$line";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "<gmd:otherConstraints>";
#print OUTFILE "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Access Constraints: None</gco:CharacterString>";
#print 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";
#print OUTFILE "<gmd:otherConstraints>\n";
#print 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>";#</gco:CharacterString>
#print OUTFILE "<!-- \$otherConstraintsCounter = $otherConstraintsCounter -->\n";
#print OUTFILE "<!-- end of the second one -->\n";

#}elsif($otherConstraintsCounter == 3){
#}elsif($otherConstraintsCounter > 3){
#print 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 -->\n";
#};


# $otherConstraintsCounter++;
#};
#print OUTFILE "<!-- End of other contraints -->\n";

#}elsif($line =~ /copyrighted/){
#}elsif($line =~ /Use Constraints:/){
#print OUTFILE "<gmd:otherConstraints>\n";
#print OUTFILE "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Access Constraints: None</gco:CharacterString>\n";
#print OUTFILE "</gmd:otherConstraints>\n";


# print OUTFILE "<gmd:otherConstraints>\n";
# print OUTFILE "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">Use Constraints:\n";
# print OUTFILE "$line";
 # print OUTFILE "</gmd:otherConstraints>";
#}elsif($line =~ /places suggest/){
#print OUTFILE "$line";
#print OUTFILE "</gco:CharacterString>";
#print OUTFILE "</gmd:otherConstraints>\n";
#print OUTFILE "<gmd:otherConstraints>\n";

#}elsif($line =~ /Distribution/){

#if ($line =~ /MD/){
#print OUTFILE "$line";
#}else{
#print OUTFILE "<gmd:otherConstraints>";
#print OUTFILE "$line";
#};

#}elsif($line =~ /legal land descriptions.<\/gmd:otherConstraints>"/){
#print OUTFILE "$line";
#print OUTFILE "</gmd:otherConstraints>\n";
#print OUTFILE "<!--- next one --> \n";
#here!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#}elsif($line =~ /<\/gmd:MD_LegalConstraints>/){
#print OUTFILE "</gmd:otherConstraints>\n";
#print OUTFILE "$line";
}elsif($line =~ /<gmd:MD_RestrictionCode/){
print OUTFILE "$line";
}elsif($line =~ /The boundary information/){
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

print "In the alternate Title!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($line =~ /gco:CharacterString/){
if ($rmTitle eq "yes"){
$rmTitle="no";
};
#----------------------------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){
$elseEnd++;#this one
#------------------4444444444444444444444444444444444444444444444
#}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";
};

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

