#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then 
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_PPPOE_IPPXY"	#PPPOE
fi


get_ippxy_user_stat()
{
	printf "{"
	printf "\"_\":\"_\""

	${FLOWEYE} ippxy get name=${CGI_name} | awk -F "=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'
	
	printf "}"
}


load_ippxy_user()
{
	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	${FLOWEYE} ippxy list | grep -i "${CGI_keyword}" | awk \
	-v start=${start} -v end=${end} -v stat=${CGI_stat} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(stat == "data" && $7 != "DATA")
			next;
		else if(stat == "dailing" && $7 == "DATA")
			next;

		row++;
		if( row > end  || row < start ) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", NR;
        printf "\"name\":\"%s\",", $(col++);
		printf "\"ifname\":\"%s\",", $(col++);
		printf "\"mtu\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"gateway\":\"%s\",", $(col++);
		printf "\"pppoe_stat\":\"%s\",", $(col++);
		printf "\"lastip\":\"%s\",", $(col++);
		printf "\"ippxy_stat\":\"%s\",", $(col++);
		printf "\"ltime\":\"%s\",", $(col++);
		printf "\"dnsreq\":\"%s\",", $(col++);
		printf "\"dnsack\":\"%s\",", $(col++);
		printf "\"ipcnt\":\"%s\",", $(col++);
		printf "\"dnsper\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


load_ippxy_stat()
{
	. ../common/ajax_object_lib

	printf "{"
    printf "\"interface_list\":`lib_interface_list`"

	${FLOWEYE} ippxy stat | awk -F"=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'

	printf "}"
}


set_ippxy_stat()
{
	[ "${CGI_enable}" = "on" ] && enable=1 || enable=0

	cmdargs="enable=${enable} ifname=${CGI_ifname} vlan=${CGI_vlan} mtu=${CGI_mtu} ttl=${CGI_ttl} waitime=${CGI_waitime}"
	errmsg=`${FLOWEYE} ippxy config ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		WEB_LOGGER "${LANG003:=PPPOE}" "${cmdargs}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


load_ipppxy_log()
{
	en=0
	[ "${PALANG}" = "en" ] && en=1

	end=$((${CGI_limit} * ${CGI_page}))
	start=$((${end} - ${CGI_limit}))

	${FLOWEYE} syslog list ippxy en=${en} | grep -i "${CGI_keyword}" | awk \
	-v name="${CGI_name}" -v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(name != "" && $2 != name) next;

		row++;
		if(row > end || row < start) next;

		printf "%s{", dot;
		printf "\"time\":\"%s\",", $1;
		printf "\"name\":\"%s\",", $2;
		printf "\"event\":\"%s\"", $3;
		printf "}"

		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


case "${CGI_action}" in
	"get_ippxy_user_stat")
		retjson 0 "OK" "`get_ippxy_user_stat`"
		;;

    "load_ippxy_user")
        retjson 0 "OK" "`load_ippxy_user`"
        ;;

	"load_ippxy_stat")
		retjson 0 "OK" "`load_ippxy_stat`"
		;;

	"set_ippxy_stat")
		action_check
		set_ippxy_stat
		;;

	"load_ipppxy_log")
		retjson 0 "OK" "`load_ipppxy_log`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
