#!/bin/sh
#
# Debian ifupdown hook script for madwifi-ng
#
# Author:   Matt Brown <matt@mattb.net.nz>
# Modified by: Kel Modderman <kelrin@tpg.com.au>
#
# Copyright (C) 2005 - 2006    Matt Brown
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free Software
# Foundation; either version 2 of the License, or (at your option) any later
# version.
#
# 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.
#
# On Debian GNU/Linux systems, the text of the GPL license can be found in
# /usr/share/common-licenses/GPL


WLANCONFIG=/sbin/wlanconfig

if [ ! -x "$WLANCONFIG" ]; then
	exit 0
fi

if [ ! -n "$IF_MADWIFI_BASE" ]; then
	exit 0
fi

if [ "$VERBOSITY" = "1" ]; then
	TO_NULL="/dev/stdout"
else
	TO_NULL="/dev/null"
fi

if [ -n "$IF_MADWIFI_MODE" ]; then
	MODE="$IF_MADWIFI_MODE"
elif [ -n "$IF_WIRELESS_MODE" ]; then
	MODE="$IF_WIRELESS_MODE"
else
	exit 0
fi

if [ -n "$IF_MADWIFI_VAP" ]; then
	VAP="$IF_MADWIFI_VAP"
else
	VAP="$IFACE"
fi

if [ -d /proc/sys/net/$VAP ]; then
	$WLANCONFIG $VAP destroy
fi

case "$MODE" in
	Managed|managed|MANAGED)
		MODE="sta"
		;;
	Ad-Hoc|ad-hoc|AD-HOC)
		MODE="adhoc"
		;;
	Master|master|MASTER)
		MODE="ap"
		;;
	Monitor|monitor|MONITOR)
		MODE="monitor"
		;;
	sta|ap|wds|adhoc|ahdemo|monitor)
		MODE="$MODE"
		;;
	*)
		MODE="sta"
		;;
esac

if [ -n "$IF_MADWIFI_OPTIONS" ]; then
	$WLANCONFIG $VAP create wlandev $IF_MADWIFI_BASE wlanmode $MODE "$IF_MADWIFI_OPTIONS" >$TO_NULL
else
	$WLANCONFIG $VAP create wlandev $IF_MADWIFI_BASE wlanmode $MODE >$TO_NULL
fi

exit $?
