#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Success"
	LANG002="Add DNS Policy"
	LANG003="Edit DNS Policy"
	LANG004="Remove DNS Policy"
	LANG005="Enable DNS Policy"
	LANG006="Disable DNS Policy"
	LANG007="Batch Enable DNS Policy"
	LANG008="Batch Disable DNS Policy"
	LANG009="Batch Remove DNS Policy"
fi


list_dnspolicy()
{
	${FLOWEYE} dnspolicy list | grep "${CGI_keyword}" | awk \
	-v active="${CGI_active}" -v disabled="${CGI_disabled}" \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(active == "" && $13 == 0) next;
		if(disabled != "" && $22 != disabled) next;

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"polno\":\"%s\",", $(col++);
		printf "\"inif\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"intype\":\"%s\",", $(col++);
		printf "\"inip\":\"%s\",", $(col++);
		printf "\"outtype\":\"%s\",", $(col++);
		printf "\"outip\":\"%s\",", $(col++);
		printf "\"dnstype\":\"%s\",", $(col++);
		printf "\"dns\":\"%s\",", $(col++);
		printf "\"bridge\":\"%s\",", $(col++);
		printf "\"sch_id\":\"%s\",", $(col++);
		printf "\"sch_name\":\"%s\",", $(col++);
		printf "\"active\":\"%s\",", $(col++);
		printf "\"action\":\"%s\",", $(col++);
		printf "\"actarg\":\"%s\",", $(col++);
		printf "\"appname\":\"%s\",", $(col++);
		printf "\"appcname\":\"%s\",", $(col++);
		printf "\"qps0\":\"%s\",", $(col++);
		printf "\"qps1\":\"%s\",", $(col++);
		printf "\"droppkts\":\"%s\",", $(col++);
		printf "\"pxystate\":\"%s\",", $(col++);
		printf "\"disabled\":\"%s\",", $(col++);
		printf "\"next\":\"%s\",", $(col++);
		printf "\"ipqps\":\"%s\",", $(col++);
		printf "\"matchpkts\":\"%s\",", $(col++);
		printf "\"usrtype\":\"%s\",", $(col++);
		printf "\"dnslist\":\"%s\",", $(col++);
		printf "\"ugroup_id\":\"%s\",", $(col++);
		printf "\"ugroup_name\":\"%s\"", $(col++);
		printf "}";
		if( dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


get_dnspolicy()
{
	${FLOWEYE} dnspolicy get id=${CGI_id} | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s", dot;
		printf "\"%s\":\"%s\"", $1, $2;
		if (dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


load_addrule_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"interface\":`lib_interface_list`"
	printf ",\"wangroup\":`lib_wangroup_list`"
	printf ",\"ippool\":`lib_ippool_list`"
	printf ",\"proxy\":`lib_proxy_list`"
	printf ",\"iptab_list\":`lib_iptable_list`"
	printf ",\"rtptime_list\":`lib_rtptime_list`"
	printf ",\"bridge_list\":`lib_bridge_list`"
	printf ",\"dnsgrp_list\":`lib_dnsgrp_list`"

	if [ "${CGI_id}" != "" ]; then
		printf ",\"policy\":`get_dnspolicy`"
	fi

	printf "}"
}


add_dnsrule()
{
	case "${CGI_intype}" in
		"ip")
			CGI_inip="any"
			;;

		"net")
			CGI_inip="${CGI_inipnet}"
			;;

		"iprange")
			CGI_inip="${CGI_iniprng}"
			;;

		"table")
			CGI_inip="${CGI_iniptbl}"
			;;
	esac

	case "${CGI_outtype}" in
		"ip")
			CGI_outip="any"
			;;

		"net")
			CGI_outip="${CGI_outipnet}"
			;;

		"iprange")
			CGI_outip="${CGI_outiprng}"
			;;

		"table")
			CGI_outip="${CGI_outiptbl}"
			;;
	esac

	case "${CGI_act}" in
		"rdr")
			CGI_actarg="${CGI_actproxy}"
			;;

		"reply")
			CGI_actarg="${CGI_actip}"
			;;

		"limit")
			CGI_actarg="${CGI_actqps}"
			;;

		*)
			CGI_actarg="null"
			;;
	esac

	[ "${CGI_ipqps}" = "" ] && CGI_ipqps="0"
	[ "${CGI_vlan}"  = "" ] && CGI_vlan="0"
	[ "${CGI_inif}"  = "" ] && CGI_inif="any"
	[ "${CGI_pool}" = "" ] && CGI_pool="0"
	[ "${CGI_dnslist}" = "" ] && CGI_dnslist="0.0.0.0"
	[ "${CGI_schtime}" = "" ] && CGI_schtime=0
	
	if [ "${CGI_action}" = "add_dnsrule" ]; then
		LOGMSG="${LANG002:=DNSܿز}"
		cmdargs="add id=${CGI_id}"
	else
		LOGMSG="${LANG003:=༭DNSܿز}"
		cmdargs="set id=${CGI_old_id} newid=${CGI_id}"
	fi

	cmdargs="${cmdargs} inip=${CGI_inip} outip=${CGI_outip} dns=${CGI_dns} vlan=${CGI_vlan}"
	cmdargs="${cmdargs} inif=${CGI_inif} app=${CGI_appid} action=${CGI_act} next=${CGI_next} ipqps=${CGI_ipqps} actarg=${CGI_actarg}"
	cmdargs="${cmdargs} usrtype=${CGI_usrtype} bridge=${CGI_bridge} pool=${CGI_pool} dnslist=${CGI_dnslist} schtime=${CGI_schtime}"

	errmsg=`${FLOWEYE} dnspolicy ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnspolicy ${cmdargs}"
		WEB_LOGGER "${LOGMSG}" "id=${CGI_id}"
		retjson 0 "${LANG001:=ɹ}"
	fi
}


rmv_dnspolicy()
{
	errmsg=`${FLOWEYE} dnspolicy remove id=${CGI_id}`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnspolicy remove id=${CGI_id}"
		WEB_LOGGER "${LANG004:=ɾDNSܿز}" "id=${CGI_id}"
		retjson 0 "${LANG001:=ɹ}"
	fi
}


enable_dnspolicy()
{
	errmsg=`${FLOWEYE} dnspolicy set id=${CGI_id} disable=0`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnspolicy set id=${CGI_id} disable=0"
		WEB_LOGGER "${LANG005:=DNSܿز}" "id=${CGI_id}"
		retjson 0 "${LANG001:=ɹ}"
	fi
}


disable_dnspolicy()
{
	errmsg=`${FLOWEYE} dnspolicy set id=${CGI_id} disable=1`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnspolicy set id=${CGI_id} disable=1"
		WEB_LOGGER "${LANG006:=DNSܿز}" "id=${CGI_id}"
		retjson 0 "${LANG001:=ɹ}"
	fi
}


btnenable_dnspolicy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dnspolicy set id=${id} disable=0`
		[ $? -ne 0 ] && retjson 1 "${errmsg}"
		sync_floweye "dnspolicy set id=${id} disable=0"
	done

	WEB_LOGGER "${LANG007:=DNSܿز}" "ids=${CGI_items}"
	retjson 0 "${LANG001:=ɹ}"
}


btndisable_dnspolicy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dnspolicy set id=${id} disable=1`
		[ $? -ne 0 ] && retjson 1 "${errmsg}"
		sync_floweye "dnspolicy set id=${id} disable=1"
	done

	WEB_LOGGER "${LANG008:=DNSܿز}" "ids=${CGI_items}"
	retjson 0 "${LANG001:=ɹ}"
}


btnremove_dnspolicy()
{
	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dnspolicy remove id=${id}`
		[ $? -ne 0 ] && retjson 1 "${errmsg}"
		sync_floweye "dnspolicy remove id=${id}"
	done

	WEB_LOGGER "${LANG009:=ɾDNSܿز}" "ids=${CGI_items}"
	retjson 0 "${LANG001:=ɹ}"
}


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

	"load_addrule_env")
		retjson 0 "OK" "`load_addrule_env`"
		;;

	"add_dnsrule"|"edit_dnsrule")
		action_check
		add_dnsrule
		;;

	"rmv_dnspolicy")
		action_check
		rmv_dnspolicy
		;;
	
	"enable_dnspolicy")
		action_check
		enable_dnspolicy
		;;
	
	"disable_dnspolicy")
		action_check
		disable_dnspolicy
		;;

	"btnenable_dnspolicy")
		action_check
		btnenable_dnspolicy
		;;

	"btndisable_dnspolicy")
		action_check
		btndisable_dnspolicy
		;;
	
	"btnremove_dnspolicy")
		action_check
		btnremove_dnspolicy
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
