#!/bin/sh
# $Id: cgi-bin-botinfo.txt 1566 2008-02-14 14:24:42Z jow $

. /usr/share/shlib.sh interface batman bmx olsr version webif splash

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

	for file in /etc/botinfo.d/*.sh; do
		file="${file##*/}"
		echo " ${file%.*}"
	done
}

# read query string
webif_parse_urlquery

output_cmd()
{
	echo "$1{"
	( eval $2 2>&1 ) | sed -e's/[\{\}]/\\&/g;s/^/	/g'
	echo '}'
}

bot_nvram() {
	output_cmd 'nvram' "nvram show 2>&- |\
		sed -e '/ff_adm\|wl0_channel\|boardtype\|boardnum\|wan_hostname\|ff_release/!d' |\
		grep -e '^.\+=[^=].*' | sort"
}

bot_resolv() {
	output_cmd 'resolv' 'cat /etc/resolv.conf | sed -ne "/^\(search\|nameserver\)/ { p }"'
}

bot_address() {
	WLDEV=$(interface_get_wifidev)
	WLMASK=$(ip -f inet addr show dev $WLDEV label $WLDEV |sed -ne'2{s# \+inet \([0-9\.\/]\+\).*#\1#;p}')
	WLMASK=$(test -n "$WLMASK" && ipcalc -m $WLMASK|cut -d'=' -f2)
	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}')

	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

	# BATMAN neighbours
	if [ -x /usr/sbin/batmand ]; then
		echo 'neighbours_batman{'

		for line in $(batman_neighbours); do
			ifs="$IFS"; IFS=","; set -- $line; IFS="$ifs"
			echo "	$(interface_to_ip $1)	$2	$3"
		done

		echo '}'
	fi

	# BMX neighbours
	if [ -x /usr/sbin/bmxd ]; then
		echo 'neighbours_bmx{'

		for line in $(bmx_neighbours); do
			ifs="$IFS"; IFS=","; set -- $line; IFS="$ifs"
			echo "	$(interface_to_ip $1)	$2	$3	$4	$5	$6"
		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 '}'
}

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

	echo 'users{'

	if [ -f "$SLEASES" ]; then
		LTIMEOUT="$(($(date +%s)-$(($(splash_getvar ff_dhcpsplash_time)*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() {
	WLDEV=$(interface_get_wifidev)
	output_cmd 'wlan_rate' 'wl rate'
	output_cmd 'wlan_mrate' 'wl mrate'
	output_cmd 'wlan_rssi' 'wl rssi'
	output_cmd 'wlan_noise' 'wl noise'
	output_cmd 'wlan_txpower' 'wl txpwr'
	output_cmd 'wlan_status' 'wl -i $WLDEV status 2>&1'
}

bot_version() {
	reldate=$(version_get_releasedate)
	output_cmd 'version' 'echo "$(version_get_fullversion nonvram),$(nvram get ff_release),${reldate:-19700101}"'
}

bot_board() {
	output_cmd 'board' 'echo "$(nvram get boardtype),$(nvram get boardnum)"'
}

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

bot_wlscan() {
	WLDEV=$(interface_get_wifidev)

	echo 'wlan_scan{'

	( APMODE=$(wl -i $WLDEV ap | grep "on")
	test "$APMODE" && wl -i $WLDEV ap 0
	wl -i $WLDEV scan
	sleep 2
	wl -i $WLDEV scan
	sleep 2
	wl -i $WLDEV scanresults
	test "$APMODE" && wl -i $WLDEV ap 1 ) | sed -e's/^/	/g'

	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 /etc/hosts | 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
		;;
		*)
		if [ -f "/etc/botinfo.d/$cat.sh" ]; then
			output_cmd "$cat" ". /etc/botinfo.d/$cat.sh"
		else
			echo -e "not implemented: $cat\n"
			bot_usage
		fi
		;;
	esac
done
