#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_URL_Blance_Policy"		#ز
	LANG004="Enable_URL_Blance_Policy"	#ز
	LANG005="Disable_URL_Blance_Policy"	#ز
	LANG006="Delete_URL_Blance_Policy"	#ɾز
	LANG007="Enable_URL_Blance_Policy"	#
	LANG008="Disable_URL_Blance_Policy"	#
	LANG009="Delete_URL_Blance_Policy"	#ɾ
fi


list_urlmap()
{
	${FLOWEYE} dnmap list | grep -i "${CGI_keyword}" | awk \
    -v disable="${CGI_disable}" \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if (disable != "" && $12 != disable) next;

		gsub("%20", " ", $16);

		col = 1;

		printf "%s", dot;
		printf "{";
		printf "\"id\":\"%s\",", $(col++);
		printf "\"wanid\":\"%s\",", $(col++);
		printf "\"wanname\":\"%s\",", $(col++);
		printf "\"wantype\":\"%s\",", $(col++);
		printf "\"dst\":\"%s\",", $(col++);
		printf "\"port\":\"%s\",", $(col++);
		printf "\"dname\":\"%s\",", $(col++);
		printf "\"maptype\":\"%s\",", $(col++);
		printf "\"mapip\":\"%s\",", $(col++);
		printf "\"mapport\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"gateway\":\"%s\",", $(col++);
		printf "\"count\":\"%s\",", $(col++);
		printf "\"rtif\":\"%s\",", $(col++);
		printf "\"disable\":\"%s\",", $(col++);
		printf "\"snattype\":\"%s\",", $(col++);
		printf "\"snatip\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\"", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


list_server()
{
	[ "${CGI_group}" != "" ] && args="group=${CGI_group}"
	
	printf "["
	${FLOWEYE} server list ${args} 
	printf "]"
}


list_svrgroup()
{
	${FLOWEYE} svrgroup list | awk \
	'BEGIN{
		dot="";
		printf "[";
	}{
		printf "%s{", dot;
		printf "\"id\":%d,", $1;
		printf "\"name\":\"%s\"", $2;
		printf "}";
		if(dot == "") dot=","
	}END{
		printf "]";
	}'
}


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


load_add_urlmap_env()
{
	. ../common/ajax_object_lib

	printf "{"
	printf "\"server\":`list_server`"
	printf ",\"wan\":`lib_proxy_list wan`"
	printf ",\"svrgroup\":`list_svrgroup`"

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

	printf "}"
}


add_urlmap()
{
	[ "${CGI_dst}" = "" ] && CGI_dst="0.0.0.0"
	[ "${CGI_gateway}" = "" ] && CGI_gateway="0.0.0.0"
	[ "${CGI_mapport}" = "" ] && CGI_mapport=0

	if [ "${CGI_wan}" = "any" -a "${CGI_dst}" = "0.0.0.0" ]; then
		retjson 1 "NO_WAN_AND_DST"
	fi

	[ "${CGI_action}" = "edit_urlmap" ] && action="set" || action="add"

	if [ "${CGI_maptype}" = "svr" ]; then
		[ "${CGI_server}" = "" ] && retjson 1 "NO_SERVER"
		CGI_mapip="${CGI_server}"
	elif [ "${CGI_maptype}" = "sg" ]; then
		[ "${CGI_svrgrp}" = "" ] && retjson 1 "NO_SERVER_GROUP"
		CGI_mapip="${CGI_svrgrp}"
	fi

	cmdargs="${action} id=${CGI_id} wan=${CGI_wan} dst=${CGI_dst} port=${CGI_port} proto=${CGI_proto} dname=${CGI_dname}"
	cmdargs="${cmdargs} mapip=${CGI_mapip} mapport=${CGI_mapport} gateway=${CGI_gateway}"
	cmdargs="${cmdargs} disable=${CGI_disable}"

	if [ "${CGI_snattype}" = "ip" ]; then
		cmdargs="${cmdargs} snattype=${CGI_snattype} snatip=${CGI_snatip}"
	else
		cmdargs="${cmdargs} snattype=${CGI_snattype}"
	fi

	CGI_note=`echo ${CGI_note} | sed -r 's/ /\%20/g'`
	[ "${CGI_note}" != "" ] && cmdargs="${cmdargs} note=${CGI_note}"

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

	if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
	else
		sync_floweye "dnmap ${cmdargs}"
		WEB_LOGGER "${LANG003:=ز}" "${action} id=${CGI_id} dst=${CGI_dst} "
		retjson 0 "${LANG002:=ɹ}"
	fi
}


enable_urlmap()
{
    errmsg=`${FLOWEYE} dnmap set id=${CGI_id} disable=0`

    if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
		sync_floweye "dnmap set id=${CGI_id} disable=0"
        WEB_LOGGER "${LANG004:=ز}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


disable_urlmap()
{
    errmsg=`${FLOWEYE} dnmap set id=${CGI_id} disable=1`

    if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
		sync_floweye "dnmap set id=${CGI_id} disable=1"
        WEB_LOGGER "${LANG005:=ز}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


rmv_urlmap()
{
    errmsg=`${FLOWEYE} dnmap remove id=${CGI_id}`

    if [ "$?" != "0" ]; then
		retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
		sync_floweye "dnmap remove id=${CGI_id}"
        WEB_LOGGER "${LANG006:=ɾز}" "id=${CGI_id}"
		retjson 0 "${LANG002:=ɹ}"
    fi
}


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

    WEB_LOGGER "${LANG007:=}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


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

    WEB_LOGGER "${LANG008:=}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


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

    WEB_LOGGER "${LANG009:ɾ}" "ids=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "list_urlmap")
        retjson 0 "OK" "`list_urlmap`"
        ;;

    "load_add_urlmap_env")
        retjson 0 "OK" "`load_add_urlmap_env`"
        ;;

    "add_urlmap"|"edit_urlmap")
        action_check
        add_urlmap
        ;;

    "enable_urlmap")
        action_check
        enable_urlmap
        ;;

    "disable_urlmap")
        action_check
        disable_urlmap
        ;;
    
    "rmv_urlmap")
        action_check
        rmv_urlmap
        ;;
    
    "btnenable_urlmap")
        action_check
        btnenable_urlmap
        ;;
    
    "btndisable_urlmap")
        action_check
        btndisable_urlmap
        ;;
    
    "btnrmv_urlmap")
        action_check
        btnrmv_urlmap
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
