#!/bin/sh
# ifupdown hook script for enhanced MadWifi (madwifi-ng) support in
# Debian and Debian-based distributions
#
# $Id: zzz_madwifi_dhcpdelay 26 2006-09-27 04:51:42Z mrenzmann $
#
# Copyright (c) 2006 Michael Renzmann <mrenzmann@otaku42.de>
# Portions Copyright (c) 2005 Matt Brown <matt@mattb.net.nz>
#
# For further information see:
# http://projects.otaku42.de/wiki/madwifi-ifupdown
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License version 2 as published by the Free
# Software Foundation.
#
# This is distributed in the hope that it will be useful, but WITHOUT ANY
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public License along with
# the madwifi-ifupdown package; if not, write to the Free Software Foundation,
# Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
LIBDIR=@LIBDIR@
if [ ! -d "$LIBDIR" ]; then
	echo "ERROR: $LIBDIR: no such directory"
	exit 1
fi

if [ ! -f $LIBDIR/defaults ]; then
	echo "ERROR: $LIBDIR/defaults: no such file"
	exit 1
fi
. $LIBDIR/defaults

if [ ! -f "$LIBDIR/functions.inc.sh" ]; then
	echo "ERROR: unable to load functions.inc.sh" >&2
	exit 1
fi
. $LIBDIR/functions.inc.sh


[ -x "$WLANCONFIG" ] || exit 0
[ -x "$IWPRIV" ] || exit 0

[ -n "$IF_ATH_PARENT" ] || exit 0

if [ "$VERBOSITY" -eq "0" ]; then
	exec 1>/dev/null
fi


# make most of the variables lowercase, to ease checking the settings later
IF_ATH_VAPTYPE=$(echo $IF_ATH_VAPTYPE | tr [A-Z] [a-z])
IF_ATH_DHCP_DELAY=$(echo $IF_ATH_DHCP_DELAY | tr [A-Z] [a-z])

# make sure that all specified parameters are valid
if [ "$IFACE" = "$IF_ATH_PARENT" ]; then
	error "interface name is identical to parent device"
fi


if [ -n "$IF_ATH_VAPTYPE" ]; then
	case "$IF_ATH_VAPTYPE" in
	sta|client)
		VAPTYPE=sta
	;;
	*)
		VAPTYPE=other
	;;
	esac
# else use default from $LIBDIR/defaults
fi

if [ -n "$IF_ATH_DHCP_DELAY" ]; then
	case "$IF_ATH_DHCP_DELAY" in
	yes|on)
		DHCP_DELAY=15
	;;
	no|off|0)
		DHCP_DELAY=0
	;;
	*)
		if isdigit $IF_ATH_DHCP_DELAY; then
			DHCP_DELAY=$IF_ATH_DHCP_DELAY
		else
			warning "ath_dhcp_delay $IF_ATH_DHCP_DELAY: invalid value, won't delay"
			DHCP_DELAY=0
		fi
	;;
	esac
# else use default from $LIBDIR/defaults
fi


if [ "$VAPTYPE" == "sta" ] && [ "$METHOD" = "dhcp" ]; then
	if [ "$DHCP_DELAY" -gt "0" ]; then
		echo "Bringing $IFACE up for dhcp_delay"
		$IFCONFIG $IFACE up
		
		echo "Waiting up to $DHCP_DELAY seconds for association of $IFACE..."
		t=0
		while [ "$t" -lt "$DHCP_DELAY" ]; do
			ap=$($IWCONFIG $IFACE | sed -n -e "s/.*\(Access Point: [-0-9a-zA-Z:]*\).*/\1/p" | cut -d" " -f3) # "
			
			if [ -z "$ap" ]; then
				ap="Not-Associated"
			fi
			
			case "$ap" in
			00:00:00:00:00:00|Not-Associated)
				sleep 1
				(( t++ ))
				continue
			;;
			esac
			
			echo "$IFACE successfully associated with $ap, going on now"
			break
		done
	fi
fi

# that's all, folks!
exit 0
