# 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/dhcpcd,v 1.19.2.4 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)
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
}

# bool dhcpcd_check_installed(void)
#
# Returns 1 if dhcpcd is installed, otherwise 0
dhcpcd_check_installed() {
	[[ -x /sbin/dhcpcd ]] && return 0
	${1:-false} && 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 interface_variable interface_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} dhcp_${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 signal pidfile="/var/run/dhcpcd-${1}.pid" dhcp
	
	dhcpcd_check_installed || return 0

	[[ ! -f ${pidfile} ]] && return 0

	ebegin "Stopping dhcpcd on ${iface}"
	local ifvar=$( interface_variable ${1} )
	local pid=$( cat ${pidfile} )

	eval dhcp=\" \$\{dhcp_${ifvar}\} \"
	if [[ ${dhcp} == *' release '* ]]; then
		signal="HUP"
	else
		signal="TERM"
	fi

	kill -s ${signal} ${pid} &>${devnull}
	process_finished ${pid} dhcpcd
	eend $? "timed out"
	return $?
}

# 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 pidfile="/var/run/dhcpcd-${1}.pid" dhcp
	local ifvar=$( interface_variable ${iface} )

	interface_exists ${iface} true || return 1

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

	# Map some generic options to dhcpcd
	eval dhcp=\" \$\{dhcp_${ifvar}\} \"
	[[ ${dhcp} == *' nodns '* ]] && opts="${opts} -R"
	[[ ${dhcp} == *' nontp '* ]] && opts="${opts} -N"
	[[ ${dhcp} == *' nonis '* ]] && opts="${opts} -Y"

	# We transmit the hostname by default
	if [[ ${opts} != *'-h '* ]]; then
		hostname=$( hostname )
		[[ -n ${hostname} && ${hostname} != "(none)" && ${hostname} != localhost ]] \
			&& opts="-h ${hostname} ${opts}"
	fi

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

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

	eval "dhcpcd ${opts} ${iface}"
	eend $? || return 1

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

	return 0
}
