#!/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;
while (<INFILE>){
$line =$_;
#print "\$line = $line\n";
if ($line =~ /Public Use Microdata Area (PUMA) State-based/){#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 non-Tiger \n";
#Change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catalog for the 2014 Public Use Microdata Area (PUMA) State-based 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";
print "\$lineCounter = $lineCounter\n";
if ($line =~ /Series Information/){
print OUTFILE "$line";
}elsif ($line =~ /" encoding="ISO-8859-1"?/){
print OUTFILE "$line";
}elsif ($line =~ /elementary/){
print OUTFILE "$line";
#print "elementary!!!!!!!!!!!!!!";
#print "\$line = $line\n";
#print "---------------------------elementary!!!!!!!!!!!!!!";
}elsif ($line =~ /database/){
print OUTFILE "$line";
}elsif ($line =~ /accessed/){
print OUTFILE "$line";
}elsif ($line =~ /Constraints/){
print OUTFILE "$line";
}elsif ($line =~ /Coordinates/){
print OUTFILE "$line";
}elsif ($line =~ /ordering/){
print OUTFILE "$line";
}elsif ($line =~ /copy/){
print OUTFILE "$line";
}elsif ($line =~ /reflects/){
print OUTFILE "$line";
}elsif($line =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Governmental Units, and Administrative and/){
print "gov!!!!!!!!!!!";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Governmental Units and Administrative and Statistical Boundaries Theme</gco:CharacterString>\n";
}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 =~ /Feature Catalog/){
print OUTFILE "<!-- This is the name of the feature catalog -->\n";

}elsif ($line =~ /TIGER\/Line Shapefile/){

if ($foundTheme ne "yes"){
$newLine=$line;
print " inserting the alernate Title(TIGER!!!!!!!!!!!!!!!!!!!!!)\n";
$lineCounter=$lineCounter+1;
$foundTheme="yes";
#:print "\$lineCounter = $lineCounter\n";
}else{
print OUTFILE "$line";
};

}else{
print OUTFILE "$line";
};
#print "---------------------------------------------------------------------\n\n";

#};
#$lineCounter++;

}elsif($foundTheme eq "yes"){
print "\$yescounter = $yescounter\n";
#print "in the yes!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
if ($yescounter ==0){
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) Public Use Microdata Area (PUMA) Shapefile";#---------------------Change for every theme!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "                   </gco:CharacterString>\n"; 
print OUTFILE "               </gmd:alternateTitle>\n";
$foundTheme="no";
$yescounter = $yescounter+1;
}else{
print OUTFILE "$line";
};

}elsif($line =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Governmental Units, and Administrative and/){
print "gov!!!!!!!!!!!";
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 =~ /Governmental Units, and Administrative and/){
print "gov!!!!!!!!!!!";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Governmental Units and Administrative and Statistical Boundaries Theme</gco:CharacterString>\n";
}elsif($line =~ /Governmental Units/){
print "gov!!!!!!!!!!!";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Governmental Units and Administrative and Statistical Boundaries Theme</gco:CharacterString>\n";
}elsif($line =~ /Statistical Boundaries<\/gco:CharacterString>/){
#nothing
}elsif($line =~ /Administrative/){
print "gov!!!!!!!!!!!";
print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Governmental Units and Administrative and Statistical Boundaries Theme</gco:CharacterString>\n";

}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 for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">http://meta.geo.census.gov/data/existing/decennial/GEO/GPMB/TIGERline/TIGER2014/puma10/tl_2014_puma10.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 Public Use Microdata Area (PUMA) 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";
};
