#!/bin/sh

. ../common/ajax_common


get_switch_stat()
{
    ${FLOWEYE} switch stat | awk -F"=" \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        printf "%s", dot;
        printf "\"%s\":\"%s\"", $1, $2;
        if(dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


set_switch_stat()
{
    errmsg=`${FLOWEYE} switch config enable=${CGI_switch_enable} ttl=${CGI_swmac_ttl}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "switch config enable=${CGI_switch_enable} ttl=${CGI_swmac_ttl}"
        WEB_LOGGER "iWAN" "enable=${CGI_switch_enable} ttl=${CGI_swmac_ttl}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


load_iwan_list()
{
    ${FLOWEYE} sdwsvr list | awk \
    'BEGIN{
        dot = "";
        printf "{";
    }{
        col = 1;
        printf "%s", dot;
        printf "\"%s\":",$(2);
        printf "{";
        printf "\"server\":\"%s\",", $(col++);
        printf "\"id\":\"%s\",", $(col++);
        printf "\"srid\":\"%s\",", $(col++);
        printf "\"acct\":\"%s\",", $(col++);
        printf "\"token\":\"%s\",", $(col++);
        printf "\"mtu\":\"%s\",", $(col++);
        printf "\"ip\":\"%s\",", $(col++);
        printf "\"gateway\":\"%s\",", $(col++);
        printf "\"dns0\":\"%s\",", $(col++);
        printf "\"dns1\":\"%s\",", $(col++);
        printf "\"state\":\"%s\",", $(col++);
        printf "\"myip\":\"%s\",", $(col++);
        printf "\"myport\":\"%s\",", $(col++);
        printf "\"peerip\":\"%s\",", $(col++);
        printf "\"peerport\":\"%s\",", $(col++);
        printf "\"inbps\":\"%s\",", $(col++);
        printf "\"outbps\":\"%s\",", $(col++);
        printf "\"flowcnt\":\"%s\",", $(col++);
        printf "\"onlinetime\":\"%s\",", $(col++);
        printf "\"leftime\":\"%s\",", $(col++);
        printf "\"curdelay\":\"%s\",", $(col++);
        printf "\"mindelay\":\"%s\",", $(col++);
        printf "\"maxdelay\":\"%s\"", $(col++);
        printf "}";
        if (dot == "") dot = ",";
    }END{
        printf "}";
    }'
}


load_base_config()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"if\":`lib_interface_list`"
    printf ",\"iwan\":`load_iwan_list`"
    printf ",\"base\":`get_switch_stat`"
    printf "}"
}


list_vlan()
{
    ${FLOWEYE} switch list vlan | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        if($0 == "MODULE_NOT_ENABLE") next;
        
        col = 1;
        printf "%s", dot;
        printf "{";
        printf "\"vlan\":\"%s\",", $(col++);
        printf "\"dpi\":\"%s\",", $(col++);
        printf "\"policy\":\"%s\",", $(col++);
        printf "\"rx\":\"%s\",", $(col++);
        printf "\"uc\":\"%s\",", $(col++);
        printf "\"bc\":\"%s\",", $(col++);
        printf "\"cnt\":\"%s\",", $(col++);
        printf "\"ports\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


edit_vlan()
{
    cmdargs="vlan=${CGI_vlan} disable_dpi=${CGI_disable_dpi} disable_policy=${CGI_disable_policy}"
    errmsg=`${FLOWEYE} switch set ${cmdargs}`

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


del_vlan()
{
    errmsg=`${FLOWEYE} switch remove vlan=${CGI_vlan}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "switch remove vlan=${CGI_vlan}"
        WEB_LOGGER "ɾiWANVLAN" "vlan=${CGI_vlan}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


add_vlan_if()
{
    cmdargs="vlan=${CGI_vlan} if=${CGI_port} untag=${CGI_untag}"
    errmsg=`${FLOWEYE} switch add ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "switch add ${cmdargs}"
        WEB_LOGGER "VLANӿ" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


edit_vlan_if()
{
    cmdargs="vlan=${CGI_vlan} if=${CGI_port} untag=${CGI_untag}"
    errmsg=`${FLOWEYE} switch set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "switch set ${cmdargs}"
        WEB_LOGGER "༭VLANӿ" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


del_vlan_if()
{
    cmdargs="vlan=${CGI_vlan} if=${CGI_if}"
    errmsg=`${FLOWEYE} switch remove ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "switch remove ${cmdargs}"
        WEB_LOGGER "ɾVLANӿ" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


load_port_list()
{
    [ "${CGI_vlan}" != "" ] && cmdargs="vlan=${CGI_vlan}"

    ${FLOWEYE} switch list vport=1 ${cmdargs} | grep "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        if($0 == "NEXIST") next;

        col = 1;
        printf "%s", dot;
        printf "{";
        printf "\"vlan\":\"%s\",", $(col++);
        printf "\"portid\":\"%s\",", $(col++);
        printf "\"untag\":\"%s\",", $(col++);
        printf "\"rx\":\"%s\",", $(col++);
        printf "\"tx\":\"%s\",", $(col++);
        printf "\"port\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


list_mac()
{
    [ "${CGI_limit}" = "" ] && CGI_limit=100
    [ "${CGI_page}"  = "" ] && CGI_page=1
    end=$((${CGI_page} * ${CGI_limit}))
    start=$((${end} - ${CGI_limit}))

    ${FLOWEYE} switch list mac vlan=${CGI_vlan} port=${CGI_port} | grep -i "${CGI_keyword}" | awk \
    -v end=${end} -v start=${start} \
    'BEGIN{
        row = 0;
        dot = "";
        printf "{\"data\":[";
    }{
        row++;
        if(row > end || row < start) next;

        col = 1;
        printf "%s", dot;
        printf "{";
        printf "\"vlan\":\"%s\"", $(col++);
        printf ",\"mac\":\"%s\"", $(col++);
        printf ",\"ip\":\"%s\"", $(col++);
        printf ",\"last\":\"%s\"", $(col++);
        printf ",\"portid\":\"%s\"", $(col++);
        printf ",\"port\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "],\"total\":%s}", row;
    }'
}


case "${CGI_action}" in

    "get_switch_stat")
        retjson 0 "OK" "`get_switch_stat`"
        ;;

    "set_switch_stat")
        action_check
        set_switch_stat
        ;;

    "load_base_config")
        retjson 0 "OK" "`load_base_config`"
        ;;

    "list_vlan")
        retjson 0 "OK" "`list_vlan`"
        ;;

    "add_vlan")
        action_check
        add_vlan
        ;;

    "edit_vlan")
        action_check
        edit_vlan
        ;;

    "load_port_list")
        action_check
        retjson 0 "OK" "`load_port_list`"
        ;;

    "del_vlan")
        action_check
        del_vlan
        ;;

    "add_vlan_if")
        action_check
        add_vlan_if
        ;;

    "edit_vlan_if")
        action_check
        edit_vlan_if
        ;;

    "del_vlan_if")
        action_check
        del_vlan_if
        ;;

    "list_mac")
        retjson 0 "OK" "`list_mac`"
        ;;

    "load_iwan_list")
        retjson 0 "OK" "`load_iwan_list`"
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
