#!/bin/sh

#looks at a file, and replaces all the occurences of "$replace" with "$with".

if [ $# -lt 3 ] ; then
	echo "Usage: $0 <replace> <with> <file pattern>"
	exit 1
fi

replace=$1
with=$2

shift ; shift

for i in "$@" ; do
	echo "$i"
	tmpfile="/tmp/$i.$$.$RANDOM.`date`"
#	A=\`     #a char that does not appear in the string
	A="`echo | tr '\012' '\001' `" # get ascii 1.
	sed "s$A$replace$A$with$A" "$i" > "$tmpfile"
	if [ "$?" = 0 ] ; then
		echo success
#folllowing is way of preserving permissions
		cp -p "$i" "$i.bak"
		cp "$tmpfile" "$i"
		rm "$tmpfile"
	fi
done
