#!/bin/sh
#BurnIn Test Version: V1.02
#check BurnIn is Start,to BurnIn error
BurnInErr=0
if [ -e /home/BurnInStart ]; then
	killall led-control.sh
	cnt=0
	cnt1=0
	while [ -e /home/BurnInStart ]
	do
		cnt=$(($cnt+1))
		cnt1=$(($cnt1+1))
		if [ $cnt -eq 1000 ]; then
			#error LED
			echo toggle > /proc/gpio/LED2
			cnt=0
		fi
		if [ $cnt1 -eq 200 ]; then
			echo off > /proc/gpio/LED0
			echo off > /proc/gpio/LED1
			#echo off > /proc/gpio/LED2
			echo off > /proc/gpio/LED3
			echo off > /proc/gpio/LED4
			echo off > /proc/gpio/LED5
			echo off > /proc/gpio/LED6
			echo off > /proc/gpio/LED7
			#check default is press,abort
			grep 1 /proc/button/default
			if [ $? -eq 0 ]; then
				rm /home/BurnInStart
			fi
			cnt1=0
		fi	
	done
	BurnInErr=1
fi
#check is BurnIn mode
if [ -e /home/BurnIn ]; then
	echo > /home/BurnInStart
	for port in 1 2 3 4 5
	do
		rapi_client -p $port 100fdx
	done
	#BurnIn mode
	ping -I eth1 192.168.1.200 &
	killall led-control.sh
else
	#BurnIn error break
	if [ $BurnInErr -eq 1 ]; then
		#LED
		/etc/init.d/led start
		echo off > /proc/gpio/LED2
		echo on > /proc/gpio/LED6
		echo off > /proc/gpio/LED5
		exit
	else		
		exit
	fi	
fi

#BurnIn mode	
cnt=0
cnt1=0
while [ -e /home/BurnIn ]
do
	#button detect delay
	cnt=$(($cnt+1))
	if [ $cnt1 -lt 5000 ]; then
		cnt1=$(($cnt1+1))
	fi
			
	if [ $cnt -eq 200 ]; then
		echo toggle > /proc/gpio/LED0
		echo toggle > /proc/gpio/LED1
		echo toggle > /proc/gpio/LED2
		echo toggle > /proc/gpio/LED3
		echo toggle > /proc/gpio/LED4
		echo toggle > /proc/gpio/LED5
		echo toggle > /proc/gpio/LED6
		echo toggle > /proc/gpio/LED7
		#check default is press,abort
		grep 1 /proc/button/default
		if [ $? -eq 0 -a $cnt1 -eq 5000 ]; then
			rm /home/BurnInStart
			rm /home/BurnIn
		fi
		cnt=0
	fi
	if [ $cnt1 -eq 4999 ]; then
		arping -I eth0 192.168.1.200 &
	fi
done
#reboot to default
rm -rf /jffs/*
reboot

##reset settings
#killall ping

##LED
#/etc/init.d/led start
#echo on > /proc/gpio/LED7
#echo on > /proc/gpio/LED4
#wireless LED
#if [ -s /etc/config/wireless ]; then
#	echo on > /proc/gpio/LED1
#else
#	echo off > /proc/gpio/LED1	
#fi
#lan port	
#for port in 1 2 3 4 5
#do
#	rapi_client -p $port auto
#done