#!/bin/sh

. ../common/ajax_common

list_domain_route()
{
	printf "["
	${FLOWEYE} dnrt list json=1
	printf "]"
}


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


load_add_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"dnsgroup\":`lib_dnsgrp_list`"
	printf ",\"proxy\":`lib_proxy_list wan`"

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

	printf "}"
}


add_domain_route()
{
	cmdargs="id=${CGI_id} dns=${CGI_dnsid} pxy=${CGI_pxy} dnsaddr=${CGI_dnsaddr} desc=${CGI_desc}"
	cmdargs="${cmdargs} trackhost=${CGI_trackhost} cachettl=${CGI_cachettl} bkuppxy=${CGI_bkuppxy}"
	
	errmsg=`${FLOWEYE} dnrt add ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnrt add ${cmdargs}"
		WEB_LOGGER "·ɲ" "${cmdargs}"
		retjson 0 "ɹ"
	fi
}


edit_domain_route()
{
	cmdargs="id=${CGI_id} dns=${CGI_dnsid} pxy=${CGI_pxy} dnsaddr=${CGI_dnsaddr} desc=${CGI_desc}"
	cmdargs="${cmdargs} trackhost=${CGI_trackhost} cachettl=${CGI_cachettl} bkuppxy=${CGI_bkuppxy}"
	
	[ "${CGI_newid}" != "${CGI_id}" ] && cmdargs="${cmdargs} newid=${CGI_newid}"
	
	errmsg=`${FLOWEYE} dnrt set ${cmdargs}`

	if [ $? -ne 0 ]; then
		retjson 1 "${errmsg}"
	else
		sync_floweye "dnrt set ${cmdargs}"
		WEB_LOGGER "༭·ɲ" "${cmdargs}"
		retjson 0 "ɹ"
	fi
}


remove_domain_route()
{
	errmsg=`${FLOWEYE} dnrt remove id=${CGI_id}`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧ:${errmsg}"
	else
		sync_floweye "dnrt remove id=${CGI_id}"
		WEB_LOGGER "ɾ·ɲ" "id=${CGI_id}"
		retjson 0 "ɹ"
	fi
}


disable_domain_route()
{
	errmsg=`${FLOWEYE} dnrt set id=${CGI_id} disable=1`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧ:${errmsg}"
	else
		sync_floweye "dnrt remove id=${CGI_id}"
		WEB_LOGGER "·ɲ" "id=${CGI_id}"
		retjson 0 "ɹ"
	fi
}


enable_domain_route()
{
	errmsg=`${FLOWEYE} dnrt set id=${CGI_id} disable=0`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧ:${errmsg}"
	else
		sync_floweye "dnrt remove id=${CGI_id}"
		WEB_LOGGER "·ɲ" "id=${CGI_id}"
		retjson 0 "ɹ"
	fi
}


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

	WEB_LOGGER "·ɲ" "id=${CGI_items}"
	retjson 0 "ɹ"
}


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

	WEB_LOGGER "·ɲ" "id=${CGI_items}"
	retjson 0 "ɹ"
}


btnremove_domain_route()
{

	for id in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dnrt remove id=${id}`
		[ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
	done

	WEB_LOGGER "ɾ·ɲ" "id=${CGI_items}"
	retjson 0 "ɹ"
}


list_active_domain()
{
	case "${CGI_sort_key}" in
		"ipcnt")
			sort_key="-nk6"
			;;

		"time")
			sort_key="-k5"
			;;

		*)
			sort_key="-nk6"
			;;
	esac

	[ "${CGI_sort_type}" = "desc" -o "${CGI_sort_type}" = "" ] && sort_type="-r"

	[ "${CGI_limit}" = "" ] && CGI_limit=100
	[ "${CGI_page}" = "" ] && CGI_page=0

	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	${FLOWEYE} dnrthost list id=${CGI_id} | grep -i "${CGI_keyword}"  | sort ${sort_type} ${sort_key} | awk \
	-v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(NF < 5) next;

		row++;
		if( row > end  || row < start ) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"domain\":\"%s\"", $(col++);
		printf ",\"cname\":\"%s\"", $(col++);
		printf ",\"policy_id\":\"%s\"", $(col++);
		printf ",\"urlgrp_id\":\"%s\"", $(col++);
		printf ",\"time\":\"%s\"", $(col++);
		printf ",\"ipcnt\":\"%s\"", $(col++);
		printf ",\"ips\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


rmv_active_domain()
{

	if [ ${CGI_type} = 'ip' ]; then
		cmdargs="ip="
	else
		cmdargs="name="
	fi

	errmsg=`${FLOWEYE} dnrthost remove ${cmdargs}${CGI_domain}`

	if [ $? -ne 0 ]; then
		retjson 1 "ʧܣ${errmsg}"
	else
		retjson 0 "ɹ"
	fi
}


btnrmv_active_domain()
{
	if [ ${CGI_type} = 'ip' ]; then
		cmdargs="ip="
	else
		cmdargs="name="
	fi

	for name in `echo ${CGI_items} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} dnrthost remove ${cmdargs}${name}`
		[ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
	done

	WEB_LOGGER "ɾٻԾ/IP" "${CGI_items}"
	retjson 0 "ɹ"
}


btnrmv_all_active_domain()
{

	if [ ${CGI_type} = 'name' ]; then
		cmdargs="name="
	else
		cmdargs="ip="
		ifIp="listip=1"
	fi
	
	${FLOWEYE} dnrthost list id=${CGI_id} ${ifIp} | while read item
	do
		name=`echo ${item} | cut -d" " -f1`
		errmsg=`${FLOWEYE} dnrthost remove ${cmdargs}${name}`
		[ $? -ne 0 ] && retjson 1 "ʧ:${errmsg}"
	done

	WEB_LOGGER "ɾٻԾ/IP" "ID=${CGI_id}"
	retjson 0 "ɹ"
}


list_active_ip()
{
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	[ "${CGI_page}" = "" ] && CGI_page=0

	end=$(( ${CGI_page} * ${CGI_limit} ))
	start=$(( ${end} - ${CGI_limit} ))

	${FLOWEYE} dnrthost list id=${CGI_id} listip=1 | grep -i "${CGI_keyword}"  | awk \
	-v start=${start} -v end=${end} \
	'BEGIN{
		row = 0;
		dot = "";
		printf "{\"data\":[";
	}{
		if(NF < 5) next;

		row++;
		if( row > end  || row < start ) next;

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ip\":\"%s\",", $(col++);
		printf "\"time\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"domain\":\"%s\",", $(col++);
		printf "\"urlgrp_id\":\"%s\",", $(col++);
		printf "\"urlgrp_name\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


case "${CGI_action}" in

	"set_segrt")
		action_check
		set_segrt
		;;

	"get_segrt")
		action_check
		get_segrt
		;;

	"list_domain_route")
		retjson 0 "OK" "`list_domain_route`"
		;;

	"load_add_env")
		retjson 0 "OK" "`load_add_env`"
		;;

	"add_domain_route")
		action_check
		add_domain_route
		;;

	"edit_domain_route")
		action_check
		edit_domain_route
		;;

	"remove_domain_route")
		action_check
		remove_domain_route
		;;

	"disable_domain_route")
		action_check
		disable_domain_route
		;;

	"enable_domain_route")
		action_check
		enable_domain_route
		;;

	"btndisable_domain_route")
		action_check
		btndisable_domain_route
		;;

	"btnenable_domain_route")
		action_check
		btnenable_domain_route
		;;

	"btnremove_domain_route")
		action_check
		btnremove_domain_route
		;;

	"list_active_domain")
		retjson 0 "OK" "`list_active_domain`"
		;;

	"list_active_ip")
		retjson 0 "OK" "`list_active_ip`"
		;;

	"rmv_active_domain")
		action_check
		rmv_active_domain
		;;

	"btnrmv_active_domain")
		action_check
		btnrmv_active_domain
		;;

	"btnrmv_all_active_domain")
		action_check
		btnrmv_all_active_domain
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac
