# DHCP (net-misc/dhcp) module for net-scripts
# Version 1.0.2
# Copyright (c) 2004 Gentoo Foundation
# Distributed under the terms of the GNU General Public License V2
# Contributed by Roy Marples (uberlord@gentoo.org)

# Fix any potential localisation problems
# Note that LC_ALL trumps LC_anything_else according to locale(7)
dhclient() {
	LC_ALL=C /sbin/dhclient "$@"
}

# char* dhclient_provides(void)
#
# Returns a string to change module definition for starting up
dhclient_provides() {
	echo "dhcp"
}

# void dhclient_depend(void)
#
# Sets up the dependancies for the module
dhclient_depend() {
	after interface
	need interface
}

# bool dhclient_check_installed(void)
#
# Returns 1 if dhclient is installed, otherwise 0
dhclient_check_installed() {
	[[ -x /sbin/dhclient ]] && return 0
	[[ ${1} == true ]] && eerror "For DHCP (dhclient) support, emerge net-misc/dhcp"
	return 1
}

# char* dhclient_get_script(void)
#
# Returns the filename of the script to run
dhclient_get_script() {
	local module=$( interface_module )
	echo "/lib/rcscripts/net.modules.d/helpers.d/dhclient-${module}"
}

# bool dhclient_check_depends(void)
#
# Checks to see if we have the needed functions
dhclient_check_depends() {
	local f

	for f in get_device interface_is_up interface_get_address interface_module; do
		[[ $(type -t ${f}) == function ]] && continue
		eerror "dhclient: missing required function ${f}\n"
		return 1
	done

	return 0
}

# char* dhclient_get_vars(char *interface)
#
# Returns a string spaced with possible user set
# configuration variables
dhclient_get_vars() {
	echo "dhclient_${1} peer_dns_${1} peer_nis_${1} peer_ntp_${1}"
}

# bool dhclient_stop(char *iface)
#
# Stop DHCP on an interface by calling dhclient -z $iface
#
# Returns 0 (true) when a DHCP address dropped
# otherwise return 1
dhclient_stop() {
	local iface=${1} count script=$( dhclient_get_script )
	
	dhclient_check_installed || return 1

	# We check for a dhclient process first as if we attempt to release
	# an interface for which dhclient has obtained an IP in the past
	# it causes a "RELEASE" event anyway.
	local r=$( ps -fC dhclient | awk "/-q ${iface}/ {print 1}")
	[[ -z ${r} ]] && return 1

	ebegin "Releasing DHCP lease for ${iface}"
	r=$( dhclient -sf ${script} -r ${iface} -q )
	[[ -z ${r} ]] && return 1

	if [[ ${r} == RELEASE ]]; then
		eend 0
	else
		ewarn "dhclient returned a ${r}"
		eend 1
	fi
	return 0
}

# bool dhclient_start(char *iface)
#
# Start DHCP on an interface by calling dhclient $iface $options
#
# Returns 0 (true) when a DHCP address is obtained, otherwise 1
dhclient_start() {
	local iface=${1} opts

	! interface_exists ${iface} true && return 1

	# Check that iface was not brought up by the kernel ...
	local check=$( get_device ${iface} )
	if [[ ${check} == ${iface} ]] && interface_is_up ${iface} true ; then
		einfo "Keeping kernel configuration for ${iface}"
		return 0
	fi

	local script=$( dhclient_get_script )

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhclient"
	eval opts=\"\$\{dhclient_${iface}\}\"
	local r=$( dhclient ${opts} -sf ${script} -q ${iface} )
	# We just check the last 5 letters
	[[ ${r:${#r} - 5:5} == BOUND ]]
	eend $? "dhclient returned a ${r}" || return 1

	# DHCP succeeded, show address retrieved
	local addr=$( interface_get_address ${iface} )
	einfo "${iface} received address ${addr}"

	return 0
}
