#!/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;
 $newTheme="ZIP Code Tabulation Area";
 $geoLevel="State-based";
 #geoLevel="National";
 $url="http://meta.geo.census.gov/data/existing/decennial/GEO/GPMB/TIGERline/TIGER2014/zcta510/tl_2014_zcta510.shp.ea.iso.xml";

 while (<INFILE>){
  $line =$_;
  #print "\$line = $line\n";
  if ($line =~ /ZIP Code Tabulation Area/){#Change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   #print "\$line = $line\n";

   if ($line =~ /Series Information/){
    print OUTFILE "$line";
   }elsif($line =~ /Feature Catalog/){
    #print OUTFILE "$line";
    print "1. Feature catolog changed\n; \n";
    print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">Feature Catalog for the 2014 $newTheme $geoLevel  Shapefile</gco:CharacterString>\n";
   }elsif ($line =~ /gco:LocalName/){
    print OUTFILE "$line";
   }elsif ($line =~ /are/){
    print OUTFILE "$line";
   }elsif ($line =~ /entities/){
    print OUTFILE "$line";
   }elsif ($line =~ /attributes/){
    print OUTFILE "$line";
   }elsif ($line =~ /gco:LocalName/){
    print OUTFILE "$line";
   }elsif ($line =~ /Tabulation/){

    if ($line =~ /TIGER\/Line Shapefile/){
     $foundTheme="yes";
     $newLine=$line;
     print "1.  inserting the alernate Title for\n";
     print "\$line = $line\n"
    }else{
     print OUTFILE "$line";
    };
 
   }elsif ($scopeVar eq "yes"){
    print OUTFILE "$line";
    $scopeVar ="no";
    }elsif($line =~ /<!/){
    print OUTFILE "$line";
    print "\$line = $line\n"
   }elsif($line =~ /<!--fullOrg:/){
    #nothing
   }elsif($line =~/fullOrg:/){
    print OUTFILE "$line";
    print "\$line = $line\n"
   }elsif($line =~ /<!--postComma1:/){
    #nothing
   }elsif($line =~ /postComma1:/){
    #nothing 
   }elsif($line =~ /<!--division:/){
    print OUTFILE "$line";
    print "\$line = $line\n"; 
   }else{
    $foundTheme="yes";
    $newLine=$line;
    print "1.  inserting the alernate Title for\n";
    print "\$line = $line\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";
    print "leaving the series info alon!!!!!!!!!!!!!!!!!!\n";
   }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:LocalName/){
    print OUTFILE "$line";
   }elsif ($line =~ /gmx:scope/){
    print OUTFILE "$line";
    $scopeVar="yes";
   }elsif($line =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Governmental Units, and Administrative and/){
    print "2a. updating the theme !!!!!!!!!!";
    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 =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Transportation/){
    print "2a. updating the theme !!!!!!!!!!\n";
    print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Transportation Theme</gco:CharacterString>\n";

   }elsif($line =~ / <gco:CharacterString> NGDA Portfolio Themes/){
    print "3a. updating NGDA Portfolio Themes !!!!!!!!!!\n";
    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";
    print "4a. updating NGDA!!!!!!!!!!\n";
   }elsif($line =~ /http:\/\/meta.geo.census.gov/){
   #Change for every theme!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
   print "6. updating the EA URL!!!!!!!!!!!!!!!!\n";
   print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">$url</gco:CharacterString>\n";
   }elsif($line =~ /Feature Catalog/){
    print OUTFILE "<!-- This is the name of the feature catalog -->\n";
   }elsif($line =~/fullOrg:/){
    #nothing
   }elsif($line =~ /<!--postComma1:/){
    #nothing
   }elsif($line =~ /<!--division:/){
    #nothing

   }elsif($line =~ /<!--/){
    print "\$line = $line\n";
    if($line =~/Series Collection File/){
    $bracPos=index($line,'-->');
    $line1 = substr($line,0,$bracPos+3);
    $line2 =substr($line,$bracPos+3);
    print OUTFILE "$line1\n";
    print OUTFILE " $line2\n";
    }elsif($line =~ /before the keyword/){
     print "dealing with the keywords!!!!!!!!!!!!!!!!!!!!!!!\n"; 
     $begpos=index($line,'<!--');
     $bracPos=index($line,'-->');
     $line1 = substr($line,0,$begpos);
     $line2 =substr($line,$bracPos+3);
    print OUTFILE "$line1\n";
    print OUTFILE " $line2\n";
    }else{
     print OUTFILE "$line";
     #print "\$line = $line\n";
    };
   }elsif($line =~ /<!/){
    print OUTFILE "$line";
    print "\$line = $line\n"
   }elsif($line =~/fullOrg:/){
    print OUTFILE "$line";
    print "\$line = $line\n"  
   }elsif($line =~ /postComma1:/){
    #nothing 
   }elsif($line =~ /<!--division:/){
    #nothing
   }elsif ($line =~ /TIGER\/Line Shapefile/){

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

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

  }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\">National Geospatial Data Asset (NGDA) $newTheme $geoLevel Shapefile</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 "2b. Theme successfully changed\n";
   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 =~ /<gco:CharacterString xmlns:gco="http:\/\/www.isotc211.org\/2005\/gco">Transportation/){
   print "3b. updating the theme !!!!!!!!!!\n";
   print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org\/2005\/gco\">Transportation Theme</gco:CharacterString>\n";
  }elsif($line =~ /Governmental Units, and Administrative and/){
   print "3b. updating the theme !!!!!!!!!!\n";
   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 "3b. updating the theme !!!!!!!!!!\n";
   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 "4b. updating NGDA Portfolio!!!!!!!!!\n";
   print OUTFILE "                <gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">National Geospatial Data Asset";
  }elsif($line =~ / <gco:CharacterString>ngda<\/gco:CharacterString>/){
   print "5b. updating NGDA !!!!!!!!!\n";
   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 "6. updating the EA URL!!!!!!!!!!!!!!!!\n";
   print OUTFILE "<gco:CharacterString xmlns:gco=\"http:\/\/www.isotc211.org/2005\/gco\">$url<\/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 $newTheme $geoLevel 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";
};
};
