#!/bin/sh
# $Id: cgi-bin-botinfo.txt 1566 2008-02-14 14:24:42Z jow $
# edit botinfo.txt 2009-09-12 matthias $
. /usr/share/shlib-webif.sh
. /usr/share/shlib-olsr.sh

bot_usage() {
	cat<<EOF
Usage: ./cgi-bin-botinfo.txt?cat=<category>[,<category2>,<category3>,...]

possible categories are:
 address
# board
 hosts
 neighbours
 nvram
 olsr
 resolv
 routes
 scan
# users
 uptime
 version
 wlan
 
additional categories:
EOF

}

# read query string
webif_parse_urlquery
WLDEV=ath0
output_cmd()
{
	echo "$1{"
	( eval $2 2>&1 ) | sed -e's/[\{\}]/\\&/g;s/^/	/g'
	echo '}'
}
interface_to_ip() {
        ip -f inet addr show dev "${1%:*}" label "$1" | sed -ne '2{s/ \+inet \([0-9\.]\+\).*/\1/;p}'
}

get_board() {
	#boardtype=$(uci get )
	#boardnum=$(uci get )
	echo a > /dev/null
}
                

bot_nvram() {
	ff_adm_nick=$(uci get freifunk.contact.nickname)
	wl0_channel=$(uci get wireless.wifi0.channel)
	wan_hostname=$(uci get system.@system[0].hostname)
	#ff_release=$(uci get )
	get_board
	ff_adm_mail=$(uci get freifunk.contact.mail)
	ff_adm_name=$(uci get freifunk.contact.name)
	ff_adm_latlon=$(uci get system.@system[0].latitude)
	ff_adm_latlon1=$(uci get system.@system[0].longitude)
	ff_adm_note=$(uci get freifunk.contact.note)
	output_cmd 'nvram' 'echo "ff_adm_nick=$ff_adm_nick"; echo "ff_adm_mail=$ff_adm_mail"; echo "ff_adm_name=$ff_adm_name"; echo "ff_adm_note=$ff_adm_note"; echo "ff_adm_latlon=$ff_adm_latlon;$ff_adm_latlon1"; echo "wan_hostname=$wan_hostname"; echo "wl0_channel=$wl0_channel"' 
}
bot_resolv() {
	output_cmd 'resolv' 'cat /etc/resolv.conf | sed -ne "/^\(search\|nameserver\)/ { p }"'
}

bot_address() {
	IP=$(ip -f inet addr show dev $WLDEV label $WLDEV |sed -ne'2{s/ \+inet \([0-9\.]\+\).*/\1/;p}')
	MAC=$(ip link show dev $WLDEV|sed -ne'2{s/.*ether \+\([^ ]\+\).*/\1/;p}')
	WLMASK=$(ifconfig $WLDEV | grep $IP| cut -d ":" -f 4)
	output_cmd 'address' 'echo "$IP,$WLMASK,$MAC"'
}

bot_neighbours() {
	# OLSR Neighbours
	if [ -x /usr/sbin/olsrd ]; then
		echo 'neighbours_olsr{'

		for line in $(olsr_neighbours); do
			ifs="$IFS"; IFS=","; set -- $line; IFS="$ifs"
			etx="${7#9999.98}"
			echo "	$(interface_to_ip $1)	$2	$3	$4	$5	$6	${etx:-0.00}"
		done

		echo '}'
	fi

	# default routes
	echo 'defaultroute{'

	ip route show exact 0/0 table all | while read line; do
			ifs="$IFS"; IFS=" "; set -- $line; IFS="$ifs"
			
			if [ "$1" = "default" -a "$2" = "via" ]; then
				echo "	$3	$5	$7"
			fi
	done
	
	echo '}'
}

ibot_users() {
	DLEASES="/var/dhcp.leases"
	SLEASES="/var/run/dhcpsplash.leases"

	echo 'users{'

	if [ -f "$SLEASES" ]; then
		LTIMEOUT="$(($(date +%s)-$(($(uci get luci_splash.general.leasetime)*3600))))"

		( if [ -s "$DLEASES" ]; then
			while read TIMEOUT MAC IP NAME FLAGS; do
				LEASESTATE=$(splash_getleasestate $MAC $IP)
				MAC=$(echo $MAC | sed y/abcdef/ABCDEF/ | webif_masqmac)
				echo "DHCP,$IP,$NAME,$MAC,$LEASESTATE"
			done < $DLEASES
		fi

		cat /proc/net/arp | sed '/^IP/d' |
		while read IP TYPE FLAGS MAC MASK IFACE; do
			NAME=$(sed -ne "s/^$IP[[:space:]]\([^[:space:]]\+\)[[:space:]]#.\+/\1/p;t" /etc/hosts)
			test -z "$NAME" && NAME='*'
			if ! grep -iqs " $IP " $DLEASES; then
				LEASESTATE=$(splash_getleasestate $MAC $IP)
				MAC=$(echo $MAC | webif_masqmac)
				echo "STATIC,$IP,$NAME,$MAC,$LEASESTATE"
			fi
		done ) | sed -e's/^/	/g'
	fi

	echo '}'
}

bot_wlan() {
	noise=$(iwconfig $WLDEV | grep Noise | cut -d "=" -f 4 | cut -d " " -f 1)
	rssi=$(iwconfig $WLDEV | grep Signal | cut -d "=" -f 3 | cut -d " " -f 1)
	txpwr=$(iwconfig $WLDEV | grep Tx-Power | cut -d "=" -f 2 | cut -d " " -f 1)
	ssid=$(iwconfig $WLDEV | grep ESSID | cut -d ":" -f 2 | cut -d " " -f 1)          
	mode=$(iwconfig $WLDEV | grep Mode | cut -d ":" -f 2 | cut -d " " -f 1)    
	channel=$(iwlist $WLDEV channel | grep Current | cut -d ":" -f 2 | cut -d " " -f 4 | cut -d ")" -f 1)
	bssid=$(iwconfig $WLDEV | grep Cell | cut -d " " -f 17)
	rate=$(wlanconfig $WLDEV list sta | awk '{print $4}' | sed -n '2p' | cut -f 1 -d "M")
	
	output_cmd 'wlan_rate' 'echo "rate is $rate Mbps"'
	output_cmd 'wlan_mrate' 'echo "mrate is $rate Mbps"'
	output_cmd 'wlan_rssi' 'echo "rssi is $rssi"'
	output_cmd 'wlan_noise' 'echo "noise is $noise"'
	output_cmd 'wlan_txpower' 'echo "txpwr is $txpwr"'
	output_cmd 'wlan_status' 'echo "SSID: $ssid"; echo -e "Mode: $mode\tRSSI: $rssi dBm\tnoise: $noise dBm\tChannel: $channel"; echo "BSSID: $bssid"'
}

bot_version() {
	version=$(cat /etc/banner | sed -n '6p' | cut -d "-" -f 1)
	output_cmd 'version' 'echo "$version"'
}
bot_board() {
	get_board
	output_cmd 'board' 'echo "$boardtype,$boardnum"'
}

bot_uptime() {
	output_cmd 'uptime' 'uptime'
}

bot_wlscan() {
	echo 'wlan_scan{'
	iwlist $WLDEV scanning |sed -n '1!p'
	echo '}'
}

bot_routes() {
	output_cmd 'routes' 'ip route'
}

bot_olsr() {
	output_cmd 'botolsr' 'wget -q -O - http://127.0.0.1:2006/'
}

bot_hosts() {
        output_cmd 'hosts' 'cat /var/etc/hosts.olsr | sed -ne"s#^\([0-9.]\+\)[[:space:]]\+\([^[:space:]]\+\).*\$#\1 \2#p"'
}

test -z $cat && bot_usage
for cat in $(echo $cat | sed 's/,/ /g'); do
	case $cat in
		board)
		bot_board
		;;
		scan)
		bot_wlscan
		;;
		neighbours)
		bot_neighbours
		;;
		routes)
		bot_routes
		;;
		users)
		bot_users
		;;
		uptime)
		bot_uptime
		;;
		version)
		bot_version
		;;
		wlan)
		bot_wlan
		;;
		address)
		bot_address
		;;
		nvram)
		bot_nvram
		;;
		resolv)
		bot_resolv
		;;
		olsr)
		bot_olsr
		;;
		hosts)
		bot_hosts
		;;
		*)
		echo -e "not implemented: $cat\n"
		bot_usage
		;;
	esac
done
