#!/bin/sh
#This script is created by ssparser automatically. The parser first created by MaoShouyan
printf "Content-type: text/html;charset=gb2312
Cache-Control: no-cache

"
echo -n "";
. /etc/PG.conf
myself="/cgi-bin/App/gamecloud/`basename $0`"
RAMDISK=/usr/ramdisk
# floweye bin
FLOWEYE=${RAMDISK}/bin/floweye
if [ "${PALANG}" = "en" ]; then
	LANG_001="Insufficient operation permission"
fi
operator_check()
{
        local user=$1
	if [ "${TOPSEC}" != "" ]; then
		ADMIN_NAME="superman"
	else
		ADMIN_NAME="admin"
	fi
        [ "${user}" = "" ] && user=${ADMIN_NAME}
        if [ "${PANABIT_USER}" != "${ADMIN_NAME}" -a "${PANABIT_USER}" != "${user}" ]; then
                echo '{"ret": "1", "msg": "${LANG_001:=操作权限不够!}"}'
                exit 0;
        fi
}
gamecloud_applist_json()
{
	local v=""
	printf '"apps":['
	${FLOWEYE} gamecloud list | \
	while read name cname appenable inbps outbps cloudin cloudout othe
	do
		printf ${v}'{"name":"'${name}'", "enable":"'${appenable}'", "outbps":"'${outbps}'", "inbps":"'${inbps}'", "cloudout":"'${cloudout}'", "cloudin":"'${cloudin}'"}'
		v=","
	done
	printf ']'
}
gamecloud_stat()
{
	for nameval in `${FLOWEYE} gamecloud stat` 	
	do
		eval "${nameval}"
	done
	srciptbl="${ipgrpid}"	
	parent="\"gamecloud\":{\"ifname\": \"${ifname}\", \"outbps\":\"${outbps}\",\"inbps\":\"${inbps}\",\"bandwidth\":\"${bandwidth}\""
	parent="${parent},\"enable\":\"${enable}\",\"waitime_left\":\"${waitime_left}\",\"connect_time\":\"${connect_time}\""
	parent="${parent},\"serialno\":\"${appwan_keyno}\",\"startm\":\"${appwan_start}\",\"endtm\":\"${appwan_end}\""
	parent="${parent},\"bandwidth\":\"${appwan_bandwidth}\",\"area\":\"${appwan_area}\",\"expired\":\"${appwan_expired}\""
	parent="${parent},\"state\":\"${state}\",\"active\":\"${active}\",\"cloud_type\":\"${cloud_type}\""
	parent="${parent},\"ipcnt\":\"${ipcnt}\",\"license_ipcnt\":\"${license_ipcnt}\""
	parent="${parent},\"delay_cur\":\"${cur_delay}\",\"delay_min\":\"${min_delay}\",\"delay_max\":\"${max_delay}\""
	parent="${parent},\"srciptbl\":\"${srciptbl}\",\"ipgrpid\":\"${ipgrpid}\",\"flowcnt\":\"${flowcnt}\",\"lastdial\":\"${lastdial}\"}"
	for vname in $(${FLOWEYE} nat getproxy ${ifname} | grep -E "^tandby=|^disable=|^bandwidth=|^inbps=|^outbps=|^ifname=|^name=|^flowcnt=|^linkup=")
	do
		eval ${vname};
	done
	parent="${parent},\"parent\":{\"name\":\"${name}\",\"outbps\":\"${outbps}\",\"inbps\":\"${inbps}\",\"bandwidth\":\"${bandwidth}\""
	parent="${parent},\"flowcnt\":\"${flowcnt}\",\"linkup\":\"${linkup}\",\"disable\":\"${disable}\",\"tandby\":\"${tandby}\"}"
	parent="${parent},`gamecloud_applist_json`"
	json='{'${parent}'}'
	if [ "${CGI_callback}" != "" ]; then
		# mobile page
		json=${CGI_callback}'('${json}')'
		#else pc page
	fi
	echo ${json}
}
gamecloud_appset()
{
	local arg=""
	
	if [ "${CGI_app}" = "" -o "${CGI_enable}" = "" ]; then
		echo '{"ret": "1", "msg": "arg err!"}'
		return;
	fi
	if [ "${CGI_enable}" = "1" ]; then
		arg="appenable=${CGI_app}"
	else
		arg="appdisable=${CGI_app}"
	fi
	errmsg="`${FLOWEYE} gamecloud config ${arg}`"
	echo '{"ret": "'${?}'", "msg": "'${errmsg}'"}'
}
gamecloud_svrenable()
{
	local arg=""
	
	if [ "${CGI_enable}" = "" ]; then
		echo '{"ret": "1", "msg": "arg err!"}'
		return;
	fi
	if [ "${CGI_enable}" = "1" ]; then
		arg="enable=1"
	else
		arg="enable=0"
	fi
	errmsg="`${FLOWEYE} gamecloud config ${arg}`"
	echo '{"ret": "'${?}'", "msg": "'${errmsg}'"}'
}
gamecloud_redial()
{
	errmsg="`${FLOWEYE} gamecloud config redial=1`"
	echo '{"ret": "'${?}'", "msg": "'${errmsg}'"}'
}
gamecloud_rtgraph()
{
	local pxyid
	pxyid=`floweye gamecloud stat | grep "^pxyid=" | cut -d'=' -f2`
	result=`floweye dbifpxy get name=${pxyid} type=${CGI_datatype}`
	echo ${result}
}
case "${CGI_act}" in
"stat")
	operator_check
	gamecloud_stat
	;;
"appset")
	operator_check
	gamecloud_appset
	;;
"svrenable")
	operator_check
	gamecloud_svrenable
	;;
"redial")
	operator_check
	gamecloud_redial
	;;
"rtgraph")
	operator_check
	gamecloud_rtgraph
	;;
esac

echo -n "
";