#!/bin/bash

#####################################################
## hpUpdateNotify ###################################
#####################################################
# Checks for changed websites and notifies via mail #
#####################################################

# where do you want to keep your files?
cwd="~/hpUpdateNotify/"

# add any homepage (its name) you want to be notified about
files=( "foobar" )

# and just put the address in the array below at the corresponding index
addresses=( "http://www.foobar.com" )

lastIndex=`expr ${#addresses[@]} - 1` # do not change
suffix=".html"

# who should be notified? eg. "foo@bar.de spam@eggs.com"
mailto="foo@bar.de"

mailContent=""

for i in $(seq 0 $lastIndex); do
    date=`date`
    file=${files[$i]}
    address=${addresses[$i]}
    checksum="0"
    if [ -f $cwd$file$suffix ]; then
	checksum=`md5sum $cwd$file$suffix` &&
	    rm $cwd$file$suffix
    fi
    wget -o /dev/null --output-document=$cwd$file$suffix $address &&
	newchecksum=`md5sum $cwd$file$suffix` &&
	if [ "$newchecksum" = "$checksum" ]; then
	    echo $date $file  "nothing updated"
	elif [ "$checksum" = "0" ]; then
	    echo $date $file$suffix " created"
	else
	    echo $date $file "has changed"
	    mailContent=$mailContent$date" "$file"( "$address" ) has changed \n\n"
	fi
done

if [ "$mailContent" != "" ]; then
    for recipient in $mailto; do
	echo -e $mailContent|mutt -s "[HP Updates]" $recipient && echo "mail to "$recipient" successfully sent"
    done
fi