#!/bin/sh
#
#

FLAG_ON=1
FLAG_OFF=0
Prm_Quic=${FLAG_ON}
if [ x$1 = x ];then
	PrmDebug=0
else
	PrmDebug=$1
fi
CMD_CHKMAC=/usr/local/bin/arg_chk_mac
CMD_SETMAC=/usr/local/bin/mb5120_set_mac_address
#CMD_SETMAC="echo 41,00:0E:FF:F0:00:31 40,01:0E:FF:F0:00:01"

PATH_MB5120_TEST=/usr/local/bin/
ARG_CHK_MAC=${PATH_MB5120_TEST}arg_chk_mac
PROC_USBDEVS=/proc/bus/usb/devices

MAC0TEST=00:0E:FF:F0:00:30
MAC1TEST=00:0E:FF:F0:00:31

# 00:0A:79: AE:50:00 ... CG-ARVPNSTD 
# 00:61:64: 6D:2D:00  Driver Default 0
# 00:61:64: 6D:2D:01  Driver Default 1
# HW_MAC_ADDRES0=`ifconfig eth0 | grep HWaddr |tr -s ' '| cut -d ' ' -f 5`
# HW_MAC_ADDRES1=`ifconfig eth1 | grep HWaddr |tr -s ' '| cut -d ' ' -f 5`


let linecnt=-1
for macline in `${CMD_SETMAC}`
do
	linecnt=`expr ${linecnt} + 1`
	test ${PrmDebug} -eq 1 && echo "linecnt=${linecnt}"
	if [ ${linecnt} -ge 2 ] ; then
		break
	fi
	test ${PrmDebug} -eq 1 && echo "macline=${macline}"
	MacNg=0
	crport=`echo ${macline} | cut -d',' -f1`
	test ${PrmDebug} -eq 1 && echo "crport=${crport}"
	crmaca=`echo  ${macline} | cut -d',' -f2`
	test ${PrmDebug} -eq 1 && echo "crmaca=${crmaca}"

	if [ x${crport} = x00 -o x${crport} = xff ] ; then
		echo "Port NG : ${crport}"
		MacNg=1
	fi

	${CMD_CHKMAC} ${crmaca} -C
	ChkMacRtc=$?
	if [ ${ChkMacRtc} -ne 0 ] ;then
	#	echo "Address NG ${linecnt}: ${crmaca} , ${ChkMacRtc}"
		MacNg=1
	fi

	if [ ${linecnt} -eq 0 ] ; then
		if [ ${crmaca} = ${MAC1TEST} ];then
			echo "Address NG ${linecnt} ,Test 1 MAC: ${crmaca} , ${ChkMacRtc}"
			MacNg=1
		fi
		CR_MACNG0=${MacNg}
		if [ x${MacNg} = x0 ] ; then
			CR_PORT0=$crport
			CR_MACA0=$crmaca
		else
			CR_PORT0=41
			CR_MACA0=${MAC0TEST}
		fi
	elif [ ${linecnt} -eq 1 ] ; then
		if [ ${crmaca} = ${MAC0TEST} ];then
			echo "Address NG ${linecnt} ,Test 0 MAC: ${crmaca} , ${ChkMacRtc}"
			MacNg=1
		fi
		CR_MACNG1=${MacNg}
		if [ x${MacNg} = x0 ] ; then
			CR_PORT1=$crport
			CR_MACA1=$crmaca
		else
			CR_PORT1=42
			CR_MACA1=${MAC1TEST}
		fi
	fi
done

test ${PrmDebug} -eq 1 && echo "CR_MACNG0=${CR_MACNG0},CR_MACNG1=${CR_MACNG1}"

if [ x${CR_MACNG0} != x0 -o x${CR_MACNG1} != x0 ];then
	if [ ${PrmDebug} -eq 1 ];then
		PRM_SETMAC=-v
	else
		PRM_SETMAC=-v
	fi
	## SETCMD_PRM="${CMD_SETMAC} -w ${PRM_SETMAC} ${CR_PORT0},${CR_MACA0} ${CR_PORT1},${CR_MACA1}"
	SETCMD_PRM="${CMD_SETMAC} -w ${PRM_SETMAC} 41,${MAC0TEST} 42,${MAC1TEST}"
	test ${PrmDebug} -eq 1 && echo "SETCMD_PRM=${SETCMD_PRM}"
	echo "Set TEST MAC ADDRESS"
	${SETCMD_PRM}
	echo "reboot start" 
	reboot
	sleep 10
	exit 1

#else
#	${CMD_SETMAC} -v

fi

exit 0
