# 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/helpers.d/config-system,v 1.15.2.2 2005/01/25 10:42:54 uberlord Exp $

# Contributed by Roy Marples (uberlord@gentoo.org)

source /etc/conf.d/net
[[ -z ${MODULES_DIR} ]] && MODULES_DIR=/lib/rcscripts/net.modules.d

if [[ $( type -t interface_variable ) != function ]]; then
	source ${MODULES_DIR}/helpers.d/functions
fi

# bool need_hostname(void)
#
# Returns a 0 if no hostname is already set
# Otherwise 1
need_hostname() {
	local n=$( hostname )
	[[ ${n} == "(none)" || ${n} == "localhost" ]]
	return $?
}

# void restore_configs(void)
#
# Restores saved configuration when the DHCP client closes down
restore_configs() {
	[[ -f /etc/resolv.conf.sv ]] && mv /etc/resolv.conf.sv /etc/resolv.conf
	[[ -f /etc/ntp.conf.sv ]] && mv /etc/ntp.conf.sv /etc/ntp.conf
	[[ -f /etc/yp.conf.sv ]] && mv /etc/yp.conf.sv /etc/yp.conf
}

# void get_wireless_vars(void)
#
# Configures variables depending on SSID connected to
get_wireless_vars() {
	# Check that wireless has not already been configured
	[[ $(type -t wireless_check_extensions) == function ]] && return

	# Check we can load up the basics
	[[ ! -f ${MODULES_DIR}/essidnet || ! -f ${MODULES_DIR}/${module} ]] && return

	local m x
	# Lets do this the easy way - iwconfig is what we need as it supports
	# getting all the information we need regardless of how it was setup
	if [[ -f /usr/sbin/iwconfig ]]; then
		m=iwconfig
	else
		# This is hairy. We need to know if a supplicant is installed
		# and running on the interface
		if [[ -f /usr/sbin/wpa_supplicant && -S /var/run/wpa_supplicant/${interface} ]]; then
			m=wpa_supplicant
		fi
	fi
	[[ -z ${m} ]] && return

	source ${MODULES_DIR}/${m}
	for x in $( typeset -f | grep -o ^${m}_'[^ ]*' ); do
		eval "wireless${x#${m}}() { ${x} \"\$@\"; }"
	done

	# Setup the devnull var as we probably aren't being called via net.lo
	[[ -z ${devnull} ]] && local devnull=/dev/null

	# Check we are a wireless interface
	wireless_check_extensions ${interface} || return

	local -a MODULES=( "${module}" )
	source ${MODULES_DIR}/${module}
	source ${MODULES_DIR}/essidnet

	essidnet_pre_start ${interface}
}

# This routine configures the local host for non-ip and routing info
# from a DHCP client
config_system() {			
	# Load our wireless mapped vars
	# For example dhcp_dns_ESSID becomes dhcp_dns_eth0
	get_wireless_vars

	local dhcp ifvar=$( interface_variable ${interface} )
	if need_hostname ; then
		[[ -n ${hostname} ]] && hostname ${hostname} >/dev/null
	fi

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

	if [[ ${dhcp} != *' nodns'* ]] && [[ -n ${domain} || -n ${dns} ]]; then
		echo "# generated automatically by net-scripts" > /etc/resolv.conf.tmp
		chmod 644 /etc/resolv.conf.tmp
		[[ -n ${domain} ]] && echo "domain ${domain}" >> /etc/resolv.conf.tmp

		for x in ${dns}; do
			echo "nameserver ${x}" >>/etc/resolv.conf.tmp
		done

		[[ -f /etc/resolv.conf && ! -f /etc/resol.conf.sv ]] && mv /etc/resolv.conf /etc/resolv.conf.sv
		mv /etc/resolv.conf.tmp /etc/resolv.conf
	fi

	if [[ ${dhcp} != *' nontp '* ]] && [[ -n ${ntpsrv} ]]; then
		echo "# generated automatically by net-scripts" > /etc/ntp.conf.tmp
		chmod 644 /etc/ntp.conf.tmp

		echo "restrict default noquery notrust nomodify" >> /etc/ntp.conf.tmp
		echo "restrict 127.0.0.1" >> /etc/ntp.conf.tmp
		echo "driftfile /var/lib/ntp/ntp.drift" >> /etc/ntp.conf.tmp

		for x in ${ntpsrv}; do
			echo "restrict ${x} nomodify notrap noquery" >> /etc/ntp.conf.tmp
			echo "server ${x}" >> /etc/ntp.conf.tmp
		done

		[[ -f /etc/ntp.conf && ! -f /etc/ntp.conf.sv ]] && mv /etc/ntp.conf /etc/ntp.conf.sv
		mv /etc/ntp.conf.tmp /etc/ntp.conf
	fi

	if [[ ${dhcp} != *' nonis '* ]] && [[ -n ${nisdomain} || -n ${nissrv} ]]; then
		echo "# generated automatically by net-scripts" > /etc/yp.conf.tmp
		chmod 644 /etc/yp.conf.tmp

		if [[ -n ${nisdomain} ]]; then
			hostname -y ${nisdomain}
			if [[ -n ${nissrv} ]]; then
				for x in ${nissrv}; do
					echo "domain ${nisdomain} server ${x}" >> /etc/yp.conf.tmp
				done
			else
				echo "domain ${nisdomain} broadcast" >> /etc/yp.conf.tmp
			fi
		else
			for x in ${nissrv}; do
				echo "ypserver ${x}" >> /etc/yp.conf.tmp
			done
		fi

		[[ -f /etc/yp.conf && ! -f /etc/yp.conf.sv ]] && mv /etc/yp.conf /etc/yp.conf.sv
		mv /etc/yp.conf.tmp /etc/yp.conf
	fi
}
