#!/bin/sh

#
# hotspot_mactotal
#
#   Get the total number of MAC connected to wireless_ap
#
# Copyright (C) 2013  Bayram Kurumahmut <bayram.kurumahmut@airties.com>
#

. /usr/bin/debug.sh

debug_level=0

. /usr/bin/config.sh

resp_type="onlyresp"
try_val="try 5"

get_val()
{
	[ "$1" = "inst_name" ] && {
		inst_name="$2"
		return
	}
}

usage()
{
	echo "{"
	echo "\"error\" : \"1\""
	echo "}"
	exit
}

echo "Content-type: application/json"
echo ""

[ "$REQUEST_METHOD" = "POST" -a -n "$CONTENT_LENGTH" ] && {
	 read -n $CONTENT_LENGTH QUERY_STRING_POST
} || {
	usage
}

q="${QUERY_STRING_POST}&"

#
# parse the query data
#
while [ -n "$q" ]
do

	p="${q%%&*}"				# pull a pair from the q list
	k="${p%%=*}"				# key from the pair
	v="${p#*=}"					# value from the pair
	q="${q#$p&*}"				# remove this pair from the list

#	echo "p=$p,k=$k,v=$v,q=$q"

	get_val "$k" "$v"

done

[ ! -n "$inst_name" ] && {
	usage
}

#echo "inst_name:$inst_name"

child_pi=`get_cfg $inst_name settings/port/inst`
[ ! -n "$child_pi" ] && {
#	child_pi=wireless_ap-0
	exit
}

#echo "child_pi:$child_pi"
sta_total=`cli query ${child_pi}:sta_list | grep sta_mac -c`

echo "{"
echo "\"hotspot\" : {"

echo "\"runtime\" : {"
echo "\"mac_total\" : $sta_total"
echo "}"

echo "}"
echo "}"
