#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="Set_Radius_Stat"		#Radius״̬
	LANG004="Set_Radius_Server"		#Radius
	LANG005="Delete_Radius_Server"	#ɾRadius
	LANG006="Delete_Radius_Server"	#ɾRadius
fi


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


set_radsvr_stat()
{
    [ "${CGI_enable}" = "on" ] && enable=1 || enable=0
    [ "${CGI_rad_acctinternal}" = "" ] && CGI_rad_acctinternal=300
    [ "${CGI_rad_ttl}" = "" ] && CGI_rad_ttl=30
    [ "${CGI_rad_nasidentifier}" = "" ] && CGI_rad_nasidentifier="Panabit"

    cmdargs="enable=${enable} acctinternal=${CGI_acctinternal} ttl=${CGI_ttl} nasidentifier=${CGI_nasidentifier}"

    errmsg=`${FLOWEYE} radius config ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "radius config ${cmdargs}"
        WEB_LOGGER "${LANG003:=Radius״̬}" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


list_radius_server()
{
    ${FLOWEYE} radsvr list | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"id\":\"%s\",", $(col++);
        printf "\"ip\":\"%s\",", $(col++);
        printf "\"name\":\"%s\",", $(col++);
        printf "\"authport\":\"%s\",", $(col++);
        printf "\"acctport\":\"%s\",", $(col++);
        printf "\"secret\":\"%s\",", $(col++);
        printf "\"pxyid0\":\"%s\",", $(col++);
        printf "\"active0\":\"%s\",", $(col++);
        printf "\"pxyname0\":\"%s\",", $(col++);
        printf "\"rteid0\":\"%s\",", $(col++);
        printf "\"pxyid1\":\"%s\",", $(col++);
        printf "\"active1\":\"%s\",", $(col++);
        printf "\"pxyname1\":\"%s\",", $(col++);
        printf "\"rteid1\":\"%s\",", $(col++);
        printf "\"authreq\":\"%s\",", $(col++);
        printf "\"authack\":\"%s\",", $(col++);
        printf "\"authrej\":\"%s\",", $(col++);
        printf "\"authtimeout\":\"%s\",", $(col++);
        printf "\"acctreq\":\"%s\",", $(col++);
        printf "\"acctack\":\"%s\",", $(col++);
        printf "\"accttimeout\":\"%s\",", $(col++);
        printf "\"disconnect\":\"%s\",", $(col++);
        printf "\"domain\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


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


load_addradius_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"proxy_list\":`lib_proxy_list`"

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

    printf "}"
}


add_radius()
{
    [ "${CGI_action}" = "edit_radius" ] && action="set id=${CGI_id}" || action="add"

    [ "${CGI_authport}" = "" ] && CGI_authport=1812
    [ "${CGI_acctport}" = "" ] && CGI_acctport=1813
    [ "${CGI_discport}" = "" ] && CGI_discport=3799
    [ "${CGI_secret}" = "" ] && CGI_secret="panabit"
    [ "${CGI_proxy}" = "" ] && CGI_proxy="NULL"
    [ "${CGI_proxy2}" = "" ] && CGI_proxy2="NULL"

    cmdargs="ip=${CGI_ip} authport=${CGI_authport} acctport=${CGI_acctport} secret=${CGI_secret} proxy=${CGI_proxy}"
	cmdargs="${cmdargs}  proxy2=${CGI_proxy2} desc=${CGI_desc} nexthop=${CGI_nexthop} domain=${CGI_domain}"

	errmsg=`${FLOWEYE} radsvr ${action} ${cmdargs}`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "radsvr ${action} ${cmdargs}"
        WEB_LOGGER "${LANG004:=Radius}" "${action} ${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


rmv_radius()
{
    errmsg=`${FLOWEYE} radsvr remove id=${CGI_id}`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "radsvr remove id=${CGI_id}"
        WEB_LOGGER "${LANG005:=ɾRadius}" "id=${CGI_id} name=${CGI_name}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


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

    WEB_LOGGER "${LANG006:=ɾRadius}" "items=${CGI_items}"
	retjson 0 "${LANG002:=ɹ}"
}


case "${CGI_action}" in
    "load_radsvr_stat")
        retjson 0 "OK" "`load_radsvr_stat`"
        ;;
    
    "set_radsvr_stat")
        action_check
        set_radsvr_stat
        ;;

    "list_radius_server")
        retjson 0 "OK" "`list_radius_server`"
        ;;

    "load_addradius_env")
        retjson 0 "OK" "`load_addradius_env`"
        ;;

    "add_radius"|"edit_radius")
        action_check
        add_radius
        ;;
    
    "rmv_radius")
        action_check
        rmv_radius
        ;;

    "btnrmv_radius")
        action_check
        btnrmv_radius
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;

esac
