#!/usr/bin/perl -w
use strict;

$| = 1;

use Futils;

my($help)    = '';   my($file)    = '';
my($search)  = '';   my($replace) = '';
my($content) = '';   my(%in)      = ();

$help = <<'__help__';


============================================================================
sr version 1.0

   Search for any occurrance of [pattern] in a [file], and
   replace each match with your designated replacement [string].

   [pattern]      - regular expression
                    (quote or add proper escapes for the shell prompt)

   [file]         - system path to the file which you will scan

   [replacement]  - string which will replace all matches on [pattern]

Usage:

   sr -f [file] -s [search pattern] -r [replacement string]
   sr --file [file] --search [search pattern] --replace [replacement string]

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

__help__

while (my($n,$v) = splice(@ARGV,0,2)) { $in{ $n } = $v }

$file    = $in{'-f'} || $in{'--file'}    || die $help;
$search  = $in{'-s'} || $in{'--search'}  || die $help;
$replace = $in{'-r'} || $in{'--replace'} || die $help;

my($f) = Futils->new();

die $help unless (length($file) and length($search) and length($replace));

$search  =~ s/(^\/)|(\/$)//go;

$replace =~ s/(^\/)|(\/$)//go;

$search  =~ s/(^.*$)/ qq["$1"] /goxee;

$replace =~ s/(^.*$)/ qq["$1"] /goxee;

$content = $f->load_file($file);

$content =~ s/$search/$replace/go;

$f->write_file( 'file' => $file, 'content' => $content );

print('Done.', qq[\012] x 2);

exit;

=pod

   AUTHOR
      Tommy Butler <tommy @ atrixnet.com>
      phone: (817)-468-7716
      6711 Forest Park Dr
      Arlington, TX
           76001-8403

   COPYRIGHT   Tommy Butler. All rights reserved
   LISCENCE    This software is free, use/distribute under the GNU GPL.
   BUGS TO     Tommy Butler <perlmod @ atrixnet.com>

   This code uses Futils, a Perl module (or reusable code library) to make life
   easier when doing things with files.  You can download the code at:
   <http://ooopps.sourceforge.net/cgi-bin/archive.pl/pub/modules/Futils.pm>

   Futils uses other modules, which may be also be found here at the
   ooOPps Open Source Code Library under the "modules" section.  Refer to the
   itemized list below for more specific information:

      "Handy::Dandy"
         used by:    Futils
         file name   Dandy.pm
         get it at: /pub/modules/Handy/Dandy.pm

      "OOorNo"
         used by:    Futils, Handy::Dandy
         file name:  OOorNo.pm
         get it at: /pub/modules/OOorNo.pm

      "Handy::Dandy::TimeTools"
         used by:    Handy::Dandy
         file name:  TimeTools.pm
         get it at: /pub/modules/Handy/Dandy/TimeTools.pm

=cut