#!/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";
$Tiger=0;
#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";
#f($Tiger>1){
#print OUTFILE "$line";
if ($line =~ /TIGER\/Line Shapefile/){#change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#$Tiger++;
print "Here 1!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "\$line = $line\n";

if ($line =~ /Series Information/){
print OUTFILE "$line";
print "Here 2!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($line =~ /Feature Catalog/){
#print OUTFILE "$line";
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catolog for the 2010 Census Traffic Analysis Zone (TAZ) State-based Shapefile</gco:CharacterString>\n";
print "Here 3!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
#change everytime
}elsif($Tiger>1){
print OUTFILE "$line";
#print "Here 3.1-------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}else{

if ($Tiger == 0){
$foundTheme="yes";
$newLine=$line;
$Tiger++;
print "\$Tiger = $Tiger\n";
#print "Here 3.2------------------------!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}else{
print OUTFILE "$line";
#print "Here 3.3-----------------------!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
};
print "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n\n";
};

#$Tiger++;
}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)   2010 Census Traffic Analysis Zone (TAZ) State-based Shapefile";#--------------Change for every theme!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "                   </gco:CharacterString>\n"; 
print OUTFILE "               </gmd:alternateTitle>\n";
$foundTheme="no";
#print "Here 4!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}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!!!!!!!!!!!
#print "Here 5!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($line =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Transportation<\/gco:CharacterString>/){
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Transportation Theme </gco:CharacterString>\n";
print OUTFILE "           </gmd:keyword>\n";
print "Here 6!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
$taz="yes";
}elsif($taz eq "yes"){
print OUTFILE "           <gmd:keyword>\n";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">NGDA <\/gco:CharacterString>\n";
print OUTFILE "           </gmd:keyword>\n";
print OUTFILE "           <gmd:keyword>\n";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">National Geospatial Data Asset <\/gco:CharacterString>\n";
print OUTFILE "           </gmd:keyword>\n";
$taz="no";
}elsif($line =~ /http:\/\/meta.geo.census.gov/){
#Change everytime!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">http://meta.geo.census.gov/data/existing/decennial/GEO/GPMB/TIGERline/TL2011/taz/2011_taz10.ea.iso.xml<\/gco:CharacterString>\n";
print "Here 9!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}elsif($line =~ /Feature Catolog/){
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catolog for the 2010 Census Traffic Analysis Zone (TAZ) State-based Shapefile</gco:CharacterString>\n";
#Feature Catalog for the 2014 TIGER/Line Shapefile
print "Here 10!!!!!!!!!!!!!!!!!!!!!!!!!\n";
}else{
print OUTFILE "$line";
};

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