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



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


opendir INDIR, $directory;

foreach $file (readdir INDIR){

$dotLocation=index($file,".");
$preDot= substr($file,0,$dotLocation);
$postDot=substr($file,$dotLocation);
$newISO= $preDot . "_newISO" . $postDot;

print "\$file = $file\n";
#print "\$preDot = $preDot\n";
#print "\$postDot = $postDot\n";
#print "\$newISO = $newISO\n";

open INFILE, "<$file";
open OUTFILE,">$newISO";
$lineCounter=0;
$yescounter=0;
$descriptiveKeywordsCounter=0;

while (<INFILE>){
$line =$_;

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

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

}elsif($descriptiveKeywordsCounter == 3){

#<gmd:keyword>
if ($line =~ /<gmd:keyword>/){
print OUTFILE "<!-- Removed <gmd:keyword -->\n";
}elsif ($line =~ /<gco:CharacterString/){
print OUTFILE "<!-- Removed <gco:CharacterString-->\n";
}elsif($line =~ /<\/gmd:keyword>/){
print OUTFILE "<!--  Removed <\/gmd:keyword> -->\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>/){
print OUTFILE  "<!--  Removed \/gmd:type  -->\n";
}elsif($line =~ / <gmd:thesaurusName>/){
print OUTFILE  "<!--  Removed <gmd:thesaurusName>  -->\n";
}elsif($line =~ /<gmd:CI_Citation> /){
print OUTFILE  "<!-- Removed <gmd:CI_Citation> -->\n";
}elsif($line =~ /<gmd:CI_Citation> /){
print OUTFILE  "<!-- Removed <gmd:CI_Citation>  -->\n";
}elsif($line =~ /<gmd:title>/){
print OUTFILE  "<!-- Removed gmd:title> -->\n";
}elsif($line =~ /<gmd:date>/){
print OUTFILE  "<!-- Removed <gmd:date> -->\n";
}elsif($line =~ /<gmd:CI_Date>/){
print OUTFILE  "<!-- Removed <gmd:CI_Date> -->\n";
}elsif($line =~ /<gco:Date /){
print OUTFILE  "<!-- Removed <gco:Date -->\n";
}elsif($line =~ / <\/gmd:date>/){
print OUTFILE  "<!-- Removed <\/gmd:date> -->\n";
}elsif($line =~ /<gmd:dateType>/){
print OUTFILE  "<!-- Removed <gmd:dateType> -->\n";
}elsif($line =~ /<gmd:CI_DateTypeCode /){
print OUTFILE  "<!-- Removed <gmd:CI_DateTypeCode  -->\n";
}elsif($line =~ /codeListValue="revision"/){
print OUTFILE  "<!-- Removed codeListValue=\"revision\"  -->\n";
}elsif($line =~ /codeSpace=\"003\"> revision/){
print OUTFILE  "<!-- Removed codeSpace=\"003\">  -->\n";
}elsif($line =~ /<\/gmd:CI_DateTypeCode>/){
print OUTFILE  "<!-- Removed <\/gmd:CI_DateTypeCode> -->\n";
}elsif($line =~ /<\/gmd:dateType>/){
print OUTFILE  "<!-- Removed <\/gmd:dateType -->\n";
}elsif($line =~ /<\/gmd:CI_Date>/){
print OUTFILE  "<!-- Removed <\/gmd:CI_Date> -->\n";
}elsif($line =~ /<\/gmd:CI_Date>/){
print OUTFILE  "<!-- Removed <\/gmd:CI_Date> -->\n";
}elsif($line =~ /<\/gmd:date>/){
print OUTFILE  "<!-- Removed <\/gmd:date> /) -->\n";
}elsif($line =~ /<gmd:otherCitationDetails>/){
print OUTFILE  "<!-- Removed <gmd:otherCitationDetails> -->\n";
}elsif($line =~ / <\/gmd:otherCitationDetails>/){
print OUTFILE  "<!-- Removed <\/gmd:otherCitationDetails> -->\n";
}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:MD_Keywords>/){
print OUTFILE  "<!-- Removed <\/gmd:MD_Keywords> -->\n";
}elsif($line =~ /<\/gmd:descriptiveKeywords/){
print OUTFILE  "<!-- Removed <\/gmd:MD_Keywords> -->\n";

}else{
print OUTFILE "$line";


};#if ($line =~ /<gmd:descriptiveKeywords>/){

};#while (<INFILE>){
};#foreach $file (readdir INDIR){
};
