#!/bin/sh

TARGET_DIRS="$HOME/public_html"

if [ $# -eq 0 ]
then
  echo Usage: move_systems_altogether \''http://daisy.uwaterloo.ca/~eddemain'\' \''http://db.uwaterloo.ca/~eddemain'\'
  echo Notice no trailing slash, but otherwise complete.
  echo In fact, you can use this to perform any substitution on my whole web directory.
  echo But note that it makes every relevant file as modified.
  echo You can also omit the last argument in order to just list the relevant files.
  exit 1
fi

pattern="`echo "$1" | sed 's/\./\\./g'`"

echo Testing the existence of getmod:
getmod . || ( echo getmod does not exist but is required ; exit 1 )

for file in `find $TARGET_DIRS -name \*.html -print`
do
  if grep "$1" $file >/dev/null
  then
    echo $file
    if [ $# -gt 1 ]
    then
      mv $file $file~
      sed "s#$pattern#$2#g" <$file~ >$file
      chmod `getmod $file~` $file
      #echo did $file, enough for me
      #exit 1
    fi
  fi
done
