#!/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
}


load_gswifi_list()
{
    ${FLOWEYE} gswifi list | grep -i "${CGI_keyword}" | awk \
    'BEGIN{
        dot = "";
        printf "[";
    }{
        col = 1;
        printf "%s{", dot;
        printf "\"ip\":\"%s\"", $(col++);
        printf ",\"mac\":\"%s\"", $(col++);
        printf ",\"time\":\"%s\"", $(col++);
        printf ",\"phone\":\"%s\"", $(col++);
        printf ",\"pc\":\"%s\"", $(col++);
        printf ",\"trust\":%s", $(col++);
        printf ",\"account\":\"%s\"", $(col++);
        printf "}";
        if(dot == "") dot = ",";
    }END{
        printf "]";
    }'
}


export_gswifi_csv()
{
    mkdir -p ${WEB_DOWNLOAD}

    file_name="豸_`date "+%Y%m%d_%H%M%S"`.csv"
    download_path="${WEB_DOWNLOAD}/${file_name}"

    echo ",豸IP,豸MAC,ʱ,ƶն,PCն" > ${download_path}

    ${FLOWEYE} gswifi list | awk \
    '{
        time = strftime("%Y-%m-%d %H:%M:%S", $3);
        printf "%s,%s,%s,%s,%s,%s\n", NR, $1, $2, time, $4, $5;
    }' >> ${download_path}

    WEB_LOGGER "豸б"
    retjson 0 "OK" "\"${file_name}\""
}


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


set_gswifi_stat()
{
    cmdargs="enable=${CGI_enable} model=${CGI_model} devlimit=${CGI_devlimit} devttl=${CGI_devttl}"

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

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

load_trust_list()
{
    total=0 dot=""
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	result=`${FLOWEYE} gstrustip list`

	if [ "${result}" = "" ]; then
		echo "{\"total\":0,\"data\":[]}"
		return 0
	fi
    
	printf "{\"data\":["
	
	while read object time description
	do
		total=$((${total}+1))
		[ ${total} -le ${start} -o ${total} -gt ${end} ] && continue

		description=`utf8togb2312 ${description}`

        printf "${dot}"
        printf "{"
        printf "\"object\":\"${object}\","
        printf "\"time\":\"${time}\","
        printf "\"description\":\"${description}\""
        printf "}"
		
		[ "${dot}" = "" ] && dot=","
		
	done << EOF
	${result}
EOF

	printf "],\"total\":%d}" ${total};

}

add_trust_ip(){

    cmdargs="ip=${CGI_wipchstr} desc=${CGI_desc}"

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

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

remove_trust_ip(){

    ip_str="${CGI_wipchstr}"
    while IFS='|' read -ra array; do
      for item in "${array[@]}"; do

    cmdargs="ip=$item"

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

      done
   done <<< "$ip_str"

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

}

trend_discovery_chart(){
	tag=""
    tag2=""
    MAX_SHARE_LEVEL="$(${FLOWEYE} gswifi stat | grep MAX_SHARE_LEVEL | cut -d '=' -f 2)"
	
	printf "{\"name\":{"

    for ((i=0; i<$MAX_SHARE_LEVEL; i++)); do
        tag="gswifi_cnt_$((i+1))"
        tag2+="tag=${tag} "

		printf "${dot}\"$tag\":{"
		printf "\"name\":\"$tag\","
		printf "\"color\":\" \""
		printf "}"
		[ "${dot}" = "" ] && dot=","
	done
	printf "},\"data\":"
    ${FLOWEYE} chart dump $tag2 json=1
	printf "}"
}

load_trend_discovery_list()
{
    total=0 dot=""
	[ "${CGI_page}" = "" ] && CGI_page=1
	[ "${CGI_limit}" = "" ] && CGI_limit=100
	end=$((${CGI_page} * ${CGI_limit}))
	start=$((${end} - ${CGI_limit}))

	result=`${FLOWEYE} gswifi list hlist=1 time="${CGI_time}" | grep -i "${CGI_keyword}"`

	if [ "${result}" = "" ]; then
		echo "{\"total\":0,\"data\":[]}"
		return 0
	fi
    
	printf "{\"data\":["
	
	while read ip mac time phone pc devinfo account
	do
		total=$((${total}+1))
		[ ${total} -le ${start} -o ${total} -gt ${end} ] && continue

        account=`utf8togb2312 ${account}`
        devinfo=`utf8togb2312 ${devinfo}`

        printf "${dot}"
        printf "{"
        printf "\"ip\":\"${ip}\","
        printf "\"mac\":\"${mac}\","
        printf "\"time\":\"${time}\","
        printf "\"phone\":\"${phone}\","
        printf "\"pc\":\"${pc}\","
        printf "\"devinfo\":\"${devinfo}\","
        printf "\"account\":\"${account}\""
        printf "}"
		
		[ "${dot}" = "" ] && dot=","
		
	done << EOF
	${result}
EOF

	printf "],\"total\":%d}" ${total};

}

export_trend_discovery_csv()
{
    
    mkdir -p ${WEB_DOWNLOAD}

    file_name="ʷ豸_`date "+%Y%m%d_%H%M%S"`.csv"
    download_path="${WEB_DOWNLOAD}/${file_name}"

    echo ",豸IP,豸MAC,ʱ,ƶն,PCն,,˺" > ${download_path}
    
    result=`${FLOWEYE} gswifi list hlist=1`
    counter=1

    while read ip mac time phone pc devinfo account
	do
        account=`utf8togb2312 ${account}`
        time_=$(date -d "@${time}" "+%Y-%m-%d %H:%M:%S")
        printf "%s,%s,%s,%s,%s,%s,%s\n" "${counter}","${ip}","${mac}","$time_","${phone}","${pc}","${devinfo}","${account}" >> ${download_path}
		counter=$((counter + 1))
	done << EOF
	${result}
EOF

    WEB_LOGGER "ʷ豸б"
    retjson 0 "OK" "\"${file_name}\""
}

set_gswifi_terminal_rec(){

    cmdargs="clireco=${CGI_clireco}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        WEB_LOGGER "/رնʶ" "${cmdargs}"
        retjson 0 "${LANG002:=ɹ}"
    fi
}

set_gswifi_traffic_drop(){

    cmdargs="drop=${CGI_drop}"

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

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

control_gswifi(){

    cmdargs="ip=${CGI_ip} trust=${CGI_trust}"

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

    if [ $? -ne 0 ]; then
        retjson 1 "${LANG001:=ʧ}:${errmsg}"
    else
        WEB_LOGGER "ⶳ򶳽IP" "${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
        ;;

    "load_gswifi_list")
        retjson 0 "OK" "`load_gswifi_list`"
        ;;

    "export_gswifi_csv")
        action_check
        export_gswifi_csv
        ;;

    "load_gswifi_stat")
        retjson 0 "OK" "`load_gswifi_stat`"
        ;;

    "set_gswifi_stat")
        action_check
        set_gswifi_stat
        set_gswifi_terminal_rec
        set_gswifi_traffic_drop
        ;;

    "load_trust_list")
        retjson 0 "OK" "`load_trust_list`"
        ;;

    "add_trust_ip")
        action_check
        add_trust_ip
        ;;

    "remove_trust_ip")
        action_check
        remove_trust_ip
        ;;

    "trend_discovery_chart")
        retjson 0 "OK" "`trend_discovery_chart`"
        ;;

    "load_trend_discovery_list")
        retjson 0 "OK" "`load_trend_discovery_list`"
        ;;

    "export_trend_discovery_csv")
        action_check
        export_trend_discovery_csv
        ;;

    "set_gswifi_terminal_rec")
        action_check
        set_gswifi_terminal_rec
        ;;

    "set_gswifi_traffic_drop")
        action_check
        set_gswifi_traffic_drop
        ;;

    "control_gswifi")
        action_check
        control_gswifi
        ;;

    *)
        retjson 1 "UNKNOW_ACTION"
        ;;

esac
