#!/bin/sh

#
# Blink POWER_LED continuosly. Blink WIFI_LED once for each node
# associated to our WiFi access point.  This script should be run in
# background from /etc/rc.
#

# the led that tells the firewall is up and running
POWER_LED="/dev/led/led1"
# the led that counts the WiFi nodes
WIFI_LED="/dev/led/led2"
# time in seconds between LED updates
UPDATE_INTERVAL=15

###################################################################### 

IFCONFIG="/sbin/ifconfig"

[ -c "$WIFI_LED" ] || exit 0

list_interfaces () {
    $IFCONFIG -l -u 2>/dev/null
}

count_peers () {
    $IFCONFIG "$1" list sta 2>/dev/null | (
	i=0
	read x
	while read x
	do
	    i=`expr $i + 1`
	done
	echo $i )
}

update_leds () {
    tot=0
    for int in `list_interfaces`
    do
	i=`count_peers $int`
	tot=`expr $tot + $i`
    done
    if [ "$tot" = 0 ]
    then
	echo 0 > "$WIFI_LED"
    else
	echo d$tot > "$WIFI_LED"
    fi
}

###################################################################### 

echo f8 > $POWER_LED

while true
do
    update_leds
    sleep $UPDATE_INTERVAL
done

# shouldn't reach this point
exit 1
