#!/bin/sh -x

FIND="site"
SCRIPTDIR=`dirname $0`
echo $SCRIPTDIR | grep -q '^\/'
if [ $? -ne 0 ] ; then
	SCRIPTDIR="`pwd`/$SCRIPTDIR"
fi
FILENAME="$SCRIPTDIR/policy.xml"

# It's certainly possible other special characters could hork the script.
REPLACE=`echo file://$SCRIPTDIR/site | sed -e 's/ /%20/g'`

FILECONTENTS=`cat "$FILENAME"`
MUNGEDFILECONTENTS=`cat "$FILENAME" | sed -e "s,$FIND,$REPLACE,g"`

if [ "x$FILECONTENTS" != "x$MUNGEDFILECONTENTS" ] ; then
	#write result If different
	echo $MUNGEDFILECONTENTS > "$SCRIPTDIR/policy.xml"
	echo "file://$SCRIPTDIR/policy.xml" > "$SCRIPTDIR/policy_url.txt"
	echo "Done. Open up the newly-created policy_url.txt to find the policy url to paste into your update preferences."
else
	echo "Unable to replace site.xml."
	exit -1
fi
