#!/usr/local/bin/perl -w
#
print "\nThe OBJECTROOT is the directory named objects/, below which\n";
print "are subdirectories representing your categories of data.\n";
print "\nPlease enter the full path to the OBJECTROOT directory ";
$OBJDIR=<STDIN>;
chop($OBJDIR);

#
# Open an audit file, keeping all removed lines
#
open(REM,">info.removed") || die "Can't open 'info.removed' to hold removed lines; $!\n";
#
# First, find all subdirectories in the given tree where there are .info files
#
open(FIND,"find $OBJDIR -name \"*.info\" -print | ");

$|=1;
#
# process each .info file, put deleted lines in a .removed file, create a .new 
#
while ($info = <FIND>) {
  print $info;
  print REM $info;
  chop $info;
#
# set a flag to indicate a 'no goahead' condition
#
  $goahead = 0;
  open(FILE,$info);
  open(NEW,">$info.new");
  while ($line = <FILE>) {
#
# we don't check the lines after we get the goahead
#
     if ($goahead == 1) {
           print NEW $line;
     } else {
#
# check for the last line (typically) to delete in a standard .info file (</h1>)
#
         if ($line =~ /\/[Hh]1/) {
             $newline = substr($line,index($line, "/h1")+4);
             print NEW $newline unless length($newline) < 2;
             print REM "</h1>\n";
#
# once the HTML tag for turning off header 1 is seen, no more deletes
#
             $goahead = 1;
         } else {
              print REM $line;
         }
     }
  }
  close(FILE); 
  close(NEW);
  rename($info,"$info.old");
  rename("$info.new",$info);
}
close(FIND);
close(REM);

print "\nCheck the file 'info.removed' in this directory which contains the\n";
print "lines deleted from all .info files.  Each entry is prefaced by the\n";
print "file name from which it was removed.  The only lines that should be in the\n";
print "info.removed file are:\n<h1>\nobjectname\n</h1>\n";
print "If any other lines are included, you can restore the old *.info file by \n";
print "renaming the *.info.old file to *.info.\n";
exit(0);
