#!/bin/sh
#
# test-wpacli
#
# Usage:
#   test-wpacli IFACE [bssid BSSID] [ssid SSID]
#
# 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!
#
# The wpa_supplicant control interface has to be in the standard
# location /var/run/wpa_supplicant. 
# 
# If you are using a non-standard location for your control 
# interfaces you can use test-command-wpacli and modify it to
# your needs.
#
# Licensed under the GNU GPL.  See /usr/share/common-licenses/GPL.
#
# History
# May 2006: test-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

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


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

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 -i"$IFACE" status | grep ^bssid)"
	[ "$ACTUAL_BSSID" = "$BSSID" ] || FAILED=1
fi
if [ "$FAILED" = 0 ] && [ "$SSID" ] ; then
	ACTUAL_SSID="$(wpa_cli -i"$IFACE" status | grep ^ssid)"
	[ "$ACTUAL_SSID" = "$SSID" ] || FAILED=1
fi

exit "$FAILED"

