#!/bin/ksh
#
# Replace one string with another in files and subdirs
#
#
#
#  Author - Adam 
#
#  When   - 21/06/2000
#
#  Where  - Dudley
#
#  Why    - F*ck knows
#
#
#
############################################################################

############################################################################
#  Setup variables
#
############################################################################

PathToStartFrom=$1
StringToSearchFor=$2
StringToReplaceWith=$3
TemporaryFile=/tmp/CreatedByReplaceAll.sh
echo ""

############################################################################
# Validate input
#
############################################################################

if [ ${StringToSearchFor}a = a ] || [ ${StringToReplaceWith}a = a ] || [ ${PathToStartFrom}a = a ] ; then
        echo "Usage Path Search Replace"
        echo "Where \"Path\"=Path to Search, \"Search\"=String to search for and \"Replace\"=String to replace with"
        exit 1
fi

echo ""
echo "------------------------------------------------------------------------------"

if [ -d $PathToStartFrom ] ; then
        cd $PathToStartFrom
        echo "Replace ALL occurences of $StringToSearchFor with $StringToReplaceWith"
        echo "from directory `pwd` and down"
else
        echo "Directory does not exist"
        exit 1
fi

############################################################################
# Create a list of all files to be replaced and show it with the option to abort
############################################################################

echo "Searching files\c"
############################################################################
# Fix Pete's shoddy coding....        hcp       04-sep-00
############################################################################
find . -type f -name '*' -exec grep -l $StringToSearchFor {} \; > /tmp/filestoupdate.txt
echo "\rFiles selected to be updated (from `pwd`)"
cat /tmp/filestoupdate.txt

echo "------------------------------------------------------------------------------"
echo ""

echo "Press <Ctrl><C> now to abort"
read SomeCrap

##########################################################################################
# Put a backslash "\" in front of all frontslashes "/" to get it through sed
#
##########################################################################################

ModiStringToSearchFor=`echo $StringToSearchFor | sed 's/\//\\\\\//g'`
ModiStringToReplaceWith=`echo $StringToReplaceWith | sed 's/\//\\\\\//g'`

############################################################################ ##############
# Loop through the list and replace the text
#
############################################################################ ##############

exec 3< /tmp/filestoupdate.txt
while read -u3 FileName; do
        Command="s/${ModiStringToSearchFor}/${ModiStringToReplaceWith}/g"
        sed $Command $FileName > $TemporaryFile
        cp $FileName ${FileName}.bak.replaceall
        cat $TemporaryFile >  $FileName
done

