#!/bin/sh
#
# $NetBSD: dhclient,v 1.8 2002/03/22 04:33:58 thorpej Exp $
# $FreeBSD: src/etc/rc.d/dhclient,v 1.20.2.4 2007/03/10 14:07:01 yar Exp $
#

# PROVIDE: dhclient
# REQUIRE: FILESYSTEMS
# KEYWORD: nojail nostart

. /etc/rc.subr
. /etc/network.subr

name="dhclient"
rcvar=
start_precmd="dhclient_prestart"
stop_precmd="dhclient_precheck"
stop_cmd="dhclient_stop"

ifn="$2"
pidfile="/var/run/${name}.${ifn}.pid"

# $rc_force check can only be done at the "run_rc_command" phase,
# so we're testing it in the pre* hooks.
dhclient_precheck()
{
	if [ -z "$rc_force" ] && ! dhcpif $ifn; then
		local msg="'$ifn' is not a DHCP-enabled interface"
		if [ -z "$rc_quiet" ]; then
			info "$msg"
		else
			debug "$msg"
		fi
		exit 1
	fi
}

dhclient_prestart()
{
	dhclient_precheck

	# Override for $ifn specific flags (see rc.subr for $flags setting)
	local specific=$(get_if_var $ifn dhclient_flags_IF)
	if [ -z "$flags" -a -n "$specific" ]; then
		rc_flags=$specific
	fi

	rc_flags="$rc_flags $ifn"
}

dhclient_stop()
{
	# Killing the pid couldn't stop the child processes, so use the
	# "-x" option to let dhclient kill them.
	${dhclient_program} -x $ifn
}

load_rc_config $name
load_rc_config network

# Only complain if a command was specified but no interface.
if [ -n "$1" ] && [ -z "$ifn" ]; then
	err 1 "no interface specified"
fi

run_rc_command "$1"
