#!/bin/dash
# Startup script for dynamic DNS updater.
# Wait for IP address changes and then update the DNS server with new info.
#
# Copyright (C) James Budiono 2013
# License: GPL Version 3 or later.
#
# Start as early as possible to catch the first "ifup" event

### configuration
INTERFACES="wlan0 eth0"            # interfaces to track, separate with spaces
DYNDNS_SERVER="192.168.1.99 9000"  # dyndns server port - must match server's
PASSWD=woofwoof                    # password (cannot contain spaces)
[ -e /etc/local-dyndns.conf ] && . /etc/local-dyndns.conf
PIDFILE=/var/run/dyndns-client.pid

start() {
	ipmon | while read iface addr; do
		for p in $INTERFACES; do
			if [ $p = $iface ]; then
				echo add $PASSWD $(hostname) $addr | nc $DYNDNS_SERVER
			fi
		done
	done
}

stop() {
	if [ -e $PIDFILE ]; then
		kill $(cat $PIDFILE)
		rm $PIDFILE
	fi
}

status() {
	[ -e $PIDFILE ] && echo "${0##*/} is running" || echo "${0##*/} is stopped"
}

case $1 in
	start|restart) 
		stop; sleep 1;
		start & 
		echo $! > $PIDFILE
		;;
	stop) stop ;;
	status) status ;;
esac
