#!/bin/sh /etc/rc.common

#this script will run by /etc/init.d/rcS after all rc script.
START=50
#STOP=99

#
#reboot if REBOOTFLAGFILE set to REBOOT=1, by another script
REBOOTFLAGFILE='/tmp/needreboot.stat'

#/etc/init.d/rcS will save all /etc/rc.d/S* exitcode into this file
RCSTATFILE='/tmp/rc.timing'

SKIPLIST='boot_check'

# messageagent was removed temporary
KEYSRVLIST='boot dnsmasq network sysapihttpd'

elog(){
	tmsg="$@"
	logger -p local0.warning -t boot_check "boot_check[${$}]: $tmsg"
	echo "`date` boot_check[${$}]: $tmsg" >> /dev/console
}

failed_flag() {
	local system_current=`nvram get flag_boot_rootfs`

	if [ $system_current -ge 0 -a $system_current -le 1 ]; then
		nvram set flag_try_sys$((system_current+1))_failed=1
		nvram set flag_ota_reboot=0
		nvram commit
	fi
}

start() {
	#
	#check /etc/rc.d/S* exitcode
	#
	errcnt=0
	touch /tmp/$$.bootcheck.log
	cat $RCSTATFILE 2>/dev/null | grep 'EXITCODE=' | grep -v 'EXITCODE=0;' > /tmp/$$.bootcheck.log
	while read oneline
	do
		#EXITCODE=0;ESP=0;LIMIT=10;CMD=/etc/rc.d/S99datacenter;ARG=boot;
		match=0
		for onesrv in $SKIPLIST
		do
			echo "$oneline" | grep -q "[0-9]\{1,100\}$onesrv;" && match=1&&break
		done
		test $match -ne 0 && continue

		match=0
		for onesrv in $KEYSRVLIST
		do
			echo "$oneline" | grep -q "[0-9]\{1,100\}$onesrv;" && match=1&&break
		done
		if [ $match -ne 0 ]
			then
			let errcnt=$errcnt+1
			elog "ERROR: KEY SERVICE FAILED: $oneline"
		else
			logger -p local0.warning -t boot_check "WARNING: SERVICE FAILED: $oneline"
		fi
	done </tmp/$$.bootcheck.log

	ft_mode=`cat /proc/xiaoqiang/ft_mode`
	ft_ret=0
	if [ $errcnt -ne 0 ]
		then
		#
		elog "ERROR: $errcnt KEY SERVICE FAILED."
		elog "Try switch system...."
		#

                # save log to crash partition
		cd /tmp
		gzip messages
		mtd_crash_log -f messages.gz

		ft_ret=1
		if [ $ft_mode -ne 1 ]
		then
			failed_flag
			reboot
			exit -1
		fi
	fi

	# 检查 wifi是否正常
	wl1_count=`cat /proc/net/dev | grep wl1 | wc -l`
	if [ $wl1_count -eq 1 ]; then
		elog "INFO: Wireless OK"
        else
		elog "INFO: reboot times $try because of no wireless available."

		interface_info=`cat /proc/net/dev | awk '{print $1}'`
		elog "interface_info: $interface_info"

		wl1_info=`iwpriv wl1 e2p 0`
		elog "wl1_info: $wl1_info"

		mod_info=`lsmod`
		elog "mod_info: $mod_info"

		7628_info=`ls -l /etc/Wireless/mt7628/`
		elog "7628_info: $7628_info"

		7628_dat=`cat /etc/Wireless/mt7628/mt7628.dat`
		elog "7628_dat: $7628_dat"

		uci2dat_pid=`pidof uci2dat`
		if [ -n "$uci2dat_pid" ]
		then
			uci2dat_fd=`ls -l /proc/$uci2dat_pid/fd/`
			elog "uci2dat_fd: $uci2dat_fd"
		fi


		ft_ret=2

		if [ $ft_mode -ne 1 ]
		then
			nvram set bootcheck_wl1=1
			failed_flag
			reboot
			exit -1
		fi
	fi

	# 检查eth0 是否存在
	cat /proc/net/dev | grep -q "eth0" > /dev/null
	[ $? -ne 0 ] && {
		elog "INFO: no network available /dev/$hdd"
                # save log to crash partition
		cd /tmp
		gzip messages
		mtd_crash_log -f messages.gz

		ft_ret=3
		if [ $ft_mode -ne 1 ]
		then
			failed_flag
			reboot
			exit -1
		fi
	}

	if [ "$ft_mode" = "1" ]
	then
		if [ "$ft_ret" != "0" ]
		then
			elog "INFO: boot_check failed $ft_ret"
			dmesg > /data/usr/log/dmesg.log
			#red led is set
			gpio 1 0
			gpio 2 1
			gpio 3 1
		else
			echo 3 > /proc/xiaoqiang/boot_status
		fi
	else
		echo 3 > /proc/xiaoqiang/boot_status
	fi
}

stop() {
	echo "stop"
}
