#!/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;
$counter=0;

opendir INDIR, $directory;

foreach $file (readdir INDIR){
if($file ne "." or $file ne ".."){

 $dotLocation=index($file,".");
 $preDot= substr($file,0,$dotLocation);
 $postDot=substr($file,$dotLocation);
 $newISO= $preDot . "_newISO" . $postDot;
print "\nxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
 print "\$file = $file\n";
 print "$counter files done!\n";
 #print "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX\n";
 $counter++;
 #print "\$preDot = $preDot\n";
 #print "\$postDot = $postDot\n";
 #print "\$newISO = $newISO\n";

 close INFILE;
 close OUTFILE;
 open INFILE, "<$file";
 open OUTFILE,">$newISO";
 $line="";

 $lineCounter=0;
 $yescounter=0;

#---------------------------Change------------------------------------------------------------------------------------
 $newTheme="Block State-based Shapefile with Housing and Population Data";#1
 $geoLevel="State-based";#2
 #geoLevel="National";
 $url="http://meta.geo.census.gov/data/existing/decennial/GEO/GPMB/TIGERline/TL2011/taz/2011_taz10.ea.iso.xml";#3
#---------------------------Change------------------------------------------------------------------------------------

 while (<INFILE>){
  $line =$_;
  #print "\$line = $line\n";
  if ($line =~ /<\/gmd:characterSet>/){#4Change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   print OUTFILE"$line\n";
   print OUTFILE"<gmd:parentIdentifier>";
   print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">TIGER/Line Shapefile, 2010, Series Information File for the 2010 Census Block State-based Shapefile with Housing and Population Data</gco:CharacterString>\n"; 
  print OUTFILE"</gmd:parentIdentifier>"
}else{
print OUTFILE"$line\n";
};
close OUTFILE;
close INFILE;
system "mv $newISO $file";
#system "rm $newISO";
};
};
};
