#! /bin/sh
#
# wlan 1.1 by Mark S. Mathews
#
# Most of this file was  copied from the 'network' script written
# by David Hinds, author of the Linux PCMCIA package.
#
# Initialize or shutdown a PCMCIA 802.11 wireless lan adapter
#
# This script should be invoked as follows:
#  wlan [start|check|stop|suspend|resume] [device name]
# e.g.
#  wlan start eth0
#
# The script passes an extended device address to 'wlan.opts' in
# the ADDRESS variable, to retrieve device-specific configuration
# options.  The address format is "scheme,socket,instance,hwaddr"
# where "scheme" is the current PCMCIA device configuration scheme,
# "socket" is the socket number, "instance" is used to number multiple
# interfaces in a single socket, and "hwaddr" is the card's hardware
# ethernet address.
#

. ./shared

# Get device attributes
get_info $DEVICE
HWADDR=`/sbin/ifconfig $DEVICE | sed -n -e 's/.*addr \(.*\)/\1/p'`

# Load site-specific settings
ADDRESS="$SCHEME,$SOCKET,$INSTANCE,$HWADDR"
. $0.opts

case "$ACTION" in

'start')

	# Scan for existing networks
	wlanctl $DEVICE scan $STARTCH $ENDCH 100 ff:ff:ff:ff:ff:ff both active $SSID
	sleep 2 	# wait for the scan to complete

	# If a wireless network exists, join it. Otherwise create a new one.
	wlanctl $DEVICE netlist > /tmp/wlan.tmp

	if grep "$SSID" /tmp/wlan.tmp ; then
		wlanctl $DEVICE bssjoin `grep "$SSID" /tmp/wlan.tmp | cut -c5-21`
	else
		# If we're allowed, create a bss
		if is_true $CREATEBSS ; then
			wlanctl $DEVICE bsscreate $CHANNEL $BCN_INT 0 "$SSID"
		else
			rm /tmp/wlan.tmp
			exit 1
		fi
	fi
	rm /tmp/wlan.tmp

	# select the appropriate ethernet conversion
	wlanctl $DEVICE ethconv $ETHCONV

	# set the scheme to be our SSID
	echo "$SSID" > /var/run/pcmcia-scheme

	# Now initialize the network interface same as always
	echo starting network $1 $2
	./network $1 $2

	exit $?

	;;

'stop'|'check'|'cksum'|'restart'|'suspend'|'resume')

	# Just handle it the same as any other net device
	echo calling network script
	./network $1 $2
	reval=$?

	#  nothing else for now...

	exit $retval
    ;;

*)
    usage
    ;;

esac

exit 0
