#!/bin/sh

usage() {
        echo \
"USAGE:  replacestring <BEFORE> <AFTER> <FILE ..>
        changes all instances of BEFORE to AFTER in each FILE
        and reports the file changes.

        Examples: replacestring TX Texas */addresses.*
                  replacestring \"foo bar\" foobar *.c"
        exit 0
}

case $1 in
        -h|-help)       usage;;
esac

if test $# -lt 3
then
        usage;
fi

TMPDIR=/tmp
TMPFILE=$TMPDIR/replacestring.$$

BEFORE=$1
AFTER=$2

shift;shift

for FILE in $*
do
        sed -e "s/$BEFORE/$AFTER/g" $FILE >$TMPFILE
        echo "$FILE:"
        diff $FILE $TMPFILE
        echo ""
        cp $TMPFILE $FILE
done

