#!/bin/sh

if [ -z "$1" -o -z "$2" ]; then
	echo "Insufficient parameters" > /dev/stderr
	exit 1
fi

IFACE="$1"
ACTION="$2"

DHCP_BIN="/sbin/dhclient"
DHCP_PID="/var/run/dhclient.$IFACE.pid"
DHCP_LEASES="/var/run/dhclient.$IFACE.leases"


case ${ACTION} in

	CONNECTED)
	if [ ! -e "$DHCP_PID" ]; then
		$DHCP_BIN -pf $DHCP_PID -lf $DHCP_LEASES $IFACE
		# Just incase, touch the pidfile
		touch $DHCP_PID
	fi
	exit 0
	;;

	DISCONNECTED)
	if [ -e "$DHCP_PID" ]; then
		$DHCP_BIN -r $IFACE
		rm -f $DHCP_PID
		# keep the leases file
		#rm -f $DHCP_LEASES
	fi
	exit 0
	;;

	*)
	echo "Unknown action \"${ACTION}\"" > /dev/stderr
	exit 1
	;;
esac
