#!/bin/sh

. ../common/ajax_common


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


load_dot1x_config()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"radsvr\":`lib_radsvr_list`"
    printf ",\"config\":`get_dot1x_stat`"
    printf "}"
}


set_dot1x_config()
{
    cmdargs="ttl=${CGI_ttl} radsvrid=${CGI_dot1x_radsvrid}"
    errmsg=`${FLOWEYE} dot1x config ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧܣ${errmsg}"
    else
        sync_floweye "dot1x config ${cmdargs}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


list_dot1x_rule()
{
    ${FLOWEYE} dot1x list | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col=1;

        printf "%s{", dot;
        printf "\"id\":\"%s\"", $(col++);
        printf ",\"if\":\"%s\"", $(col++);
        printf ",\"disable\":\"%s\"", $(col++);
        printf ",\"vlan\":\"%s\"", $(col++);
        printf ",\"desc\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


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


load_addrule_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"interface\":`lib_interface_list`"

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

    printf "}"
}


add_dot1x_rule()
{
    cmdargs="id=${CGI_id} inif=${CGI_inif} vlan=${CGI_vlan} desc=${CGI_desc}"

    errmsg=`${FLOWEYE} dot1x add ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x add ${cmdargs}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


set_dot1x_rule()
{
    cmdargs="id=${CGI_id} inif=${CGI_inif} vlan=${CGI_vlan} desc=${CGI_desc}"

    errmsg=`${FLOWEYE} dot1x set ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x set ${cmdargs}"
        WEB_LOGGER "༭802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


enable_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x enable id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x enable id=${CGI_id}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


disable_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x disable id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x disable id=${CGI_id}"
        WEB_LOGGER "802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


rmv_dot1x_rule()
{
    errmsg=`${FLOWEYE} dot1x remove id=${CGI_id}`

    if [ $? -ne 0 ]; then
        retjson 1 "ʧ:${errmsg}"
    else
        sync_floweye "dot1x remove id=${CGI_id}"
        WEB_LOGGER "ɾ802.1x֤" "${cmdargs}"
        retjson 0 "ɹ"
    fi
}


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

	WEB_LOGGER "802.1x֤" "ids=${CGI_items}"
	retjson 0 "ɹ"
}


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

    WEB_LOGGER "802.1x֤" "ids=${CGI_items}"
    retjson 0 "ɹ"
}


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

    WEB_LOGGER "ɾ802.1x֤" "ids=${CGI_items}"
    retjson 0 "ɹ"
}


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

    ${FLOWEYE} dot1x list client=1 | grep -i "${CGI_keyword}" | awk -F" " \
	-v start=${start} -v end=${end} \
    'BEGIN{
        row = 0;
        dot = "";
        printf "{\"list\":[";
    }{
        row++;
        if( row > end  || row <= start ) next;

        col=1;

        printf "%s{", dot;
        printf "\"id\":\"%s\"", $(col++);
        printf ",\"name\":\"%s\"", $(col++);
        printf ",\"mac\":\"%s\"", $(col++);
        printf ",\"stat\":\"%s\"", $(col++);
        printf ",\"ipv4\":\"%s\"", $(col++);
        printf ",\"ipv6\":\"%s\"", $(col++);
        printf ",\"if\":\"%s\"", $(col++);
        printf ",\"vlan\":\"%s\"", $(col++);
        printf ",\"active_time\":\"%s\"", $(col++);
        printf ",\"create_time\":\"%s\"", $(col++);
        printf ",\"count\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "],\"total\":\"%s\"}", row;
    }'
}


case "${CGI_action}" in

    "load_dot1x_config")
        retjson 0 "OK" "`load_dot1x_config`"
        ;;

    "set_dot1x_config")
        action_check
        set_dot1x_config
        ;;

    "list_dot1x_rule")
        retjson 0 "OK" "`list_dot1x_rule`"
        ;;

    "load_addrule_env")
        retjson 0 "OK" "`load_addrule_env`"
        ;;

    "add_dot1x_rule")
        action_check
        add_dot1x_rule
        ;;

    "set_dot1x_rule")
        action_check
        set_dot1x_rule
        ;;

    "enable_dot1x_rule")
        action_check
        enable_dot1x_rule
        ;;

    "disable_dot1x_rule")
        action_check
        disable_dot1x_rule
        ;;

    "rmv_dot1x_rule")
        action_check
        rmv_dot1x_rule
        ;;

    "bat_enable_dot1x_rule")
        action_check
        bat_enable_dot1x_rule
        ;;

    "bat_disable_dot1x_rule")
        action_check
        bat_disable_dot1x_rule
        ;;

    "bat_rmv_dot1x_rule")
        action_check
        bat_rmv_dot1x_rule
        ;;

    "list_dot1x_client")
        retjson 0 "OK" "`list_dot1x_client`"
        ;;
        
    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
        
esac
