# 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/essidnet,v 1.7.4.6 2005/04/19 16:28:46 uberlord Exp $

# Contributed by Roy Marples (uberlord@gentoo.org)
# Many thanks to all the people in the Gentoo forums for their ideas and
# motivation for me to make this and keep on improving it

# Load our config if it exists
[[ -f /etc/conf.d/wireless ]] && source /etc/conf.d/wireless

# void essidnet_depend(void)
#
# Sets up the dependancies for the module
essidnet_depend() {
	before interface
	after wireless
	installed wireless
}

# bool essidnet_check_installed(void)
#
# Always returns 0 as we are "installed" by wireless in the depend function
essidnet_check_installed() {
	return 0
}

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

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

	for f in interface_variable wireless_check_extensions wireless_get_essid wireless_get_ap_mac_address; do
		[[ $( type -t ${f} ) == function ]] && continue
		eerror "essidnet: missing required function ${f}\n"
		return 1
	done

	return 0
}

# bool essidnet_start(char *iface)
#
# All interfaces and module scripts expose modulename_get_vars
# which returns a space seperated list of user configuration variables
# We can override each variable here from a given ESSID or the MAC
# of the AP connected to. MAC configuration takes precedence
# Always returns 0
essidnet_pre_start() {
	local iface=${1} ifvar=$( interface_variable ${1} )

	wireless_check_extensions ${iface} || return 0

	local mod func evars evar vars x i mvars mac=$( wireless_get_ap_mac_address ${iface} )
	local ESSID=$( wireless_get_essid ${iface} )
	local essid
	LC_ALL=C essid=${ESSID//[![:word:]]/_}
	mac=${mac//:/}

	vebegin "Configuring ${iface} for \"${ESSID//\\\\/\\\\}\"" 2>/dev/null

	for mod in ${MODULES[@]}; do
		func="${mod}_get_vars"
		if [[ $(type -t ${func}) == function ]]; then
			evars=( $( ${func} ${essid} ) )
			mvars=( $( ${func} ${mac} ) )
			vars=( $( ${func} ${ifvar} ) )
			for ((i = 0; i<${#mvars[@]}; i++)); do
				eval x=( \"\$\{mac_${mvars[i]}\[@\]\}\" )
				[[ -z ${x} ]] && eval x=( \"\$\{${evars[i]}\[@\]\}\" )
				[[ -n ${x} ]] && eval "${vars[i]}=( "\"\$\{x\[@\]\}\"" )"
			done
		fi
	done

	# Nameserver setup for the essid if required
	local -a nameservers=( \"\$\{mac_dns_servers_${mac}\[@\]\}\" )
	[[ -z ${nameservers} ]] && eval nameservers=( \"\$\{dns_servers_${essid}\[@\]\}\" )
	if [[ -n ${nameservers} ]]; then
		# Make a backup of the origional file if necessary
		[[ -f /etc/resolv.conf ]] && mv /etc/resolv.conf /etc/resolv.conf.sv

		echo "# Generated by net-scripts essidnet module" > /etc/resolv.conf
		chmod 644 /etc/resolv.conf

		eval x=\"\$\{mac_dns_domain_${mac}\}\"
		[[ -z ${x} ]] && eval x=\"\$\{dns_domain_${essid}\}\"
		[[ -n ${x} ]] && echo "domain ${x}" >> /etc/resolv.conf

		for x in ${nameservers[@]}; do
			echo "nameserver ${x}" >> /etc/resolv.conf
		done

		eval x=\"\$\{mac_dns_search_domains_${mac}\}\"
		[[ -z ${x} ]] && eval x=\"\$\{dns_search_domains_${essid}\}\"
		[[ -n ${x} ]] && echo "search ${x}" >> /etc/resolv.conf
	fi

	# Backwards compat for old gateway var
	eval x=\"\$\{gateway_${essid}\}\"
	[[ -n ${x} ]] && gateway="${iface}/${x}"

	veend 0 2>/dev/null
	return 0
}
