#!/bin/sh
#---------#---------#---------#---------#---------#---------#---------#---------
#
# title
#   Replace one string with another in files and subdirs
# author
#   Adam  21/06/2000
# description
#   Change all parameter 2 with parameter 3 in all files including subdirs
#   from parameter 1
# modifications
#
#---------#---------#---------#---------#---------#---------#---------#---------
# setup
    usage_message="
    usage:
      $0 [-a] Path Search Replace
    example:
      the command
         $0 /opt/iplanet/iws6/isp01 131072 1024000
      replaces ALL occurences of 131072 with 1024000
    description
      Where
      -a          search ascii text files only. NOT IMPLEMENTED YET
      \"Path\"    the Path to Search,
      \"Search\"  the String to search for and
      \"Replace\" the String to replace it with
    "
# command line options
    PathToStartFrom=$1
    StringToSearchFor=$2
    StringToReplaceWith=$3
    # no options provided? tell the punter how to use this utility
    if [ $# -eq 0 ]; then
      echo "no options were provided."
      echo "$usage_message" 1>&2
      exit 1
    fi
    if  [ ${StringToSearchFor}a   = a ] || \
        [ ${StringToReplaceWith}a = a ] ||  \
        [ ${PathToStartFrom}a     = a ]
    then
      echo "missing options."
      echo "$usage_message" 1>&2
      exit 1
    fi

# variables
    TemporaryFile=/tmp/CreatedByReplaceAll.sh

    # 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'`

# main
# directory path
    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

# search
    # Create a list of all files to be replaced and
    # show it with the option to abort
    echo "Searching files..."
    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 ""

# prompt
    echo "Press <Ctrl><C> to abort"
    echo "Press <Return> to continue"
    read SomeCrap

# replace
    # 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
            # backup the original file
            cp $FileName ${FileName}.bak.replaceall
            cat $TemporaryFile >  $FileName
    done
#---------#---------#---------#---------#---------#---------#---------#---------
