#!/bin/sh
#
# test-command-wpacli
#
# Usage:
#   test-command-wpacli [bssid BSSID] [ssid SSID]
#
# This is designed to work as a "guessnet custom script", i.e.
# it needs $IFACE to be set when called.
#
# If you are using wpa_supplicant you can use this test
# to check wether the current interface has the 
# appropriate BSSID address and/or the appropriate SSID.
#
# Since it uses wpa_cli to get this information
# BSSID letters must be in lower case!
#
# If you are using a non-standard location for your control 
# interfaces you can use test-wpacli-command and modify it to
# your needs.
#
# Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL.
#
# History
# May 2006: test-command-wpacli Written by Felix Homann 
#	    based on test-wireless 
# Oct 2004: test-wireless Written by Thomas Hood

set -o errexit   # -e
set -o noglob    # -f

MYNAME="$(basename $0)"
PATH=/sbin:/bin

# Edit this line:
WPA_CTRL_INTERFACE=/var/run/wpa_supplicant

report_err() { echo "${MYNAME}: Error: $*" >&2 ; }


[ "$IFACE" ] || { report_err "Interface not specified.  Exiting." ; exit 1 ; }

while [ "$1" ] ; do
	case "$1" in
		bssid)
			BSSID="bssid=$2"
			shift
			;;
		ssid)
			SSID="ssid=$2"
			shift
			;;
	esac
	shift
done

[ "$BSSID" ] || [ "$SSID" ] || { report_err "Neither AP BSSID nor SSID specified.  Exiting." ; exit 1 ; }

FAILED=0

if [ "$BSSID" ] ; then
	ACTUAL_BSSID="$(wpa_cli -p"$WPA_CTRL_INTERFACE"-i"$IFACE" status | grep ^bssid)"
	[ "$ACTUAL_BSSID" = "$BSSID" ] || FAILED=1
fi
if [ "$FAILED" = 0 ] && [ "$SSID" ] ; then
	ACTUAL_SSID="$(wpa_cli -p"$WPA_CTRL_INTERFACE" -i"$IFACE" status | grep ^ssid)"
	[ "$ACTUAL_SSID" = "$SSID" ] || FAILED=1
fi

exit "$FAILED"

