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

while (<INFILE>){
$line =$_;
#print "\$line = $line\n";
if ($line =~ / Current Block Group State-based<\/gco:CharacterString>/){#change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!


if ($line =~ /Series Information/){
print OUTFILE "$line";
}elsif($line =~ /Feature Catalog/){
print OUTFILE "$line";
}else{
$foundTheme="yes";
$newLine=$line;
};

}elsif($foundTheme eq "yes"){
print OUTFILE "$newLine";
print OUTFILE "               </gmd:title>\n";
print OUTFILE "               <gmd:alternateTitle>\n";
print OUTFILE "                   <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">";
print OUTFILE "National Geospatial Data Asset (NGDA)  Current Block Group State-based Shapefile";#---------------------Change for every theme!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "                   </gco:CharacterString>\n"; 
print OUTFILE "               </gmd:alternateTitle>\n";
$foundTheme="no";
}elsif($line =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Governmental Units, and Administrative and/){
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Governmental Units and Administrative and Statistical Boundaries Theme</gco:CharacterString>\n";#Make sure that this is the right theme!!!!!!!!!!!
}elsif($line =~ /Statistical Boundaries<\/gco:CharacterString>/){
#nothing
}elsif($line =~ / <gco:CharacterString> NGDA Portfolio Themes/){
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">National Geospatial Data Asset";
}elsif($line =~ / <gco:CharacterString>ngda<\/gco:CharacterString>/){
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">NGDA<\/gco:CharacterString>\n";
}else{
print OUTFILE "$line";
};

#close OUTFILE;
#close INFILE;
};
close OUTFILE;
close INFILE;
system "mv $newISO $file";
system "rm $newISO";
};
