# config-system 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)

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

need_hostname() {
	local n=$( hostname )
	[[ ${n} == "(none)" || ${n} == "localhost" ]]
	return $?
}

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
}

get_wireless_vars() {
	# Check that wireless has not already been configured
	[[ $(type -t wireless_check_extensions) == function ]] && return

	# Check we can actually load the file
	# and our calling dhcp module
	[[ ! -f ${MODULES_DIR}/iwconfig || ! -f ${MODULES_DIR}/essidnet || ! -f ${MODULES_DIR}/${module} ]] && return

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

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

	# Check we are installed
	wireless_check_installed || return

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

	# Wait until we get a valid ESSID
	ESSID=$( wireless_get_essid ${interface} )

	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.new
		chmod 644 /etc/resolv.conf.new
		[[ -n ${domain} ]] && echo "domain ${domain}" >> /etc/resolv.conf.new

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

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

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

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

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

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

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

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

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