#!/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


load_ippool_tree()
{
	[ "${CGI_pid}" = "" ] && CGI_pid=0

	${FLOWEYE} pppoeippool list parent=${CGI_pid} | grep "${CGI_keyword}" | sort -nk3 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;

		if(pid != "" && pid != $3) next;

		printf "%s{", dot;
		printf "\"id\":%s", $(col++);
		printf ",\"name\":\"%s\"", $(col++);
		printf ",\"pid\":%s", $(col++);
		printf ",\"pname\":\"%s\"", $(col++);
		printf ",\"start\":\"%s\"", $(col++);
		printf ",\"end\":\"%s\"", $(col++);
		printf ",\"ipleft\":\"%s\"", $(col++);
		printf ",\"nextip\":\"%s\"", $(col++);
		printf ",\"acctcnt\":\"%s\"", $(col++);
		printf ",\"ratein\":\"%s\"", $(col++);
		printf ",\"rateout\":\"%s\"", $(col++);
		printf ",\"ratein6\":\"%s\"", $(col++);
		printf ",\"rateout6\":\"%s\"", $(col++);
		printf ",\"dns0\":\"%s\"", $(col++);
		printf ",\"dns1\":\"%s\"", $(col++);
		printf ",\"maxonlinetime\":\"%s\"", $(col++);
		printf ",\"clntepa\":\"%s\"", $(col++);
		printf ",\"ifname\":\"%s\"", $(col++);
		printf ",\"vlan\":\"%s\"", $(col++);
		printf ",\"prefix\":\"%s\"", $(col++);
        printf ",\"dhcp6_start\":\"%s\"", $(col++);
		printf ",\"dhcp6_end\":\"%s\"", $(col++);
		printf ",\"isParent\":%s", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


list_ippool()
{
	${FLOWEYE} pppoeippool list | sort -nk3 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;

		printf "%s{", dot;
		printf "\"id\":\"%s\"", $(col);
		printf ",\"field\":\"%s\"", $(col++);
		printf ",\"title\":\"%s\"", $(col++);
		printf ",\"pid\":\"%s\"", $(col++);
		printf ",\"pname\":\"%s\"", $(col++);
		printf ",\"start\":\"%s\"", $(col++);
		printf ",\"end\":\"%s\"", $(col++);
		printf ",\"ipleft\":\"%s\"", $(col++);
		printf ",\"nextip\":\"%s\"", $(col++);
		printf ",\"acctcnt\":\"%s\"", $(col++);
		printf ",\"ratein\":\"%s\"", $(col++);
		printf ",\"rateout\":\"%s\"", $(col++);
		printf ",\"ratein6\":\"%s\"", $(col++);
		printf ",\"rateout6\":\"%s\"", $(col++);
		printf ",\"dns0\":\"%s\"", $(col++);
		printf ",\"dns1\":\"%s\"", $(col++);
		printf ",\"maxonlinetime\":\"%s\"", $(col++);
		printf ",\"clntepa\":\"%s\"", $(col++);
		printf ",\"ifname\":\"%s\"", $(col++);
		printf ",\"vlan\":\"%s\"", $(col++);
		printf ",\"prefix\":\"%s\"", $(col++);
		printf ",\"children\":[]";
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_pool_stat()
{
	acct_cnt=`${FLOWEYE} pppoeippool stat | grep "^acct_count=" | cut -d"=" -f2`

	printf "{"
	printf "\"pool\":`list_ippool`"
	printf ",\"acct_cnt\":\"${acct_cnt}\""
	printf "}"
}


load_secure_group()
{
	${FLOWEYE} pppoeippool list parent=sg | grep "${CGI_keyword}" | sort -rk2 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\"", $1;
		printf ",\"name\":\"%s\"", $2;
		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_ratein6}" = "" ] && CGI_ratein6="0"
	[ "${CGI_rateout6}" = "" ] && CGI_rateout6="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"
	[ "${CGI_prefix}" = "" ] && CGI_prefix="::"
	[ "${CGI_pfxlen}" = "" ] && CGI_pfxlen="0"
	[ "${CGI_pdlen}" = "" ] && CGI_pdlen="0"
    [ "${CGI_dhcp6_start}" = "" ] && CGI_dhcp6_start="::"
    [ "${CGI_dhcp6_end}" = "" ] && CGI_dhcp6_end="::"

	cmdargs="id=${CGI_id} pid=${CGI_pid} name=${CGI_name} start=${CGI_start} end=${CGI_end}"
	cmdargs="${cmdargs} ratein=${CGI_ratein} rateout=${CGI_rateout} ratein6=${CGI_ratein6} rateout6=${CGI_rateout6} dns=${CGI_dns}"
	cmdargs="${cmdargs} clntepa=${CGI_clntepa} maxonlinetime=${CGI_maxonlinetime} vlan_first=${CGI_vlan_first}"
    cmdargs="${cmdargs} assign_mode=${CGI_assign_mode} dhcp6_start=${CGI_dhcp6_start} dhcp6_end=${CGI_dhcp6_end}"   

	if [ "${CGI_dns6}" != "" ]; then
		cmdargs="${cmdargs} dns6=${CGI_dns6}"
	fi

	if [ "${CGI_ifname1}" != "NULL" -a "${CGI_ifname1}" != "" ]; 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

	cmdargs="${cmdargs} prefix=${CGI_prefix} pfxlen=${CGI_pfxlen} pdlen=${CGI_pdlen}"

	echo "${FLOWEYE} pppoeippool ${action} ${cmdargs}" >/root/cmd.log
	errmsg=`${FLOWEYE} pppoeippool ${action} ${cmdargs}`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "pppoeippool ${action} ${cmdargs}"
		WEB_LOGGER "${LANG003:=û}" "${cmdargs}"
		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 [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
	else
		sync_floweye "pppoeippool remove id=${CGI_id}"
		WEB_LOGGER "${LANG006:=ɾû}" "id=${CGI_id} name=${CGI_name}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


clear_ippool()
{
	errmsg=`${FLOWEYE} pppoeacct config clearall=1`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧ:${errmsg}"
	else
		WEB_LOGGER "û"
		sync_floweye "pppoeacct config clearall=1"
		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()
{
	pppoepwd_plaintext=1
	[ -f "${PGETC}/web/webacl.conf" ] && . ${PGETC}/web/webacl.conf 2>/dev/null
	printf "{"
	printf "\"id\":\"${CGI_id}\""
	${FLOWEYE} pppoeacct get name=${CGI_account} | awk -F"=" -v plaintext=${pppoepwd_plaintext} \
	'{
		if($1 == "cname" || $1 == "other")
			gsub("%20", " ", $2);
		if(plaintext == 0 && $1 == "passwd")
            next;
		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_ippool_tree")
		retjson 0 "OK" "`load_ippool_tree`"
		;;

	"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`"
		;;

	"load_secure_group")
		retjson 0 "OK" "`load_secure_group`"
		;;

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

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

	"del_ippool")
		action_check
		del_ippool
		;;

	"clear_ippool")
		action_check
		clear_ippool
		;;

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

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

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
