#! /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

# set -x 

# 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
	echo "wlan scanning channels $STARTCH to $ENDCH"
	netlist=`wlanctl $DEVICE scan \
		$SCAN_STARTCH $SCAN_ENDCH $SCAN_TIMEPERCHANNEL \
		$SCAN_BSSID $SCAN_BSSTYPE $SCAN_TYPE "$SCAN_SSID"`
	echo "Detected BSSs:"
	echo "ch  bssid              bcn   cap_info  ssid"
	echo "------------------------------------------------------"
	echo "$netlist"
	
	# Set your wep keys, if present
	# MSM: setting them works, but there's no cypher so strange things
	#      might happen.
	if is_true $PRIVACY ; then
		wlanctl $DEVICE privacy \
			$EXCLUDE \
			$WEP_DEFKEY \
			$WEP_KEY1 \
			$WEP_KEY2 \
			$WEP_KEY3 \
			$WEP_KEY4
	fi

	# Set the ethernet conversion
	wlanctl $DEVICE ethconv $P80211_CONVERSION

	# If our $SCAN_SSID matches one found in the scan, join it.  
	#  else, check to see if we're allowed to create one.
	if (echo "$netlist" | grep -q "$DESIRED_SSID") ; then
		join_bssinfo=`(echo "$netlist" | grep "$DESIRED_SSID")`
		join_ssid=$DESIRED_SSID
		join_bssid=`(echo "$join_bssinfo" | cut -c5-21)`
		join_channel=`(echo "$join_bssinfo" | cut -c1-2)`
		join_capinfo=`(echo "$join_bssinfo" | cut -c30-35)`

		wlanctl $DEVICE bssjoin $join_bssid

		echo "wlan joined BSS, channel=$join_channel"\
		     "bssid=$join_bssid ssid=\"$DESIRED_SSID\""

		# Now see if we need to auth and assoc w/ an infra bss
		if is_true $SNIFONLY; then
			echo "wlan skipping auth and assoc for sniffer"
		else
			ISIBSS=$[$join_capinfo & 0x02]
			if [ $ISIBSS -eq 0 ]; then
				wlanctl $DEVICE authen
				wlanctl $DEVICE assoc
			fi
		fi
	else
		# If we're allowed, create a bss
		if is_true $CREATEBSS ; then
			wlanctl $DEVICE bsscreate \
				$CREATE_CHANNEL $CREATE_BCN_INT 0 \
				"$CREATE_SSID"

			echo "wlan created BSS with" \
				"channel=$CREATE_CHANNEL and" \
				"ssid=$CREATE_SSID"

			$DESIRED_SSID=$CREATE_SSID
		else
			echo "wlan failed to find ssid=$DESIRED_SSID"
			exit 1
		fi
	fi

	# Set the scheme to be our SSID, this allows us to set up
	#  multiple settings for different locations in the network.opts file.
	echo "$DESIRED_SSID" > /var/run/pcmcia-scheme

	# Now initialize the network interface in the normal way
	./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
