#!/bin/sh

# PROVIDE: dhcrelay
# REQUIRE: DAEMON
# KEYWORD: shutdown
#
# Add the following line to /etc/rc.conf to enable dhcrelay:
#
# dhcrelay_enable="YES"

# override these variables in /etc/rc.conf
dhcrelay_enable=${dhcrelay_enable:-"NO"}
dhcrelay6_enable=${dhcrelay6_enable:-"NO"}
# dhcrelay_flags=""			# command option(s)
# dhcrelay_servers=""			# v4 dhcrelay server(s)
# dhcrelay_ifaces=""			# v4 ethernet interface(s)
# dhcrelay6_lowers=""			# v6 "lower" network interface(s)
# dhcrelay6_uppers=""			# v6 "upper" network interface(s)


case $0 in
/etc/rc*)
	# during boot (shutdown) $0 is /etc/rc (/etc/rc.shutdown),
	# so get the name of the script from $_file
	name=$_file
	;;
*)
	name=$0
	;;
esac

name=${name##*/isc-}

case ${name} in
*6)
	ipversion=-6
	;;
*)
	ipversion=-4
	;;
esac

dhcrelay_precmd ()
{
	local ifaces _servers _ifaces

	eval "_servers=\${${name}_servers}"
	eval "_ifaces=\${${name}_ifaces}"

	if [ -z "${_servers}" ]; then
		err 1 "no ${name} server(s) configured."
	fi

	ifaces=
	for iface in ${_ifaces}; do
		ifaces="${ifaces} -i ${iface}"
	done

	rc_flags="${rc_flags} ${ipversion} ${ifaces} ${_servers}"
}

dhcrelay6_precmd ()
{
	local lowers _lowers uppers _uppers

	eval "_lowers=\${${name}_lowers}"
	eval "_uppers=\${${name}_uppers}"

	if [ -z "${_lowers}" ]; then
		err 1 "no ${name} lower(s) configured."
	fi

	if [ -z "${_uppers}" ]; then
		err 1 "no ${name} upper(s) configured."
	fi

	lowers=
	for lower in ${_lowers}; do
		lowers="${lowers} -l ${lower}"
	done

	uppers=
	for upper in ${_uppers}; do
		uppers="${uppers} -u ${upper}"
	done

	rc_flags="${rc_flags} ${ipversion} ${lowers} ${uppers}"
}

. /etc/rc.subr

rcvar=${name}_enable

command=%%PREFIX%%/sbin/dhcrelay
pidfile=/var/run/${name}.pid
start_precmd=${name}_precmd

load_rc_config ${name}
run_rc_command "$1"
