#!/bin/sh

# $1 = URL to watch.
# $2 = File to use for temporary storage of a copy.
# $3 = Program to run (with URL as parameter) when watched page changes.

# RNC, 18 June 2010.

URL=$1
FILEONE=$2
FILETWO=$FILEONE.new
PROGRAM=$3

if [ -f $FILEONE ];
then
	echo "Page previously stored."
	wget -O $FILETWO $URL >/dev/null 2>&1 && ( echo "Web page fetched using wget." ) || ( echo "Failed to fetch web page."; exit 1 )
	cmp $FILEONE $FILETWO && ( echo "No change to page." ) || ( echo "Page changed."; $PROGRAM $URL )
	rm $FILEONE
	mv $FILETWO $FILEONE
else
	echo "Page not previously stored."
	wget -O $FILEONE $URL >/dev/null 2>&1 && ( echo "Web page fetched using wget." ) || ( echo "Failed to fetch web page."; exit 1 )
fi
exit 0
