#!/bin/sh

. ../common/ajax_common

ifNote="${PGETC}/remark/ifnote.conf"

if [ "${PALANG}" = "en" ]; then
	LANG001="Error"
	LANG002="Success"
	LANG003="Set Interface Configure"
	LANG004="Set Interface Queue"
fi


load_if_list()
{
	# name mode zone state driver mac type inbps outbps inpps outpps bgname speed peer ifgrp misspkt theothers

	printf "{"
	printf "\"list\":"

	${FLOWEYE} if list showsum=1 | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\",", $(col++);
		printf "\"mode\":\"%s\",", $(col++);
		printf "\"zone\":\"%s\",", $(col++);
		printf "\"state\":\"%s\",", $(col++);
		printf "\"driver\":\"%s\",", $(col++);
		printf "\"mac\":\"%s\",", $(col++);
		printf "\"type\":\"%s\",", $(col++);
		printf "\"inbps\":\"%s\",", $(col++);
		printf "\"outbps\":\"%s\",", $(col++);
		printf "\"inpps\":\"%s\",", $(col++);
		printf "\"outpps\":\"%s\",", $(col++);
		printf "\"bgname\":\"%s\",", $(col++);
		printf "\"speed\":\"%s\",", $(col++);
		printf "\"peer\":\"%s\",", $(col++);
		printf "\"ifgrp\":\"%s\",", $(col++);
		printf "\"misspkt\":\"%s\"", $(col++);
		printf "}";
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'

	printf ",\"note\":"
	
	if [ ! -f ${ifNote} ]; then
		printf "{}"
	else
		awk -F "=" \
		'BEGIN{
			dot = "";
			printf "{";
		}{
			printf "%s", dot;
			printf "\"%s\":\"%s\"", $1, $2;
			if ( dot == "") dot = ",";
		}END{
			printf "}";
		}' ${ifNote}
	fi

	printf "}"
}


load_if_core()
{
	# flow cpu wait burst pps pktrejs theother
	${FLOWEYE} system list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(NR == 1) next;
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"flow\":%s,", $(col++);
		printf "\"cpu\":%s,", $(col++);
		printf "\"wait\":%s,", $(col++);
		printf "\"burst\":%s,", $(col++);
		printf "\"pps\":%s,", $(col++);
		printf "\"pktrejs\":%s", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


load_if_queue()
{
	# name queue autoflow setflow pps theother
	${FLOWEYE} ifmap list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		if(NR == 1) next;
		col = 1;
		printf "%s", dot;
		printf "{";
		printf "\"name\":\"%s\",", $(col++);
		printf "\"queue\":%s,", $(col++);
		printf "\"autoflow\":%s,", $(col++);
		printf "\"setflow\":%s,", $(col++);
		printf "\"pps\":%s", $(col++);
		printf "}";
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


load_ifsch_env()
{
	printf "{"
	printf "\"core\":`load_if_core`,"
	printf "\"queue\":`load_if_queue`"
	printf "}"
}


get_bg_name()
{
	${FLOWEYE} policy listlink | sort -nk2 | awk \
	'BEGIN{
		dot = "";
		printf "{";
	}{
		if($2 > 4) exit;

		printf "%s", dot;
		printf "\"bg_%s\":\"%s\"", $2, $1;
		if ( dot == "" ) dot = ",";
	}END{
		printf "}";
	}'
}


get_if_status()
{
	note=`grep "${CGI_if}=" ${ifNote} | cut -d"=" -f2`

	printf "{"
	printf "\"name\":\"${CGI_if}\""
	printf ",\"note\":\"${note}\""

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


dump_if_status()
{
	${FLOWEYE} if stat ${CGI_if} | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		gsub("\\t", "    ", $0);
		printf "%s\"%s\"", dot, $0;
		if ( dot == "" ) dot = ",";
	}END{
		printf "]";
	}'
}


if_num_list()
{
	${FLOWEYE} if list | awk \
	'BEGIN{
		dot = "";
		printf "[";
	}{
		printf "%s", dot;
		printf "\"%s\"", $1;
		if (dot == "") dot = ",";
	}END{
		printf "]";
	}'
}


load_edit_env()
{
	printf "{"
	printf "\"info\":`get_if_status`,"
	printf "\"bgname\":`get_bg_name`,"
	printf "\"iflist\":`if_num_list`"
	printf "}"
}


set_if_access()
{
	# ע
	mkdir -p "${PGETC}/remark"
	grep -v "${CGI_ifname}=" ${ifNote} > ${ifNote}.bak
	if [ "${CGI_note}" != "" ]; then
		echo "${CGI_ifname}=${CGI_note}" >> ${ifNote}.bak
	fi
	mv ${ifNote}.bak ${ifNote}

	# 
	if [ "${CGI_mode}" != "0" ]; then
		errmsg=`${FLOWEYE} policy setlink link=${CGI_mode} name="${CGI_bgname}"`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "policy setlink link=${CGI_mode} name=${CGI_bgname}"
	fi

	#  
	[ "${CGI_lagroup}" = "" ] && CGI_lagroup="0"
	cmdargs="name=${CGI_ifname} mode=${CGI_mode} zone=${CGI_zone} lagroup=${CGI_lagroup}"
	[ "${CGI_mode}" != "0" ] && cmdargs="${cmdargs} peer=${CGI_peer}"
	errmsg=`${FLOWEYE} if set ${cmdargs}`
	[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
	sync_floweye "if set ${cmdargs}"

	# 
	if [ "${CGI_media}" != "" ]; then
		errmsg=`${FLOWEYE} em setspeed ${CGI_ifname} ${CGI_media}`
		[ $? -ne 0 ] && retjson 1 "${LANG001:=ʧ}:${errmsg}"
		sync_floweye "em setspeed ${CGI_ifname} ${CGI_media}"
	fi
	
	WEB_LOGGER "${LANG003:=޸}" "name=${CGI_ifname}"
	retjson 0 "${LANG002:=óɹ}"
}


load_if_rate()
{
	printf "["
	
	${FLOWEYE} dbifpxy get name=${CGI_if} type=in
	
	printf ","

	${FLOWEYE} dbifpxy get name=${CGI_if} type=out
	
	printf "]"
}


set_if_queue()
{
	for line in `echo ${CGI_data} | tr ";" " "`
	do
		flow=`echo ${line} | cut -d"_" -f1`
		queue=`echo ${line} | cut -d"_" -f2`
		ifname=`echo ${line} | cut -d"_" -f3`
		errmsg=`${FLOWEYE} ifmap set name=${ifname} queue=${queue} flow=${flow}`
		if [ $? -ne 0 ]; then
			[ "${errmsg}" = "NEXIST" ] && continue
			retjson 1 "${ifname}:${errmsg}"
		fi
	done

	WEB_LOGGER "${LANG004:=}"
	retjson 0 "${LANG002:=óɹ}"
}


case "${CGI_action}" in

	"load_if_rate")
		retjson 0 "OK" "`load_if_rate`"
		;;

	"load_if_list")
		retjson 0 "OK" "`load_if_list`"
		;;
		
	"load_if_core")
		retjson 0 "OK" "`load_if_core`"
		;;
	
	"load_if_queue")
		retjson 0 "OK" "`load_if_queue`"
		;;

	"load_ifsch_env")
		retjson 0 "OK" "`load_ifsch_env`"
		;;

	"get_if_status")
		retjson 0 "OK" "`get_if_status`"
		;;

	"dump_if_status")
		retjson 0 "OK" "`dump_if_status`"
		;;

	"load_edit_env")
		retjson 0 "OK" "`load_edit_env`"
		;;

	"set_if_access")
		action_check
		set_if_access
		;;
		
	"set_if_queue")
		action_check
		set_if_queue
		;;

	*)
		retjson 1 "UNKNOW_ACTION!"
		;;
esac
