#!/bin/sh
#
# Public Domain, 1999 David McKellar <damc@sympatico.ca>
#
# This script is made do be run from cron frequently (eg every 5 minutes)
# if your IP address has changed it does nothing otherwise it updates
# it using ddclient.  Should be run as root.
# It's slicker to set up a script to be run when dhcp changes your IP address but I couldn't make that work.
#
# Use crontab -e to add this:
#
#	0-59/5 * * * * /usr/local/bin/ddupdate
#

# You can change the configuration here
# FILE: The file where you want ddupdate to keep the current ip address
# PROXY_HOST: If your ISP demands that you use a proxy for web access put it here
#             Bell Canada Sympatico users put fasthttp.sympatio.ca here
#	      If you don't need a proxy just leave it blank
# RESTART_SAMBA: Do you want Samba restarted when the IP address changes
FILE=/etc/dd_ip_address
#PROXY_HOST=fasthttp.sympatico.ca
RESTART_SAMBA=yes

# Get the current and old host names
CURRENT_IP=`hostname --ip-address | tr -d ' '`
OLD_IP=`cat $FILE 2>/dev/null`

# Update if it's changed
if [ "$CURRENT_IP" != "$OLD_IP" ]; then
	if [ -n "$PROXY_HOST" ]; then
		PROXY="-proxy $PROXY_HOST"
	fi
	echo "IP address has changed from $OLD_IP to $CURRENT_IP" | logger -t ddupdate
	/bin/ddclient $PROXY -wildcard -ip $CURRENT_IP | logger -t ddupdate
	echo $CURRENT_IP > $FILE

	if [ "$RESTART_SAMBA" = "yes" ]; then
		/etc/rc.d/init.d/smb restart
	fi
else
	# This is debug and will be removed
	echo "IP address has not changed - still $CURRENT_IP" | logger -t ddupdate
fi
