#!/bin/bash
#
# Written by Justin Huff <jjhuff@cs.washington.edu>
# Posted to zaurus-general (Message ID <1015553560.18841.36.camel@foraker>).
#
# Reformatted slightly by Ryan W. Maple <ryan@guardiandigital.com>.
#
# Hacked to fit in a nice little knosole window of the right size, to show
# ping times, to detect when the WiFi card is missing, and to use
# /sbin/wlanctl-ng instead of wlanctl-ng by Joshua Wise
# <joshua@joshuawise.com>.
#

set -e

show_data() {
  if ! /sbin/ifconfig wlan0 2>/dev/null >/dev/null; then
  	clear
	echo "No wireless card loaded"
  else
	list=`/sbin/wlanctl-ng  wlan0 dot11req_mibget "mibattribute=p2CommsQuality"|grep 'p2CommsQuality'|cut -d= -f 3`

	qual=`echo "$list" | cut -d, -f 1`
	signal=`echo "$list" | cut -d, -f 2`
	noise=`echo "$list" | cut -d, -f 3`

	ssid=`/sbin/wlanctl-ng  wlan0 dot11req_mibget "mibattribute=p2CurrentSSID"|grep 'p2CurrentSSID'|cut -d= -f 3`
	mac=`/sbin/wlanctl-ng  wlan0 dot11req_mibget "mibattribute=p2CurrentBSSID"|grep 'p2CurrentBSSID'|cut -d= -f 3`
  
	ip1latency=`ping -c 1 -w 1 192.168.2.245 2>/dev/null| head -2 | tail -1 | cut -d= -f 4`
	ip2latency=`ping -c 1 -w 1 192.168.2.1 2>/dev/null| head -2 | tail -1 | cut -d= -f 4`

	clear	        
	echo "$ssid ($mac)"
	echo "  Quality=$qual   Signal=$signal  Noise=$noise"
	echo
	echo    "192.168.2.245: $ip1latency"
	echo -n "192.168.2.1:   $ip2latency             "
  fi
}

while true; do sleep 1; show_data; done
