#!/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()
{
	printf "["
	${FLOWEYE} dnspolicy list json=1
	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_list\":`lib_ippool_list`"
	printf ",\"proxy\":`lib_proxy_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_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_submit_srcip} outip=${CGI_submit_dstip} dns=${CGI_dns} vlan=${CGI_vlan} atype=${CGI_atype}"
	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}"
	
	# ͸Դַ
	if [ "${CGI_act}" = "rdr" ]; then
		[ "${CGI_nosnat}" = "on" ] && nosnat=1 || nosnat=0
		cmdargs="${cmdargs} nosnat=${nosnat}"
	fi

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

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnspolicy ${cmdargs}"
		WEB_LOGGER "${LOGMSG}" "${cmdargs}"
		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
