#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_Dynamic_Limit"	#·̬
	LANG004="Set_Time_Limit"	#·ʱδ
fi


list_plink()
{
    ${FLOWEYE} policy listlink | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"name\":\"%s\",", $(col++);
        printf "\"id\":\"%s\",", $(col++);
        printf "\"type\":\"%s\",", $(col++);
        printf "\"bandwidth\":\"%s\",", $(col++);
        printf "\"enable\":\"%s\",", $(col++);
        printf "\"holdtime\":\"%s\",", $(col++);
        printf "\"lowwater\":\"%s\",", $(col++);
        printf "\"highwater\":\"%s\",", $(col++);
        printf "\"speedup\":\"%s\",", $(col++);
        printf "\"speeddown\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


get_plink()
{
    printf "{"
    printf "\"stat\":{"

    ${FLOWEYE} policy getlink link=${CGI_name} | awk -F"=" \
    'BEGIN{
        dot = "";   
    }{
        printf "%s\"%s\":\"%s\"", dot, $1, $2;
        if(dot == "") dot = ",";
    }'

    printf "},\"sch\":["

    ${FLOWEYE} policy getlinksch link=${CGI_name} | awk \
    'BEGIN{
        dot = "";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"day\":\"%s\",", $(col++);
        printf "\"time\":\"%s\",", $(col++);
        printf "\"bandwidth\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }'

    printf "]}"
}


set_plink_stat()
{
    enable=0
    [ "${CGI_enable}" = "on" ] && enable=1

    linkargs="link=${CGI_id} name="${CGI_name}" bandwidth=${CGI_bandwidth}"
    linkargs="${linkargs} enable=${enable} holdtime=${CGI_holdtime}"
    linkargs="${linkargs} lowwater=${CGI_lowwater} speedup=${CGI_speedup}"
    linkargs="${linkargs} highwater=${CGI_highwater} speeddown=${CGI_speeddown}"

    errmsg=`${FLOWEYE} policy setlink ${linkargs}`

    if [ "$?" != "0" ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "policy setlink ${linkargs}"
        WEB_LOGGER "${LANG003:=·̬}" "${linkargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


set_plink_sch()
{
    [ "${CGI_id}" = "" ] && retjson 1 "INV_LINK"

    cmdargs="link=${CGI_id} day=${CGI_day} time=${CGI_time} bandwidth=${CGI_bandwidth}"
    errmsg=`${FLOWEYE} policy setlinksch ${cmdargs}`

    if [ "$?" != "0" ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "policy setlinksch ${cmdargs}"
        WEB_LOGGER "${LANG004:=·ʱδ}" " ${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


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

    "get_plink")
        retjson 0 "OK" "`get_plink`"
        ;;

    "set_plink_stat")
        action_check
        set_plink_stat
        ;;

    "set_plink_sch")
        action_check
        set_plink_sch
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
