#!/bin/sh
PATH_MB5120_TEST=/usr/local/bin/
PROC_USBDEVS=/proc/bus/usb/devices
ARG_CHK_MAC=${PATH_MB5120_TEST}arg_chk_mac
LEDD_CMDPIPE=/tmp/mb5120_ledd_pipe
DEBUG=0

let Ram_ErrEth0=0
let Ram_ErrEth1=0
let Ram_ErrRtc=0

#--------------------------------------
sigusr1_receive()
{
	logger "$progname:USR1 signal received."
	DEBUG=1
}
#--------------------------------------
sighup_receive()
{
	if [ x$DEBUG = x1 ] ; then
		echo hungup signal received
	fi
}
#--------------------------------------
signal_receive()
{
	if [ x$DEBUG = x1 ] ; then
		echo signal received
	fi
}

#--------------------------------------
trap sigusr1_receive USR1
trap sighup_receive HUP
trap signal_receive INT TERM

MSG_TOPT="/// MB5120 MANUFACTURE TEST START ///////"
MSG_ETCT="ETHER MAC CHECK ============"
MSG_FROT="FLASH ROM TEST ============="
MSG_RTCT="RTC TEST ==================="
MSG_ETHT="ETHER TEST ================="
MSG_LEDT="LED TEST ==================="
MSG_DIPT="DipSwitch TEST ============="
MSG_MACT="MAC address write =========="
MSG_ENDT="/// MB5120 TEST END /////////"
MSG_ETHM="ETHER MAC ADDRES   ... "
MSG_FROM="FLASH ROM          ... "
MSG_RTC0="RTC                ... "
MSG_ETH0="ETHER 0            ... "
MSG_ETH1="ETHER 1            ... "
MSG_LED0="LED                ... "
MSG_DIP0="DipSw              ... "
MSG_MACW="MAC address write  ... "

echo Bg >${LEDD_CMDPIPE}
main_err=0
echo
echo  "${MSG_TOPT}"
${PATH_MB5120_TEST}testdevmake.sh

#echo
#echo  "${MSG_ETCT}"
if ! ${PATH_MB5120_TEST}mb5120_ether_mac_chk.sh ; then
	echo "REBOOT"
	/sbin/reboot
	sleep 100
#else
#	echo "${MSG_ETHM}OK"
fi

echo
StartTime=`date +%s`
SubTime=0
while [ ${SubTime} -le 300 ] ; do 
	echo    "${MSG_ETHT}"
	echo -n "${MSG_ETH0}"
	if ! ${PATH_MB5120_TEST}mb5120_ether_test.sh eth0 ; then 
		main_err=`expr $main_err + 1`
#		echo C >${LEDD_CMDPIPE}
		Ram_ErrEth0=1
		break
	fi
	echo -n "${MSG_ETH1}"
	if ! ${PATH_MB5120_TEST}mb5120_ether_test.sh eth1 ; then 
		main_err=`expr $main_err + 1`
#		echo C >${LEDD_CMDPIPE}
		Ram_ErrEth1=1
		break
	fi
	NowTime=`date +%s`
	let "SubTime = ${NowTime} - ${StartTime}"
	echo "Time= ${SubTime} Sec"
	
done


test $DEBUG -eq 1 && echo main_err=$main_err

mb5120_NvRamCountUty.sh CountUp ${Ram_ErrEth0} ${Ram_ErrEth1} ${Ram_ErrRtc}

test $DEBUG -eq 1 && echo main_err=$main_err
echo
echo "${MSG_ENDT}"
if [ x${main_err} = x0  ]; then
	echo "Good complete. $main_err"
	echo "OK"
#	echo Bd >${LEDD_CMDPIPE}
else
	echo "found some errors. $main_err"
	echo "NG"

	if [ ${Ram_ErrEth0} -ne 0 ];then
		echo Gc >${LEDD_CMDPIPE}
	else
		echo Hc >${LEDD_CMDPIPE}
	fi
	while true ; do
		read x
		if [ "X$x" = "X!" ]; then
			/bin/sh -l
		fi
	done
fi

/sbin/reboot
sleep 100
exit $main_err
