#!/bin/sh


statistic_usage()
{
	echo ""
	echo "Usage: switch -t statistic -o <get |flush> [-p portsid]"
	echo ""
	echo "Example:"
	echo "       switch -t statistic -o get"
	echo "       switch -t statistic -o get -p \"1 2\" "
	echo "       switch -t statistic -o flush -p \"1 2 4\" "
	echo ""
}

statistic_arg_check()
{
	local portsid=$1
	local lan_cpu_port="$(uci get network.switch.lan_cpu_port)"

	for i in $portsid; do
		[ "$i" == "$((${lan_cpu_port}+1))" ] && continue
		if [ `expr match $i "[0-9]"` = 0 ] || [ $i -lt 1 ] || [ $i -gt $MAX_PORT_NUM ] ;then
		echo "[statistic] the port is invalid. "
		statistic_usage
		exit 0
		fi
	done
}

# flush switch port statistic
# $1: portsid (1.2.3.4.5...)
flush_port_statistic()
{
	if [ -z "$1" ]; then
		# if input portsid = null, flush all mibs
		#swconfig_reset_all_mibs
		for i in `seq 1 $MAX_PORT_NUM`;do
			local phy_id=`get_phy_port_id $i`
			[ -n "$phy_id" ] || return

			swconfig dev switch0 port $phy_id set reset_mib
		done
	else
		statistic_arg_check "$1"

		for i in $1; do
			local phy_id=`get_phy_port_id $i`
			[ -n "$phy_id" ] || continue

			swconfig_reset_port_mib $phy_id
		done
	fi

	
}

# convert statistic to standard format
# $1: port
get_port_standard_statistic()
{
	hwtype=`swconfig_switch_type_read`
	if [ "$hwtype" == "MT7530" ]; then
		swconfig_get_port_mib $(($1-1)) | awk 'BEGIN{t=0;o=0;p=0;x=0;y=0}
		/Rx64Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/Rx65Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/Rx128Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/Rx256Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/Rx512Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/Rx1024Byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/TxByte/	{o+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/RxByte/	{x+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/RxUni/		{print(gensub(/.*: ([0-9]+).*/, "RxUnicast : \\1", "1"));}
		/RxBroad/	{print(gensub(/.*: ([0-9]+).*/, "RxBroad : \\1", "1"));}
		/RxPause/	{print(gensub(/.*: ([0-9]+).*/, "RxPause : \\1", "1"));}
		/RxMulti/	{print(gensub(/.*: ([0-9]+).*/, "RxMulti : \\1", "1"));}
		/RxUnderSize/	{print(gensub(/.*: ([0-9]+).*/, "RxLitte : \\1", "1"));}
		/RxOverSize/	{print(gensub(/.*: ([0-9]+).*/, "RxTooLong : \\1", "1"));}
		/TxUni/		{print(gensub(/.*: ([0-9]+).*/, "TxUnicast : \\1", "1"));}
		/TxBroad/	{print(gensub(/.*: ([0-9]+).*/, "TxBroad : \\1", "1"));}
		/TxPause/	{print(gensub(/.*: ([0-9]+).*/, "TxPause : \\1", "1"));}
		/TxMulti/	{print(gensub(/.*: ([0-9]+).*/, "TxMulti : \\1", "1"));}
		END{print("TxAll : "o);print("RxAll : "x);print("RxNormal : "t); }'
	else
		swconfig_get_port_mib $(($1-1)) | awk 'BEGIN{t=0;o=0;p=0;x=0;y=0}
		/rxPacketLenIn64byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxPacketLenIn65byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxPacketLenIn128byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxPacketLenIn256byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxPacketLenIn512byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxPacketLenIn1024byte/	{t+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/txOctetCounterLowDoubleWord/	{o+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/txOctetCounterHighDoubleWord/	{p+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxOctetCounterLowDoubleWord/	{x+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxOctetCounterHighDoubleWord/	{y+=gensub(/.*: ([0-9]+).*/, "\\1", "1");}
		/rxUnicastPacket/		{print(gensub(/.*: ([0-9]+).*/, "RxUnicast : \\1", "1"));}
		/rxBroadcastPacket/		{print(gensub(/.*: ([0-9]+).*/, "RxBroad : \\1", "1"));}
		/rxPausePacket/			{print(gensub(/.*: ([0-9]+).*/, "RxPause : \\1", "1"));}
		/rxMulticastPacket/		{print(gensub(/.*: ([0-9]+).*/, "RxMulti : \\1", "1"));}
		/rxUndersizePacket/		{print(gensub(/.*: ([0-9]+).*/, "RxLitte : \\1", "1"));}
		/rxOversizePacket/		{print(gensub(/.*: ([0-9]+).*/, "RxTooLong : \\1", "1"));}
		/txUnicastPacket/		{print(gensub(/.*: ([0-9]+).*/, "TxUnicast : \\1", "1"));}
		/txBroadcastPacket/		{print(gensub(/.*: ([0-9]+).*/, "TxBroad : \\1", "1"));}
		/txPausePacket/			{print(gensub(/.*: ([0-9]+).*/, "TxPause : \\1", "1"));}
		/txMulticastPacket/		{print(gensub(/.*: ([0-9]+).*/, "TxMulti : \\1", "1"));}
		END{o+=lshift(1,31)*2*p;x+=lshift(1,31)*2*y;print("TxAll : "o);print("RxAll : "x);print("RxNormal : "t); }'
	fi
}

# get switch port statistic
# $1: portsid
get_port_statistic()
{
	local ports=$1

	# if input portsid = null, get all mibs
	if [ -z "$ports" ]; then
		ports="1 2 3 4 5"
	else
		statistic_arg_check "$ports"
	fi	

	for i in $ports; do
		get_port_standard_statistic "$i"
	done
	
}

# mt7621 switch port statistic
# $1: op (get|flush)
# $2: portsid (1.2.3.4.5...)
mt7621_switch_port_statistic()
{
	local op=$1
	local portsid=$2

	if [ $op = "get" ]; then	
		get_port_statistic "$portsid"
	elif [ $op = "flush" ]; then
		flush_port_statistic "$portsid"
	else
		echo "[statistic] error op. (get | flush)"
	fi
}
