#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          dhcpcd
# Required-Start:    $remote_fs $local_fs
# Required-Stop:     $remote_fs $local_fs
# Should-Start: 
# Should-Stop:
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: IPv4 DHCP client with IPv4LL support
# Description:
### END INIT INFO

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DHCPCD=/sbin/dhcpcd
NAME=dhcpcd
PIDFILE=/var/run/dhcpcd.pid

test -x $DHCPCD || exit 0

INTERFACES=/etc/network/interfaces

. /lib/lsb/init-functions

sanity()
{
	local x=

	case "$($DHCPCD --version)" in
	[1234].*)
		log_failure_msg "Not running $NAME because an older version" \
			"is currently preferred"
		exit 6
	esac

	for x in /var/run/dhcpcd-*.pid; do
		[ -f "$x" ] || continue
		log_failure_msg "Not running $NAME because there is aleady an" \
			"interface specific instance"
		log_failure_msg "$x"
		exit 6
	done
		
	if grep -q "^[[:space:]]*iface[[:space:]]*.*[[:space:]]*inet[[:space:]]*dhcp" \
	$INTERFACES; then
		log_failure_msg "Not running $NAME because $INTERFACES"
		log_failure_msg "defines some interfaces that will use a" \
			"DHCP client"
		exit 6
	fi
}

case "$1" in
start)
	sanity
	if pidofproc -p $PIDFILE $DHCPCD >/dev/null; then
		log_warning_msg "$NAME is already running"
		exit 0
	fi
	$DHCPCD
        ;;
stop)
	sanity
	$DHCPCD -x
	;;
restart|force-reload)
	sanity
	$DHCPCD -x
	$DHCPCD
        ;;
try-restart)
	if ! pidofproc -p $PIDFILE $DHCPCD >/dev/null; then
		log_warning_msg "$NAME is not running"
	else
		sanity
		$DHCPCD -x
		$DHCPCD
	fi
	;;
reload)
	if ! pidofproc -p $PIDFILE $DHCPCD >/dev/null; then
		log_failure_msg "$NAME is not running"
		exit 7
	fi
	sanity
	$DHCPCD -n
        ;;
status)
        status_of_proc -p $PIDFILE $DHCPCD "$NAME" || exit $?
        ;;
*)
        log_failure_msg "Usage: /etc/init.d/dhcpcd {start|stop|restart|try-restart|force-reload|status}"
	exit 1
        ;;
esac
