#!/bin/bash
#
# WPA supplicant control script
#

WPA_DAEMON=/usr/sbin/wpa_supplicant
WPA_CLI=/usr/sbin/wpa_cli
DEVICE=$1

if [ -z "$WPA_CONF" ]; then
  WPA_CONF=/etc/wpa_supplicant.conf
fi

if [ ${DEVICE:0:3} = "ath" ]; then
  WPA_DRIVER=madwifi
else
  WPA_DRIVER=wext
fi

$WPA_DAEMON -B -D "$WPA_DRIVER" -i "$DEVICE" -c "$WPA_CONF" > /dev/null 2>&1
$WPA_CLI interface $DEVICE > /dev/null 2>&1
NETWORK_ID=`$WPA_CLI list_network | awk '\$2 == "'"$ESSID"'" {print \$1}'`
$WPA_CLI select $NETWORK_ID > /dev/null  2>&1
