#!/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);
};

print "runing version $0\n";

#Infile = $gOptions{listfile};
$directory=$gOptions{directory};
chdir $directory;


opendir INDIR, $directory;

foreach $file (readdir INDIR){
$totalFileCounter++;
$dotLocation=index($file,".");
$preDot= substr($file,0,$dotLocation);
$postDot=substr($file,$dotLocation);
$newISO= $preDot . "_newISO" . $postDot;
$listFile="ListOfNGDAFiles.txt";
print "\$otherConstraintsCounter = $otherConstraintsCounter\n";
print "-----------------------------------------------------------\n";
print "\$file = $file\n";
#print "\$preDot = $preDot\n";
#print "\$postDot = $postDot\n";
#print "\$newISO = $newISO\n";

open INFILE, "<$file";
open OUTFILE,">$newISO";
open LISTFILE,">>$listFile";

$lineCounter=0;
$yescounter=0;

$descriptiveKeywordsCounter=0;
$CI_CitationCounter=0;
$CharacterStringCounter=0;
$titleCounter=0;
$dateTypeCounter=0;
$CI_DateCounter=0;
$dateCounter=0;
$otherCitationDetailsCounbter=0;
$CI_DateTypeCounter=0;
$codeListValue=0;
$gcodateCounter=0;
$tcCounter=0;
$elseEnd=0;
$otherConstraintsCounter=0;
$otherVar="no";
$CharacterStringCounter =0;
$newTitleCounter=0;
$endTitleCounter=0;

while (<INFILE>){
$line =$_;

#-----------------------------SSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSSTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTTOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOOO{{{{{{{

#}else{
#print OUTFILE "$line";
if ($line =~ /<gco:CharacterString/){
 print "in the character string\n";
 if ($CharacterStringCounter == 0){
  $newTitle= $line;
  print "\$newTitle = $newTitle\n";
  $CharacterStringCounter++;
 }elsif($CharacterStringCounter ==1){
  $CharacterStringCounter ++;
  $newTitleB= $line;
  print "\$newTitleB = $newTitleB\n";
 }else{
  print OUTFILE "$line";
 };

}elsif($line =~ /<\/gmd:title>/){
 print "IN the title!!!!!!!!!!!!!!!!!!!!!!!\n";

# if($CharacterStringCounter ==1){
if($endTitleCounter ==0){
print OUTFILE "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$endTitleCounter++;
}elsif($endTitleCounter ==1){
$endTitleCounter++;
#print OUTFILE "<gmd:alternateTitle>\n";
#print OUTFILE "<gco:CharacterString xmlns:gco=\"http://www.isotc211.org/2005/gco\">National Geospatial Data Asset (NGDA) All Roads County-based Shapefile</gco:CharacterString>\n";
#print OUTFILE "</gmd:alternateTitle>\n";


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

}elsif($line =~ /<gmd:title>/){
if ($newTitleCounter == 0){
print OUTFILE "<gmd:title> $newTitle </gmd:title>\n";
$newTitleCounter++;
}else{
print OUTFILE "$line";
};


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

#};#if ($line =~ /<gmd:descriptiveKeywords>/){
};#while (<INFILE>){
#print "now moving the file !!!!!!!!!!!!!!!!!!!!!!\n";
system "mv $newISO $file";
};#foreach $file (readdir INDIR){a

#sort @ngdaArray;


print LISTFILE "The following files need to have their NGDA tags removed:\n";

foreach $ngdaArray (@ngdaArray){
$ngdaCounter++;
print LISTFILE "$ngdaCounter:  $ngdaArray\n";

};

print " A total of $totalFileCounter files have had their NGDA tags removed\n\n ";

