#!/bin/sh

. ../common/ajax_common


list_domain()
{
	printf "["
	
	${FLOWEYE} topdns list

	printf "]"
}


topdns_stat()
{
	${FLOWEYE} topdns stat | awk -F "=" \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		printf "%s\"%s\":\"%s\"", dot, $1, $2;
		if (dot == "") dot = ",";
	}END{
		printf "}";
	}'
}


edit_topdns()
{
	cmdargs=""
	cmdargs="${cmdargs} enable=${CGI_enable} ttl=${CGI_ttl}"
	cmdargs="${cmdargs} appdelay=${CGI_app0},${CGI_app1},${CGI_app2}"
	cmdargs="${cmdargs} dnsdelay=${CGI_dns0},${CGI_dns1},${CGI_dns2}"
	cmdargs="${cmdargs} colors=${CGI_color0},${CGI_color1},${CGI_color2},${CGI_color3}"
	errmsg=`${FLOWEYE} topdns config ${cmdargs}`

	[ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"
	
	${FLOWEYE} npmflow config enable=${CGI_enable}
	[ $? -ne 0 ] && retjson 1 "ʧܣ${errmsg}"

	WEB_LOGGER "TOP" "${cmdargs}"
	retjson 0 "ɹ"
}


case "${CGI_action}" in

	"list_domain")
		retjson 0 "OK" "`list_domain`"
		;;

	"topdns_stat")
		retjson 0 "OK" "`topdns_stat`"
		;;
	
	"edit_topdns")
		action_check
		edit_topdns
		;;
	
	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac

