#!/opt/perl/bin/perl -w
#
$vernum = "1.0 - 04 MAR 99" ;           # Code version and modify date
#
# Correct pattern in file
# (Hand-edit the OLDPATTERN and NEWPATTERN lines to be whatever you need
# before using this script.)
#
# This code copyright 1995-1999 by
# D. W. Eaton, Artronic Development, Phoenix, AZ -- dwe@arde.com
#
# This software is made freely available under the provisions of the Perl
# "Artistic" license:  http://language.perl.com/misc/Artistic.html
#
# This code is not supported and is not warranteed to perform any particular
# function. Contact dwe@arde.com for aditional information.
# If you find bugs or make enhancements, it would be appreciated if you
# sent them on to the author at dwe@arde.com.

require "ctime.pl" ; # (incase we need time for something in the transform)
#
$true = 1;  # truth values
$false = 0;
# ---------------------------------------------------
# Configurable values:
$keepme   = $true;          # Set to TRUE to just re-name old file
$verbose  = $false;         # Set to TRUE for additional messages
# ---- end normal configuration items --------------------
$errmsg = '' ;

#----- new values:

# --------------------------- logic ----------------------------------
# Get any command-line arguments that may have been passed (in pairs):
# (This does not use GetOptions ... it brute-forces the arguments)
# (GetOptions would be nicer but hasn't been worth implementing yet.)
$numargs = scalar (@ARGV) ;
if ($numargs == 1)
{
 $thisfile = shift (@ARGV) ;
}
else
{
 print STDERR "Error: should have 1 argument (filename), found $numargs\n";
 exit (1);
}
# Got all agruments now ....

#
$submitdate = time ;
$submitdate = ctime($submitdate) ;
$submitdate =~ s/\n//g ;
#
# ---------------- read file -----------------
#
$outcontent = ""; # Initialize to no output content
$changed = $false;
if (open (MYFILE, "<$thisfile"))
{
   $lineno = 0;
   while (defined ($line = <MYFILE>))
   {
    $lineno++;

    chomp ($line);
    $oldline=$line;

    # - - - - - - - - - - - - - - -
    # Alter the patterns below and add more such blocks as you need them ...
    if ($line =~ /ORIGPATTERN1/)
    {
     # Convert line containing this pattern:
     $line =~ s/ORIGPATTERN1/NEWPATTERN1/g;
     $changed = $true;
    }
    elsif ($line =~ /ORIGPATTERN2/)
    {
     $line =~ s/ORIGPATTERN2/NEWPATTERN2/g;
     $changed = $true;
    }
    # Alter the above and/or add more such blocks as you need them ...
    # - - - - - - - - - - - - - - -

    if (($line ne $oldline) && $verbose)
    {
     print STDERR "Diff. [$lineno] old:$oldline\nnew:\n$line\n";
    }

    # Just pass through all others:
    $outcontent .= "$line\n";
   }
   close (MYFILE);
}
else
{
   $errmsg .= " (can't open file '$thisfile')" ;
}
if ($errmsg)
{
 &err_msg ("$errmsg\n") ;
 exit (1) ;
}
# ---------------- file read -----------------

# -- -- -- save new file -- -- --
if ($changed)
{
 # Yep, we changed something
 # Write to alt file until we know it all got there:
 if (open (MYFILE, ">$thisfile.new"))
 {
  print MYFILE "$outcontent" ;
  close (MYFILE) ;
 }
 else
 {
  # Wanted to write file but couldn't
  &err_msg ("Can't create new file $thisfile.new:  $!\n") ;
  exit (0) ;
 }
 # -- -- -- end save new file -- -- --

 # Get rid of old one and rename this one:
 if ($keepme)
 {
  # Rename orig file (rather than just delete it):
  unlink ("$thisfile.bak") ;
  rename ("$thisfile", "$thisfile.bak") ;
 }
 else
 {
  # Just get rid of old file:
  unlink ("$thisfile") ;
 }
 rename ("$thisfile.new", "$thisfile") ;
 print "Fixed file $thisfile\n";
}
else
{
 print "File $thisfile already OK\n";
}
#
exit (0) ;
### END.

# --------------------------- subroutines ----------------------------
#
# Tell user that an error has occurred
sub err_msg
{
   local ($msg) = @_ ;

   print "Error found by $0 version $vernum --\n";
   print "$msg\n" ;
}
#---------------------
