#!/bin/sh

if [ $# -ne 4 ] ; then
  echo "Invalid parameters. Pass SED location, KEY, VALUE and FILE"
	exit 0
fi

SED=$1
KEY=$2
VALUE=$3
FILE=$4

## Some OS don't have mktemp, AIX 5.3 for instance :S
## Some OS don't support sed -i , Darwin for instance :S
UNAME=`uname`
if [ "${UNAME}" = "Darwin" -o "${UNAME}" = "AIX" ] ; then
	TMPFILE=substitute-$$
	${SED} "s|${KEY}|${VALUE}|g" < ${FILE} >${TMPFILE}
	command mv -f ${TMPFILE} ${FILE}
else
	${SED} "s|${KEY}|${VALUE}|g" -i ${FILE}
fi

chmod a+r ${FILE}

# Check if the resulting file is a script
if [ "${FILE##*.}" = "sh" ] ; then
	chmod a+x ${FILE}
fi
