#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then 
	LANG001="Error"		#ʧ
	LANG002="Success"	#ɹ
	LANG003="SET_L2BYPASS_STAT"		#PPPOE·
	LANG004="SET_L2BYPASS_SERVER"	#PPPOE·
	LANG005="SET_L2BYPASS_ACCOUNT"	#PPPOE·˺
	LANG006="DEL_L2BYPASS_ACCOUNT"	#ɾPPPOE·˺
	LANG007="IMPORT_L2BYPASS_ACCOUNT"	#PPPOE·˺
	LANG008="EXPORT_L2BYPASS_ACCOUNT"	#PPPOE·˺
fi


load_l2bypass_stat()
{
    printf "{"
    printf "\"_\":\"_\""

    ${FLOWEYE} l2route stat | awk -F "=" \
    '{
        printf ",\"%s\":\"%s\"", $1, $2;
    }'

    printf "}"
}


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

    cmdargs="enable=${enable} nedeny=${CGI_nedeny}"
    errmsg=`${FLOWEYE} l2route config ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        WEB_LOGGER "${LANG003:=PPPOE·}" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


load_l2bypass_svrlist()
{
    #id service ifname vlan refcnt bpsin bpsout
	${FLOWEYE} l2route list | awk \
	'BEGIN{
		dot = "";
        printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
        printf "\"id\":\"%s\",", $(col++);
        printf "\"service\":\"%s\",", $(col++);
		printf "\"ifname\":\"%s\",", $(col++);
		printf "\"vlan\":\"%s\",", $(col++);
		printf "\"refcnt\":\"%s\",", $(col++);
		printf "\"bpsin\":\"%s\",", $(col++);
		printf "\"bpsout\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
        printf "]";
	}'
}


set_l2bypass_service()
{
    cmdargs="id=${CGI_id}"

    if [ "${CGI_service}" != "" -a "${CGI_ifname}" != "" ]; then
        [ "${CGI_vlan}" = "" ] && CGI_vlan=0
        cmdargs="${cmdargs} service=${CGI_service} vlan=${CGI_vlan} if=${CGI_ifname}"
    fi

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

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


load_l2bypass_maclist()
{
    #id service mac inbps outbps ttl birth account expdeny nexist theothers
    ${FLOWEYE} l2route list type=mac id=${CGI_id} | awk \
	'BEGIN{
		dot = "";
        printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
        printf "\"id\":\"%s\",", $(col++);
        printf "\"service\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"ttl\":\"%s\",", $(col++);
		printf "\"birth\":\"%s\",", $(col++);
		printf "\"account\":\"%s\",", $(col++);
		printf "\"expdeny\":\"%s\",", $(col++);
		printf "\"nexist\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
        printf "]";
	}'
}


load_l2bypass_account()
{
    #gid group name leftime expire desc
    ${FLOWEYE} l2route list type=account id=${CGI_id} | grep -i "${CGI_keyword}" | awk \
	'BEGIN{
		dot = "";
        printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
        printf "\"gid\":\"%s\",", $(col++);
        printf "\"group\":\"%s\",", $(col++);
		printf "\"name\":\"%s\",", $(col++);
		printf "\"leftime\":\"%s\",", $(col++);
		printf "\"expire\":\"%s\",", $(col++);
		printf "\"desc\":\"%s\"", $(col++);
		printf "}";
		if(dot == "") dot = ",";
	}END{
        printf "]";
	}'
}


add_l2bypass_account()
{
    cmdargs="account=${CGI_name} expire=${CGI_expire} gid=${CGI_gid}"
	[ "${CGI_desc}" != "" ] && cmdargs="${cmdargs} memo=${CGI_desc}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        WEB_LOGGER "${LANG005:=PPPOE·˺}" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


rmv_l2bypass_account()
{
    cmdargs=""

    for acct in `echo ${CGI_name} | tr ";" " "`
    do
		cmdargs="${cmdargs} account=${acct}"
    done

    errmsg=`${FLOWEYE} l2route remove ${cmdargs}`

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        WEB_LOGGER "${LANG006:=ɾPPPOE·˺}" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


import_l2bypass_account()
{
	errmsg=`${FLOWEYE} l2route config loadacct=${CGI_file}`

    if [ $? -ne 0 ]; then
        rm -rf ${CGI_file}
        retjson 1 "${LANG001:=ʧ}${errmsg}"
    else
        rm -rf ${CGI_file}
        WEB_LOGGER "${LANG007:=PPPOE·˺}" "${CGI_name}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


export_l2bypass_account()
{
    file_name="l2bypass.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"
    src_file="${PGETC}/l2rt_account.conf"

    mkdir -p ${WEB_DOWNLOAD}

    if [ -f "${src_file}" ]; then
        cp ${src_file} ${file_path}
    else
        touch ${file_path}
    fi

    WEB_LOGGER "${LANG008:=PPPOE·˺}"
    retjson 0 "OK" "\"${file_name}\""
}


case "${CGI_action}" in
    "load_l2bypass_stat")
        retjson 0 "OK" "`load_l2bypass_stat`"
        ;;
    
    "set_l2bypass_stat")
        action_check
        set_l2bypass_stat
        ;;

    "load_l2bypass_svrlist")
        retjson 0 "OK" "`load_l2bypass_svrlist`"
        ;;

    "set_l2bypass_service")
        action_check
        set_l2bypass_service
        ;;

    "load_l2bypass_maclist")
        retjson 0 "OK" "`load_l2bypass_maclist`"
        ;;

    "load_l2bypass_account")
        retjson 0 "OK" "`load_l2bypass_account`"
        ;;

    "add_l2bypass_account")
        action_check
        add_l2bypass_account
        ;;

    "rmv_l2bypass_account")
        action_check
        rmv_l2bypass_account
        ;;

    "import_l2bypass_account")
        action_check
        import_l2bypass_account
        ;;

    "export_l2bypass_account")
        action_check
        export_l2bypass_account
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
