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

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

# 00:0A:79: AE:50:00 ... CG-ARVPNSTD 
# 00:61:64: 6D:2D:00  Driver Default 1
# 00:61:64: 6D:2D:01  Driver Default 2

Mac0Chk=${FLAG_ON}
Mac1Chk=${FLAG_ON}
HW_MAC_ADDRES0=${MACANULL}
HW_MAC_ADDRES1=${MACANULL}

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

	${CMD_CHKMAC} ${crmaca} -cCs
	ChkMacRtc=$?
	if [ ${ChkMacRtc} -ne 0 ] ;then
		test ${PrmDebug} -eq 1 && echo "Address NG ${linecnt}: ${crmaca} , ${ChkMacRtc}"
		MacNg=1
	fi

	if [ ${ChkMacRtc} -eq 3 -o ${ChkMacRtc} -eq 4 ];then
		DUMMY=0;
	else
		if [ ${linecnt} -eq 0 ] ; then
			HW_MAC_ADDRES0=$crmaca
		elif [ ${linecnt} -eq 1 ] ; then
			CR_MACNG1=${MacNg}
			HW_MAC_ADDRES1=$crmaca
		fi
	fi
done


if [ ${PrmDebug} -eq 1 ];then
	echo "MAC0=${HW_MAC_ADDRES0}"
	echo "MAC1=${HW_MAC_ADDRES1}"
fi

SET_MAC_ADDRES0=${HW_MAC_ADDRES0}
SET_MAC_ADDRES1=${HW_MAC_ADDRES1}

MacChk=${FLAG_ON}
while [ ${MacChk} -eq ${FLAG_ON} ] ; do

#	if [ ${Prm_Quic} -eq ${FLAG_ON} ];then
#		Mac0Chk=${FLAG_ON}
#		Mac1Chk=${FLAG_ON}
#	fi
#	if [ ${Mac0Chk} -eq ${FLAG_OFF} ];then
#		echo -n "MAC address 0 : ${SET_MAC_ADDRES0} OK ? (y/*n) > "
#		read YesNo
#		if [ X${YesNo} != Xy ] ; then 
#			Mac0Chk=${FLAG_ON}
#		fi
#	fi

	while [ ${Mac0Chk} -ne ${FLAG_OFF} ] ; do
		echo -n "Input MAC address 0 [${SET_MAC_ADDRES0}]> "
		read MAC_ADDRES
		if [ x${MAC_ADDRES} = x ];then
			MAC_ADDRES=${SET_MAC_ADDRES0}
		fi
		MAC_ADDRES=`${CMD_CHKMAC} -uocC ${MAC_ADDRES}`
		Mac0Chk=$?
		if [ ${Mac0Chk} -eq ${FLAG_OFF} ]; then
			SET_MAC_ADDRES0=${MAC_ADDRES}
		else
			echo ${MAC_ADDRES}
		fi
	done

#	if [ ${Mac1Chk} -eq ${FLAG_OFF} ];then
#		echo -n "MAC address 1 : ${SET_MAC_ADDRES1} OK ? (y/*n) > "
#		read YesNo
#		if [ X${YesNo} != Xy ] ; then 
#			Mac1Chk=${FLAG_ON}
#		fi
#	fi

	while [ ${Mac1Chk} -ne ${FLAG_OFF} ] ; do
		echo -n "Input MAC address 1 [${SET_MAC_ADDRES1}]> "
		read MAC_ADDRES
		if [ x${MAC_ADDRES} = x ];then
			MAC_ADDRES=${SET_MAC_ADDRES1}
		fi
		MAC_ADDRES=`${CMD_CHKMAC} -uocC ${MAC_ADDRES}`
		Mac1Chk=$?
		if [ ${Mac1Chk} -eq ${FLAG_OFF} ]; then
			SET_MAC_ADDRES1=${MAC_ADDRES}
		fi
	done
	echo "MAC0=${SET_MAC_ADDRES0}"
	echo "MAC1=${SET_MAC_ADDRES1}"
	if [ "X${SET_MAC_ADDRES0}" = "X${SET_MAC_ADDRES1}" ] ;then
		echo "ERROR : Address 0 and 1 equal"
		Mac0Chk=${FLAG_ON}
		Mac1Chk=${FLAG_ON}
	fi
	if [ ${Mac0Chk} -eq ${FLAG_OFF} -a ${Mac1Chk} -eq ${FLAG_OFF} ] ; then
		echo -n "MAC address 0 and 1  OK ? (y/*n/c) > "
		read YesNo
		if [ X${YesNo} = Xc ] ; then 
			MacChk=${FLAG_OFF}
			WRITE_FLG=0
		elif  [ X${YesNo} != Xy ] ; then 
			MacChk=${FLAG_ON}
			Mac0Chk=${FLAG_ON}
			Mac1Chk=${FLAG_ON}
		else
			MacChk=${FLAG_OFF}
			WRITE_FLG=1
		fi
	fi

done

Retn=0
if [ x${WRITE_FLG} != x0 ];then
	if [ x${SET_MAC_ADDRES0} != x${HW_MAC_ADDRES0} -o ${SET_MAC_ADDRES1} != ${HW_MAC_ADDRES1} -o ${MacNg} -ne 0 ]; then
		MACSETCMD="${CMD_SETMAC} -wv 0x41,${SET_MAC_ADDRES0}  0x42,${SET_MAC_ADDRES1}"
		${MACSETCMD}
		Retn=$?
		if [ ${Retn} -eq 0 ] ;then
			echo "Write MAC Address OK"
		else
			echo "Write MAC Address ERROR"
		fi
	else
		echo "MAC Address OK"
	fi
else
		echo "Not write MAC Address"
fi
exit ${Retn}
