#!/bin/sh
#
#  ret 0 = OK
#      1 = NG

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"

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} -cCs
	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
		if [ x${crport} != x41 ]; then
			MacNg=1
		fi
		CR_MACNG0=${MacNg}
		CR_PORT0=${crport}
		CR_MACA0=${crmaca}
	elif [ ${linecnt} -eq 1 ] ; then
		if [ ${crmaca} = ${MAC0TEST} ];then
			echo "Address NG ${linecnt} ,Test 0 MAC: ${crmaca} , ${ChkMacRtc}"
			MacNg=1
		fi
		if [ x${crport} != x42 ]; then
			MacNg=1
		fi
		CR_MACNG1=${MacNg}
		CR_PORT1=${crport}
		CR_MACA1=${crmaca}
	fi
done

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

if [ x${CR_MACA0} = x${CR_MACA1} ]; then
	CR_MACNG0=1
fi
if [ x${CR_MACNG0} != x0 -o x${CR_MACNG1} != x0 ];then
	RETCODE=1
else
	RETCODE=0
fi

test ${PrmDebug} -eq 1 && echo RETCODE=${RETCODE}

exit ${RETCODE}
