#!/bin/rc
rfork e

DEVICE=/net/ether0

BSSIDS=(fcecda1489eb)

ESSIDS=(PHONE j j-slowwwww 'Woodford Reserve' 'Google Starbucks')
FOUND=0

if (~ $#* 1) {
	BSSIDS=()
	ESSIDS=($1)
}
if (ps |grep wpa >/dev/null) {
	echo not starting aux/wpa
}
if not aux/wpa $DEVICE

for (bssid in $BSSIDS) {
	if (~ $FOUND 0 && grep '^node: '^$"bssid $DEVICE/ifstats) {
		echo connecting to $bssid...
		echo bssid ''''$bssid'''' > $DEVICE/clone
		FOUND=1
	}
}
for (essid in $ESSIDS) {
	if (~ $FOUND 0 && grep $"essid^'$' $DEVICE/ifstats) {
		echo connecting to $essid...
		echo essid ''''$essid'''' > $DEVICE/clone
		FOUND=1
	}
}

if (~ $FOUND 1) {
	kill ipconfig |rc
	ip/ipconfig ether $DEVICE unbind
	ip/ipconfig ether $DEVICE
}
