#!/bin/sh

# Source function library
. /etc/init.d/functions

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DESC=udhcpc
DAEMON=udhcpc
IFACE=eth0
PIDFILE_BASE=/var/run/udhcpc
PIDFILE=${PIDFILE_BASE}.$IFACE.pid

if [ -e /etc/default/vlans ];then
	source /etc/default/vlans
fi;


set -e

case $1 in
	start)
		echo -n "Starting $DESC: "
		(start-stop-daemon --start -p $PIDFILE $DAEMON -- -R -b -p /var/run/udhcpc.$IFACE.pid -i $IFACE) | :
		if [ "${VLANS}" ];then
			for vlanid in ${VLANS};do
				(start-stop-daemon --start -p ${PIDFILE_BASE}.${IFACE}_${vlanid}.pid $DAEMON -- -R -b -p ${PIDFILE_BASE}.${IFACE}_${vlanid}.pid -i ${IFACE}.${vlanid}) | :
			done;
		fi;
	;;
	stop)
		echo -n "Stopping $DESC: "
		if [ -e $PIDFILE ];then
			(start-stop-daemon --stop -s SIGKILL -p $PIDFILE $DAEMON) | :
			rm $PIDFILE
		fi;
		if [ "${VLANS}" ];then
			for vlanid in ${VLANS};do
				if [ -e ${PIDFILE_BASE}.${IFACE}_${vlanid}.pid ];then
					(start-stop-daemon --stop -s SIGKILL -p ${PIDFILE_BASE}.${IFACE}_${vlanid}.pid) | :
					rm ${PIDFILE_BASE}.${IFACE}_${vlanid}.pid
				fi;
			done;
		fi;
	;;
	restart|force-reload)
		$0 stop
		sleep 1
		$0 start
	;;
	status)
		status ${DAEMON} || exit $?
	;;
	*)
		N=/etc/init.d/udhcpc
		echo "Usage: $N {start|stop|restart|force-reload|status}" >&2
		exit 1
	;;
esac

exit 0
