#!/bin/sh
. ../common/ajax_common


list_static_nat()
{
    ${FLOWEYE} stanat list | grep "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s{", dot;
		printf "\"id\":\"%s\",", $1;
        printf "\"disable\":\"%s\",", $2;
		printf "\"active\":\"%s\",", $3;
        printf "\"wan_name\":\"%s\",", $4;
        printf "\"pcip\":\"%s\",", $5;
        printf "\"wanip\":\"%s\",", $6;
		printf "\"gateway\":\"%s\",", $7;
		printf "\"desc\":\"%s\"", $8;
		printf "}";
		if(dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


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


load_add_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"info\":`info_static_nat`"
	printf ",\"proxy\":`lib_proxy_list wan`"
	printf "}"
}


static_nat_add()
{
	cmdarg="id=${CGI_id} pxy=${CGI_pxy} wanip=${CGI_wanip} pcip=${CGI_pcip} desc=${CGI_desc} gateway=${CGI_gateway}"
	errmsg=`${FLOWEYE} stanat add ${cmdarg}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "stanat add ${cmdarg}"
		WEB_LOGGER "Ӿ̬NAT" "${cmdarg}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


static_nat_edit()
{
	cmdarg="id=${CGI_id} newid=${CGI_newid} pxy=${CGI_pxy} wanip=${CGI_wanip} pcip=${CGI_pcip} desc=${CGI_desc} gateway=${CGI_gateway}"
	errmsg=`${FLOWEYE} stanat set ${cmdarg}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "stanat set ${cmdarg}"
		WEB_LOGGER "༭̬NAT" "${cmdarg}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


rmv_static_nat()
{
	errmsg=`${FLOWEYE} stanat remove id=${CGI_id}`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "stanat remove id=${CGI_id}"
		WEB_LOGGER "ɾ̬NAT" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


disabled_nat()
{
	errmsg=`${FLOWEYE} stanat set id=${CGI_id} disable=1`
	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "stanat set id=${CGI_id} disable=1"
		WEB_LOGGER "þ̬NAT" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


enable_nat()
{
	errmsg=`${FLOWEYE} stanat set id=${CGI_id} disable=0`

	if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "stanat set id=${CGI_id} disable=0"
		WEB_LOGGER "þ̬NAT" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
	fi
}


bat_disabled_nat()
{
	for id in `echo ${CGI_ids} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} stanat set id=$id disable=1`
		[ $? -ne 0 ] && retjson 1 "${errmsg}"
		sync_floweye "stanat set id=${id} disable=1"
	done
	
	WEB_LOGGER "þ̬NAT" "id=${CGI_ids}"
	retjson 0 "ɹ"
}


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

	WEB_LOGGER "þ̬NAT" "id=${CGI_ids}"
	retjson 0 "ɹ"
}


bat_remove_nat()
{
	for id in `echo ${CGI_ids} | tr ";" " "`
	do
		errmsg=`${FLOWEYE} stanat remove id=${id}`
		[ $? -ne 0 ] && retjson 1 "${errmsg}"
		sync_floweye "stanat remove id=${id}"
	done
	
	WEB_LOGGER "ɾ̬NAT" "id=${CGI_ids}"
	retjson 0 "ɹ"
}


case "${CGI_action}" in
	"list_static_nat")
		retjson 0 "OK" "`list_static_nat`"
		;;
	"load_add_env")
		retjson 0 "OK" "`load_add_env`"
		;;
	"static_nat_add")
		action_check
		static_nat_add
		;;
	"static_nat_edit")
		action_check
		static_nat_edit
		;;
	"rmv_static_nat")
		action_check
		rmv_static_nat
		;;
	"disabled_nat")
		action_check
		disabled_nat
		;;
	"enable_nat")
		action_check
		enable_nat
		;;
	"bat_disabled_nat")
		action_check
		bat_disabled_nat
		;;
	"bat_enable_nat")
		action_check
		bat_enable_nat
		;;
	"bat_remove_nat")
		action_check
		bat_remove_nat
		;;

	*)
		retjson 1 "UNKNOW_ACTION"
		;;
esac