#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_User_Group"	#û
	LANG004="INV_PID"			#ѡԼΪϼڵ
	LANG005="There are child nodes under the current group, and the parent node cannot be replaced"	#ǰӽڵ㣬޷ϼڵ
	LANG006="Delete_User_Group"	#ɾû
fi


list_ippool()
{
	${FLOWEYE} pppoeippool list | sort -nk3 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s{", dot;
		printf "\"id\":\"%s\",", $1;
		printf "\"field\":\"%s\",", $1;
		printf "\"title\":\"%s\",", $2;
		printf "\"pid\":\"%s\",", $3;
		printf "\"pname\":\"%s\",", $4;
		printf "\"start\":\"%s\",", $5;
		printf "\"end\":\"%s\",", $6;
		printf "\"ipleft\":\"%s\",", $7;
		printf "\"nextip\":\"%s\",", $8;
		printf "\"acctcnt\":\"%s\",", $9;
		printf "\"ratein\":\"%s\",", $10;
		printf "\"rateout\":\"%s\",", $11;
		printf "\"dns0\":\"%s\",", $12;
		printf "\"dns1\":\"%s\",", $13;
		printf "\"maxonlinetime\":\"%s\",", $14;
		printf "\"clntepa\":\"%s\",", $15;
		printf "\"ifname\":\"%s\",", $16;
		printf "\"vlan\":\"%s\",", $17;
		printf "\"children\":[]";
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


have_child_check()
{
	cid="${1}"
	have_child=0

	for id in `${FLOWEYE} pppoeippool list | cut -d" " -f3`
	do
		if [ "${id}" = "${cid}" ]; then
			have_child=1
			break
		fi
	done

	echo ${have_child}
}


add_ippool()
{
	if [ "${CGI_action}" = "add_ippool" ]; then
		action="add"
	else
		have_child=`have_child_check ${CGI_id}`
		[ "${have_child}" = "1" -a "${CGI_pid}" != "${CGI_old_pid}" ] && retjson 1 "${LANG005:=ǰӽڵ㣬޷ϼڵ}"
		[ "${CG_pid}" = "${CGI_id}" ] && retjson 1 "${LANG004:=ѡԼΪϼڵ}"
		action="set"
	fi

	[ "${CGI_pid}" = "" ] && CGI_pid="0"
	[ "${CGI_ratein}" = "" ] && CGI_ratein="0"
	[ "${CGI_rateout}" = "" ] && CGI_rateout="0"
	[ "${CGI_dns}" = "" ] && CGI_dns="0.0.0.0"
	[ "${CGI_maxonlinetime}" = "" ] && CGI_maxonlinetime="0"
	[ "${CGI_ifvlan1}" = "" ] && CGI_ifvlan1="0"
	[ "${CGI_ifvlan2}" = "" ] && CGI_ifvlan2="0"
	[ "${CGI_svcname}" = "" ] && CGI_svcname="NULL"
	[ "${CGI_maxip}" = "" ] && CGI_maxip="0"
	[ "${CGI_maxdial}" = "" ] && CGI_maxdial="0"

	cmdargs="id=${CGI_id} pid=${CGI_pid} name=${CGI_name} start=${CGI_start} end=${CGI_end}"
	cmdargs="${cmdargs} ratein=${CGI_ratein} rateout=${CGI_rateout} dns=${CGI_dns}"
	cmdargs="${cmdargs} clntepa=${CGI_clntepa} maxonlinetime=${CGI_maxonlinetime}"

	if [ "${CGI_ifname}" != "NULL" ]; then
		cmdargs="${cmdargs} ifname=${CGI_ifname1}|${CGI_ifname2} vlan=${CGI_ifvlan1}|${CGI_ifvlan2} svcname=${CGI_svcname}"
		cmdargs="${cmdargs} maxip=${CGI_maxip} maxdial=${CGI_maxdial}"
	fi

	errmsg=`${FLOWEYE} pppoeippool ${action} ${cmdargs}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:ʧ}:${errmsg}"
	else
		sync_floweye "pppoeippool ${action} ${cmdargs}"
		WEB_LOGGER "${LANG003:=û}" "name=${CGI_name}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


del_ippool()
{
	have_child=`have_child_check ${CGI_id}`
	
	[ "${have_child}" = "1" ] && retjson 1 "${LANG005:=ǰӽڵ㣬޷ϼڵ}"

	errmsg=`${FLOWEYE} pppoeippool remove id=${CGI_id}`

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:ʧ}:${errmsg}"
	else
		sync_floweye "pppoeippool remove id=${CGI_id}"
		WEB_LOGGER "${LANG006:=ɾû}" "name=${CGI_name}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


get_ippool()
{
	printf "{"
	printf "\"id\":\"${CGI_id}\""
	
	${FLOWEYE} pppoeippool get id=${CGI_id} pid=${CGI_pid} | awk -F"=" \
	'{
		printf ",\"%s\":\"%s\"", $1, $2;
	}'

	printf "}"
}


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

	printf "}"
}


get_user_account()
{
	printf "{"
	printf "\"id\":\"${CGI_id}\""

	${FLOWEYE} pppoeacct get name=${CGI_account} | awk -F"=" \
	'{
		if($1 == "cname" || $1 == "other")
			gsub("%20", " ", $2);

		printf ",\"%s\":\"%s\"", $1, $2;
	}'

	printf "}"
}


load_ippool_id()
{
	${FLOWEYE} pppoeippool list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s{", dot;
		printf "\"id\":\"%s\",", $1;
		printf "\"name\":\"%s\"", $2;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_if()
{
	${FLOWEYE} if list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\",", $1;
		printf "\"type\":\"%s\"", $3;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_add_env()
{
	printf "{"
	printf "\"if\":`load_if`,"
	printf "\"ippool\":`load_ippool_id`"
	printf "}"
}


load_edit_group_env()
{
	printf "{"
	printf "\"if\":`load_if`,"
	printf "\"info\":`get_ippool`,"
	printf "\"ippool\":`load_ippool_id`"
	printf "}"
}


load_edit_acct_env()
{
	printf "{"
	printf "\"if\":`load_if`,"
	printf "\"acct\":`get_user_account`,"
	printf "\"ippool\":`load_ippool_id`"
	printf "}"
}


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

	"load_edit_group_env")
		retjson 0 "OK" "`load_edit_group_env`"
		;;

	"load_edit_acct_env")
		retjson 0 "OK" "`load_edit_acct_env`"
		;;

	"list_ippool")
		retjson 0 "OK" "`list_ippool`"
		;;

	"add_ippool"|"edit_ippool")
		action_check
		add_ippool
		;;

	"del_ippool")
		action_check
		del_ippool
		;;

	"get_ippool")
		retjson 0 "OK" "`get_ippool`"
		;;

	"get_ippool_byname")
		retjson 0 "OK" "`get_ippool_byname`"
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
