
#!/bin/sh

# lets check if we have at least two strings and
# one file

if [ -n "$1" ] && [ -n "$2" ] && [ -n "$3" ]; then
    echo "Replacing '$1' with '$2'";
else
    echo "nxreplace: str1 str2 files";
    echo "  to replace str1 with str2 in files.";
    exit 2;
fi

# lets check where is the temp directory

if [ -n "$TEMP" ]; then
   echo "Using temp dir '$TEMP'";
else
   TEMP="/tmp/"
   echo "Using temp dir '$TEMP'";
fi

# lets check sed presence

SED=`which sed`

if [ -x "$SED" ]; then
  echo "Using sed '$SED'";
else
  echo "Cannot find sed! Please install it or modify your PATH.";
  exit 2;
fi

STR1=$1
STR2=$2

shift
shift

for i in $*
do
  # this is not needed but will prevent sed error
  if [ -f "$i" ]; then
    echo "Patching $i";
    sed 's/"$STR1"/"$STR2"/g' $i > "$TEMP/"$i
    mv "$TEMP/"$i $i
  else
    echo "Cannot find file $i";
  fi
done

echo "Finished.";
exit 0;








