# Pump 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)
pump() {
	LC_ALL=C /sbin/pump "$@"
}

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

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

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

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

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

	return 0
}

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

# bool pump_stop(char *iface)
#
# Stop pump on an interface by calling pumpcd -z $iface
#
# Returns 0 (true) when a dhcp address dropped
# otherwise return 1
pump_stop() {
	local iface=${1} count e
	
	pump_check_installed || return 1

	# We check for a pump process first as querying for status
	# causes pump to spawn a process
	e=$( ps -C pump | grep pump )
	[[ -z ${e} ]] && return 1

	e=$( pump --status --interface ${iface} 2>${devnull} | grep ${iface})
	[[ -z ${e} ]] && return 1
	
	ebegin "Releasing DHCP lease for ${iface}"
	for ((count = 0; count < 9; count = count + 1)); do
		e=$( pump --release --interface ${iface} 2>${devnull} )
		[[ -z ${e} ]] && break
		sleep 1
	done
	[[ ${count} -lt 9 ]]
	eend $? "Timed out"

	return 0  # we did *attempt* to stop pump
}

# bool pump_start(char *iface)
#
# Start pump on an interface by calling pumpcd $iface $options
#
# Returns 0 (true) when a dhcp address is obtained, otherwise
# the return value from pump
pump_start() {
	local iface=${1} opts hostname

	! 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

	eval opts=\"\$\{pump_${iface}\}\"

	# Map some generic options to pump
	eval peer=\"\$\{peer_dns_${iface}\}\"
	[[ -z ${peer} ]] && eval peer=\"\$\{peer_dns\}\"
	[[ ${peer} == no ]] && opts="--no-dns ${opts}"

	# We transmit the hostname by default	
	hostname=$( hostname )
	[[ -n ${hostname} ]] && opts="--hostname=${hostname%%.*} ${opts}"

	# Bring up DHCP for this interface (or alias)
	ebegin "Running pump"
	pump ${opts} --win-client-ident --interface ${iface} 2>${devnull}
	eend $? || return $?

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

	eval peer=\"\$\{peer_ntp_${iface}\}\"
	[[ -z ${peer} ]] && eval peer=\"\$\{peer_ntp\}\"
	if [[ ${peer} != no ]]; then
		export new_ntp_servers=$( pump -i ${iface} --status 2>/dev/null | awk '/Ntpservers/ {print $2}' 2>/dev/null )
		source /lib/rcscripts/net.modules.d/helpers.d/config-system
		config_system
	fi

	return 0
}
