#!/bin/sh

. ../common/ajax_common

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"			#ʧ
	LANG002="Success"		#ɹ
	LANG003="Set_IP/MAC_Binding"	#IP/MAC
	LANG004="Del_IP/MAC_Binding"	#ɾIP/MAC
	LANG005="Export_IP/MAC_Binding"	#IP/MAC
	LANG006="Import_IP/MAC_Binding"	#IP/MAC
fi


load_ipmac_stat()
{
    . ../common/ajax_object_lib

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


set_ipmac_stat()
{
    [ "${CGI_ipmac_enable}"  = "on" ] && enable=1 || enable=0
    [ "${CGI_ipmac_gateway}" = "" ] && CGI_ipmac_gateway="0.0.0.0"

    args="ipmac_enable=${enable} ipmac_deny_dynamic=${CGI_ipmac_deny_dynamic}"
    args="${args} ipmac_whitetbl=${CGI_ipmac_whitetbl}"
    args="${args} ipmac_gateway=${CGI_ipmac_gateway}"

    errmsg=`${FLOWEYE} ipmac set ${args}`
    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "ipmac set ${args}"
        WEB_LOGGER "${LANG003:=IP/MAC}" "${args}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


load_ipmac_list()
{
    #ipaddr intip mac xmac drop name
    ${FLOWEYE} ipmac list | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"ipstr\":\"%s\",", $(col++);
        printf "\"intip\":\"%s\",", $(col++);
        printf "\"mac\":\"%s\",", $(col++);
        printf "\"xmac\":\"%s\",", $(col++);
        printf "\"drop\":\"%s\",", $(col++);
        printf "\"desc\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


load_uipmac_list()
{
    end=$(( ${CGI_page} * ${CGI_limit} ))
    start=$(( ${end} - ${CGI_limit} ))

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

		gsub("%20", " ", $4);

		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"ipaddr\":\"%s\",", $1;
		printf "\"mac\":\"%s\",",$3;
		printf "\"desc\":\"%s\"", ($4 == "NULL" ? "" : $4);
		printf "}";

		if(dot == "") dot = ",";
	}END{
		printf "],\"total\":\"%s\"}", row;
	}'
}


set_ipmac_bind()
{
    if [ "${CGI_oldip}" != "" -a "${CGI_oldip}" != "${CGI_ip}" ]; then
        ${FLOWEYE} ipmac set ipmac="00:00:00:00:00:00-${CGI_oldip}"
        sync_floweye "ipmac set ipmac=00:00:00:00:00:00-${CGI_oldip}"
    fi

    errmsg=`${FLOWEYE} ipmac set ipmac=${CGI_mac}-${CGI_ip}`

    [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
    sync_floweye "ipmac set ipmac=${CGI_mac}-${CGI_ip}"

    if [ "${CGI_desc}" != "" ]; then
        [ "${PALANG}" = "en" ] && CGI_desc=`echo ${CGI_desc} | sed -r 's/ /\%20/g'`
        errmsg=`${FLOWEYE} macdesc set m=${CGI_mac} d=${CGI_desc}`
        [ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
        sync_floweye "macdesc set m=${CGI_mac} d=${CGI_desc}"
    fi

    WEB_LOGGER "${LANG003:=IP/MAC}" "ipmac=${CGI_mac}-${CGI_ip}"
	retjson 0 "${LANG002:=ɹ}"
}


rmv_ipmac_bind()
{
    LOG_MSG="${LANG004:=ɾIP/MAC}"
    [ "${CGI_logmsg}" != "" ] && LOG_MSG=${CGI_logmsg}

    errmsg=`${FLOWEYE} ipmac set ${CGI_ipmaclist}`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        sync_floweye "ipmac set ${CGI_ipmaclist}"
        WEB_LOGGER ${LOG_MSG}
        retjson 0 "${LANG002:=ɹ}"
    fi
}


export_ipmac_bind()
{
    mkdir -p ${WEB_DOWNLOAD}

    file_name="ipmac_bind.conf"
    file_path="${WEB_DOWNLOAD}/${file_name}"

    ${FLOWEYE} ipmac list | while read ipaddr intip mac theothers
    do
        if [ "${mac}" != "00-00-00-00-00-00" ]; then
            echo "${ipaddr} ${mac}" >> ${file_path}
        fi
    done

    WEB_LOGGER "${LANG005:=IP/MAC}"
    retjson 0 "OK" "\"${file_name}\""
}


import_ipmac_bind()
{
    errmsg=`${FLOWEYE} ipmac loadfile file=${CGI_file}`

    if [ $? -ne 0 ]; then
		retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        rm -rf ${CGI_file}
        WEB_LOGGER "${LANG006:=IP/MAC}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}


case "${CGI_action}" in
    "load_ipmac_stat")
        retjson 0 "OK" "`load_ipmac_stat`"
        ;;
    
    "set_ipmac_stat")
        action_check
        set_ipmac_stat
        ;;

    "load_ipmac_list")
        retjson 0 "OK" "`load_ipmac_list`"
        ;;

    "load_uipmac_list")
        retjson 0 "OK" "`load_uipmac_list`"
        ;;

    "set_ipmac_bind")
        action_check
        set_ipmac_bind
        ;;

    "rmv_ipmac_bind")
        action_check
        rmv_ipmac_bind
        ;;

    "export_ipmac_bind")
        action_check
        export_ipmac_bind
        ;;

    "import_ipmac_bind")
        action_check
        import_ipmac_bind
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;
esac
