#!/usr/bin/perl -w
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   Replace one string with another in files in one dir
# author
#   idc@planetlarg.net 08 march 06
# description
#   replace text, make a backup. Doing this
#   ./f2.pl -b fsaf -a XXX *.txt
#   replaces all occurrences of fsaf with XXX in all files endign in .txt
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
   use 5.005;
   use strict;

# command line switches
    # we want before value and after value
    # replace before with after
    use Getopt::Std;
    use vars qw($opt_b   $opt_a);
    my $options_ok;
    $options_ok = getopts('b:a:');
    # Check for options we don't want
    print "missing arguments or unknown options encountered. \n" unless $options_ok;
    # Check for the options we do want
    print "-b  is missing. \n" unless $opt_b;
    print "-a  is missing. \n" unless $opt_a;
    # print a usage message if there is a problem with the options
    usage() if not($opt_b and $opt_a);


#---------#---------#---------#---------#---------#---------#---------#---------
# main
#   The main program goes here.
    # command line values
    my $old;
    while ($old = shift) {
        my $new = "$old.new";
        open (OLD, "< $old")    or die "Can't open $old: $!";
        open (NEW, "> $new")    or die "Can't open $new: $!";
        select (NEW);

        while (<OLD>) {
            # change $_, then...
            s/$opt_b/$opt_a/g;
            print NEW $_    or die "Can't write $new: $!";
        }
        close (OLD)     or die "Can't close $old: $!";
        close (NEW)     or die "Can't close $new: $!";
        rename ($old, "$old.orig") or die "Can't rename $old to $old.orig: $!";
        rename ($new, $old) or die "Can't rename $new to $old: $!";
    }

#---------#---------#---------#---------#---------#---------#---------#---------
# subs
#   all subroutines go below here.
# subroutine
#   usage
# description
#   describe how this script should be used.
#   This is one way of producing a set of instructions, using simple indentation.
#   For more complex document, use mark-up. See the perldoc section below.
#
sub usage {

   use Text::Wrap;
   $Text::Wrap::columns = 60;
   my $pre1             = "   ";
   my $pre2             = "   ";
   my $name             = "$0 - Edit a file in place";
   my $synopsis         = "$0  -b before_text -a after_text file1 file2 file3 ...";
   my $description      = "
Read an original file, write changes to a temporary file.
Copy the original to a backup and copy the temporary file to the original.
";
   my $examples         = "
This command:
    $0 -b fsaf -a XXX *.txt
replaces all occurrences of fsaf with XXX in all files ending in .txt
";

   print
               "NAME \n",
               wrap ($pre1, $pre2, $name), "\n",
               "SYNOPSIS \n",
               wrap ($pre1, $pre2, $synopsis), "\n",
               "DESCRIPTION \n",
               fill ($pre1, $pre2, $description), "\n",
               "EXAMPLES \n",
               wrap ($pre1, $pre2, $examples), "\n",
   ;
   exit 1;
}

#---------#---------#---------#---------#---------#---------#---------#---------
