# DHCP (net-misc/dhcpcd) module for net-scripts
# Version 1.0.3
# 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)
dhcpcd() {
	LC_ALL=C /sbin/dhcpcd "$@"
}

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

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

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

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

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

	return 0
}

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

# bool dhcpcd_stop(char *iface)
#
# Stop DHCP on an interface by calling dhcpcd -z $iface
#
# Returns 0 (true) when a DHCP address dropped
# otherwise return 1
dhcpcd_stop() {
	local iface=${1} count
	
	dhcpcd_check_installed || return 1
	if ! dhcpcd -z ${iface} &>${devnull}; then
		return 1
	fi

	ebegin "Releasing DHCP lease for ${iface}"
	for ((count = 0; count < 9; count = count + 1)); do
		dhcpcd -z ${iface} &>${devnull} || break
		sleep 1
	done
	[[ ${count} -lt 9 ]]
	eend $? "timed out"

	# Remove stale PID file
	[[ -f /var/run/dhcpcd-${iface}.pid ]] && rm -f /var/run/dhcpcd-${iface}.pid

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

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

	! interface_exists ${iface} true && return 1

	local check=$( get_device ${iface} )

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

	# Remove stale PID file
	[[ -f /var/run/dhcpcd-${iface}.pid ]] && rm -f /var/run/dhcpcd-${iface}.pid

	# Get our options
	eval opts=\"\$\{dhcpcd_${iface}\}\"

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

	eval peer=\"\$\{peer_nis_${iface}\}\"
	[[ -z ${peer} ]] && eval peer=\"\$\{peer_nis\}\"
	[[ ${peer} == no ]] && opts="-Y ${opts}"

	eval peer=\"\$\{peer_ntp_${iface}\}\"
	[[ -z ${peer} ]] && eval peer=\"\$\{peer_ntp\}\"
	[[ ${peer} == no ]] && opts="-N ${opts}"

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

	# Bring up DHCP for this interface (or alias)
	ebegin "Running dhcpcd"
	dhcpcd ${opts} ${iface}
	eend $? || return 1

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

	return 0
}
