#!/bin/sh

. ../common/ajax_common


if [ "${PALANG}" = "en" ]; then
    LANG001="Error"		#ʧ
    LANG002="Success"	#ɹ
fi


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


load_set_env()
{
    . ../common/ajax_object_lib

    printf "{"
    printf "\"iptab\":`lib_iptable_list`"
    printf ",\"conf\":`load_config`"
    printf "}"
}


set_config()
{
    cmdargs=""
    cmdargs="${cmdargs} enable=${CGI_enable}"
    cmdargs="${cmdargs} whitelist=${CGI_whitelist}"
    cmdargs="${cmdargs} start=${CGI_start}"
    cmdargs="${cmdargs} end=${CGI_end}"

    newttl=`${FLOWEYE} phonewifi stat | grep "newttl=" | cut -d"=" -f2`
    if [ ${newttl} -le 2 -o ${CGI_newttl} = "1" ]; then
        cmdargs="${cmdargs} newttl=${CGI_newttl}"
    fi

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "phonewifi config ${cmdargs}"
        WEB_LOGGER "繲ģ" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


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

    "load_set_env")
        retjson 0 "OK" "`load_set_env`"
        ;;
    
    "set_config")
        action_check
        set_config
        ;;

esac
