#!/bin/sh

. ../../common/ajax_common

route_list()
{
	printf "["

	${FLOWEYE} route list type=${CGI_type} group=pkt | while read polno iniftype inifname proto appid appname srctype \
	srcip srcport dsttype dstip dstport dscp disabled vlan ttl action nhtype nexthop gwtype gateway state pkts \
	rtptimeid rtptimename active standby usrtype pid pname newdstip desc theothers
	do
		[ "${PALANG}" = "en" ] && appname=${appid}
		[ "${newdstip}" = "0.0.0.0" ] && newdstip=""
		[ "${desc}" = "NULL" ] && desc=""
		desc=`echo ${desc} | sed -r 's/\%20/ /g'`
		
		printf "${dot}{"
		printf "\"polno\":\"${polno}\","
		printf "\"iniftype\":\"${iniftype}\","
		printf "\"inifname\":\"${inifname}\","
		printf "\"proto\":\"${proto}\","
		printf "\"appid\":\"${appid}\","
		printf "\"appname\":\"${appname}\","
		printf "\"srctype\":\"${srctype}\","
		printf "\"srcip\":\"${srcip}\","
		printf "\"srcport\":\"${srcport}\","
		printf "\"dsttype\":\"${dsttype}\","
		printf "\"dstip\":\"${dstip}\","
		printf "\"dstport\":\"${dstport}\","
		printf "\"dscp\":\"${dscp}\","
		printf "\"disabled\":\"${disabled}\","
		printf "\"vlan\":\"${vlan}\","
		printf "\"ttl\":\"${ttl}\","
		printf "\"action\":\"${action}\","
		printf "\"nhtype\":\"${nhtype}\","
		printf "\"nexthop\":\"${nexthop}\","
		printf "\"gwtype\":\"${gwtype}\","
		printf "\"gateway\":\"${gateway}\","
		printf "\"state\":\"${state}\","
		printf "\"pkts\":\"${pkts}\","
		printf "\"rtptimeid\":\"${rtptimeid}\","
		printf "\"rtptimename\":\"${rtptimename}\","
		printf "\"active\":\"${active}\","
		printf "\"standby\":\"${standby}\","
		printf "\"usrtype\":\"${usrtype}\","
		printf "\"pid\":\"${pid}\","
		printf "\"pname\":\"${pname}\","
		printf "\"newdstip\":\"${newdstip}\","
		printf "\"desc\":\"${desc}\""
		printf "}"

		[ "${dot}" = "" ] && dot=","
	done
	
	printf "]"
}


route_remove()
{
	errmsg=`${FLOWEYE} route remove id=pkt.${CGI_id}`
	if [ "$?" != "0" ]; then
		retjson 1 "Error:${errmsg}"
	else
		[ "${PALANG}" = "en" ] && logmsg="Delete_Routing_Policy"
		WEB_LOGGER "${logmsg:=ɾת}" "id=${CGI_id}"
		retjson 0 "Success"
	fi
}


route_enable()
{
	errmsg=`${FLOWEYE} route enable id=pkt.${CGI_id}`
	if [ "$?" != "0" ]; then
		retjson 1 "Error:${errmsg}"
	else
		[ "${PALANG}" = "en" ] && logmsg="Enable_Routing_Policy"
		WEB_LOGGER "${logmsg:=ðת}" "id=${CGI_id}"
		retjson 0 "Success"
	fi
}


route_disable()
{
	errmsg=`${FLOWEYE} route disable id=pkt.${CGI_id}`
	if [ "$?" != "0" ]; then
		retjson 1 "Error:${errmsg}"
	else
		[ "${PALANG}" = "en" ] && logmsg="Enable_Routing_Policy"
		WEB_LOGGER "${logmsg:=·ɲ}" "id=${CGI_id}"
		retjson 0 "Success"
	fi
}


case "${CGI_action}" in
	"route_list")
		print_json_head
		route_list
		;;
	
	"route_remove")
		action_check
		route_remove
		;;

	"route_enable")
		action_check
		route_enable
		;;
	
	"route_disable")
		action_check
		route_disable
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
