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


#--------------------------------------
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 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_USB0="USB (onboard)      ... "
MSG_USB1="USB (external UJ1) ... "
MSG_USB2="USB (external UJ2) ... "
MSG_RTC0="RTC                ... "
MSG_ETH0="ETHER 0            ... "
MSG_ETH1="ETHER 1            ... "
MSG_LED0="LED                ... "
MSG_DIP0="DipSw              ... "
MSG_MACW="MAC address write  ... "

main_err=0

${PATH_MB5120_TEST}testdevmake.sh
echo
mb5120_NvRamCountUty.sh Display

echo
echo  "${MSG_TOPT}"
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 -n "${MSG_USB0}"
#${PATH_MB5120_TEST}usb_test.sh 5 || err=`expr $err + 1`
#echo -n "${MSG_USB1}"
#${PATH_MB5120_TEST}usb_test.sh 1 || err=`expr $err + 1`
#echo -n "${MSG_USB2}"
#${PATH_MB5120_TEST}usb_test.sh 3 || err=`expr $err + 1`

echo
echo    "${MSG_RTCT}"
echo -n "${MSG_RTC0}"
if ! ${PATH_MB5120_TEST}mb5120_hwclock_test.sh ; then
	main_err=`expr $main_err + 1`
	echo C >${LEDD_CMDPIPE}
fi

echo
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}
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}
fi

echo
echo  "${MSG_FROT}"
${PATH_MB5120_TEST}mb5120_flash_chk.sh
stat=$?
echo -n "${MSG_FROM}"
if [ x${stat} = x0 ] ; then
	echo "OK"
else
	echo "ERROR"
	main_err=`expr $main_err + 1`
	echo C >${LEDD_CMDPIPE}
fi

# LEDd STOP
echo Aa >${LEDD_CMDPIPE}
echo
echo  "${MSG_LEDT}"
${PATH_MB5120_TEST}mb5120_led_test
stat=$?
echo -n "${MSG_LED0}"
if [ x${stat} = x0 ] ; then
	echo "OK"
else
	echo "ERROR"
	main_err=`expr $main_err + 1`
fi
if [ x${main_err} = x0  ]; then
	echo Dd >${LEDD_CMDPIPE}
else
	echo Cd >${LEDD_CMDPIPE}
fi

test $DEBUG -eq 1 && echo main_err=$main_err
echo
echo "${MSG_DIPT}"
${PATH_MB5120_TEST}mb5120_dipswitch_test
stat=$?
echo -n "${MSG_DIP0}"
if [ x${stat} = x0 ]; then
	echo "OK"
else
	echo "ERROR"
	main_err=`expr $main_err + 1`
	echo C >${LEDD_CMDPIPE}
fi

mb5120_NvRamCountUty.sh ErrorChk
MTESTOK=$?
if [ x${MTESTOK} != x0 ];then
	main_err=`expr $main_err + 1`
fi
test $DEBUG -eq 1 && echo "main_err=${main_err} MTESTOK=${MTESTOK}"

${PATH_MB5120_TEST}mb5120_mac_okchk.sh
MACADROK=$?
test $DEBUG -eq 1 && echo "main_err=${main_err} MACADROK=${MACADROK}"

#if [ x${MACADROK} = x0 ]; then
#	echo "MAC Address OK"
#fi

if [ x$main_err = x0 -a x${MACADROK} != x0 ]; then
	echo -n "${MSG_MACT}"
	echo
	${PATH_MB5120_TEST}mb5120_set_mac.sh
	stat=$?
	
	echo -n "${MSG_MACW}"
	if [ x${stat} = x0 ]; then
		echo "OK"
	else
		echo "ERROR"
		main_err=`expr $main_err + 1`
		echo C >${LEDD_CMDPIPE}
	fi
fi
test $DEBUG -eq 1 && echo main_err=$main_err
echo
echo "${MSG_ENDT}"
if [ x${main_err} = x0  ]; then
	mb5120_NvRamCountUty.sh ChkErase
	echo "Good complete. $main_err"
	echo "OK"
	echo Bd >${LEDD_CMDPIPE}
else
	echo "found some errors. $main_err"
	echo "NG"
	echo Cd >${LEDD_CMDPIPE}
fi

test $DEBUG -eq 1 && echo main_err=$main_err
while true ; do
	read x
	if [ "X$x" = "X!" ]; then
		/bin/sh -l
	fi
done
exit $main_err
