#!/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;
while (<INFILE>){
$line =$_;
#print "\$line = $line\n";
if ($line =~ /Statistical Area (CSA) National/){#change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
#print "\$line = $line\n";

if ($line =~ /Series Information/){
print OUTFILE "$line";
}elsif($line =~ /Feature Catalog/){
print OUTFILE "$line";
print "1st fc\n";
#change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catalog for the 2014 Current Combined Statistical Area (CSA) National Shapefile</gco:CharacterString>\n";
}else{
$foundTheme="yes";
$newLine=$line;
print " inserting the alernate Title\n";
};

}elsif ($foundTheme ne "yes" and $lineCounter ==0){
print "In the TIGER!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
print "\$line = $line\n";

if ($line =~ /Series Information/){
print OUTFILE "$line";
}elsif ($line =~ /" encoding="ISO-8859-1"?/){
print OUTFILE "$line";
}elsif ($line =~ /TIGER\/Line Shapefile/){

if ($foundTheme ne "yes"){
$newLine=$line;
print " inserting the alernate Title\n";
$lineCounter++;
$foundTheme="yes";

}else{
print OUTFILE "$line";
};

}else{
print OUTFILE "$line";
};

#};
#$lineCounter++;

}elsif($foundTheme eq "yes"){
print "in the yes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
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 Combined Statistical Area (CSA) National 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";
}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/TIGER2014/csa/tl_2014_csa.shp.ea.iso.xml<\/gco:CharacterString>\n";
}elsif($line =~ /Feature Catalog for the 2014 TIGER\/Line Shapefile/){
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catalog for the 2014  Current County Subdivision State-based Shapefile	</gco:CharacterString>\n";
print "1st fc\n";
#Feature Catalog for the 2014 TIGER/Line Shapefile
}else{
print OUTFILE "$line";
};

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