# Copyright (c) 2004-2005 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: /var/cvsroot/gentoo-src/rc-scripts/net-scripts/net.modules.d/dhclient,v 1.18.2.3 2005/01/25 10:42:54 uberlord Exp $

# 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
}

# bool dhclient_check_installed(void)
#
# Returns 1 if dhclient is installed, otherwise 0
dhclient_check_installed() {
	[[ -x /sbin/dhclient ]] && return 0
	${1:-false} && 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 "${MODULES_DIR}/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 interface_variable interface_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} dhcp_${1}"
}

# bool dhclient_stop(char *iface)
#
# Stop dhclient on an interface
# Always returns 0
dhclient_stop() {
	local iface=${1} dhcp release pidfile="/var/run/dhclient-${1}.pid"
	
	dhclient_check_installed || return 0
	[[ ! -f ${pidfile} ]] && return 0

	# 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 pid=$( cat ${pidfile} )
	local script=$( dhclient_get_script )

	eval dhcp=\" \$\{dhcp_${ifvar}\} \"

	ebegin "Stopping dhclient on ${iface}"
	if [[ ${dhcp} == *' release '* ]]; then
		local r=$( dhclient -q -r -sf ${script} -pf ${pidfile} ${iface} )
		[[ ${r} == deconfig ]]
		eend $? "dhclient returned a ${r}"
	else
		kill -s TERM ${pid} 2>/dev/null
		clean_pidfile ${pidfile}
		eend 0
	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 pidfile="/var/run/dhclient-${1}.pid"
	local ifvar=$( interface_variable ${iface} ) x
	local cffile="/etc/dhclient.conf"

	interface_exists ${iface} true || return 1

	local script=$( dhclient_get_script )

        # Load our options
	eval opts=\" \$\{dhclient_${ifvar}\} \"

	# Work out our cffile
	x="${opts##* -cf }"
	if [[ ${x} != ${opts} ]]; then
	    x="${x%% *}"
	    if [[ -n ${x} ]]; then
		cffile="${x}"
		opts="${opts//-cf ${cffile}/}"
	    fi
	fi
	opts="${opts} -cf ${cffile}"

	# Ensure that the cffile does not contain any script lines
	# as that will stop our helpers from running
	if [[ -e ${cffile} ]] ; then
	    if grep -q "^[ \t]*script " "${cffile}" ; then
                eerror "You have to remove the script parameter from ${cffile}"
                return 1
            fi
        fi

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhclient"

	if ! clean_pidfile ${pidfile} ; then
		ewarn "dhclient is already running on ${iface}"
		eend 0
		return 0
	fi

	eval opts=\"\$\{dhclient_${ifvar}\}\"
	local x=$( dhclient ${opts} -1 -sf ${script} -pf ${pidfile} -q ${iface} 2>&1 )

	# We just check the last 5 letters
	[[ ${x:${#x} - 5:5} == bound ]]
	if [[ $? != 0 ]]; then
		echo "${x}" >&2
		# We need to kill the process if we fail
		kill -s TERM $( < ${pidfile} ) 2>/dev/null
		eend 1
		return 1
	fi
	eend 0

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

	return 0
}
