#!/bin/sh

#sleep 5
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/var/tmp/mmcblock0/lib
get_path()
{
	i=1
	sdpath=""
	lastele=""
	OLDIFS=$IFS;IFS='\/'
	for elem in $0
	do
		if [ $i -eq 1 ]; then
			if [ -z $elem ]; then
				lastele=""
			else
				lastele=$elem
			fi
		else
			sdpath=$sdpath$lastele"="
			lastele=$elem
		fi
		i=$(($i + 1))
	done
	IFS=OLDIFS
	
	sdpath=`echo $sdpath | sed 's/=/\//g'`
	echo $sdpath
}

script_path=`cd "$(get_path "$0")";pwd`

FLAGS_DIR="/mnt/mtd/Flags"
LOG_DIR="/mnt/mtd/Log"

sleep 1

# GK7201V300
# GPIO84 ---> 1 for spk
XmGpio sethigh 8 4

# GPIO10 wifi enable
XmGpio setlow 1 0

./getAudioPowerCtrlGpio
if [ $? -eq 0 ]; then
	output=$(cat AudioPowerCtrlGpio)
	XmGpio setdirout $output
	XmGpio sethigh $output
fi

count=0
istrue=true
while [ "$istrue" = true ]
do
ps > /tmp/ps.txt
ProcNumber=`ps | ./busybox grep "/usr/bin/App" | ./busybox grep -v grep | $script_path/busybox wc -l`
if [ "$ProcNumber" -eq 1 ];then
        killall App
        istrue=no
fi

if [ "$count" -gt 30 ];then
	killall App
	istrue=no
fi

let count+=1
sleep 1
done

#rmmod hi3516ev200_wdt
#insmod /usr/lib/modules/hi3516ev200_wdt.ko

cd $script_path/

MOTOR=`cat function.ini | ./busybox grep "motor:" | ./busybox grep -v grep | sed 's/motor://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
AUDIO=`cat function.ini | ./busybox grep "audio:" | ./busybox grep -v grep | sed 's/audio://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WIFI=`cat function.ini | ./busybox grep "wifi:" | ./busybox grep -v grep | sed 's/wifi://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
UPGRADE=`cat function.ini | ./busybox grep "upgrade:" | ./busybox grep -v grep | sed 's/upgrade://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LANG=`cat function.ini | ./busybox grep "language:" | ./busybox grep -v grep | sed 's/language://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WIFISTARTUP=`cat function.ini | ./busybox grep "wifistartup:" | ./busybox grep -v grep | sed 's/wifistartup://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
MIRRORXFLIP=`cat function.ini | ./busybox grep "mirrorXflip:" | ./busybox grep -v grep | sed 's/mirrorXflip://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
CLOUDSERVER=`cat function.ini | ./busybox grep "cloudserver:" | ./busybox grep -v grep | sed 's/cloudserver://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
TRACK=`cat function.ini | ./busybox grep "track:" | ./busybox grep -v grep | sed 's/track://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
IRSWITCH=`cat function.ini | ./busybox grep "irSwitch:" | ./busybox grep -v grep | sed 's/irSwitch://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
INFRAREDREVERSE=`cat function.ini | ./busybox grep "InfraredReverse:" | ./busybox grep -v grep | sed 's/InfraredReverse://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
CLEAR=`cat function.ini | ./busybox grep "clear:" | ./busybox grep -v grep | sed 's/clear://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DAYNIGHT=`cat function.ini | ./busybox grep "dayNight:" | ./busybox grep -v grep | sed 's/dayNight://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DOUBLELIGHT=`cat function.ini | ./busybox grep "doubleLight:" | ./busybox grep -v grep | sed 's/doubleLight://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
IRCUTREVERSE=`cat function.ini | ./busybox grep "IRCutReverse:" | ./busybox grep -v grep | sed 's/IRCutReverse://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
TALK=`cat function.ini | ./busybox grep "talk:" | ./busybox grep -v grep | sed 's/talk://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
PTZDIRECTION=`cat function.ini | ./busybox grep "ptzDirection:" | ./busybox grep -v grep | sed 's/ptzDirection://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
PTZCTRLCLOSE=`cat function.ini | ./busybox grep "ptzCtrlClose:" | ./busybox grep -v grep | sed 's/ptzCtrlClose://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
IPERF=`cat function.ini | ./busybox grep "iperf:" | ./busybox grep -v grep | sed 's/iperf://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLE=`cat function.ini | ./busybox grep "ble:" | ./busybox grep -v grep | sed 's/ble://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LAMPCAMERA=`cat function.ini | ./busybox grep "lampCamera:" | ./busybox grep -v grep | sed 's/lampCamera://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
CUSTOMAPNLIST=`cat function.ini | ./busybox grep "customApnList:" | ./busybox grep -v grep | sed 's/customApnList://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
PRODUCTIONTESTMODE=`cat function.ini | ./busybox grep "productionTestMode:" | ./busybox grep -v grep | sed 's/productionTestMode://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WIRESCHEDULE=`cat function.ini | ./busybox grep "wireSchedule:" | ./busybox grep -v grep | sed 's/wireSchedule://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
TEST4G=`cat function.ini | ./busybox grep "4g:" | ./busybox grep -v grep | sed 's/4g://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LCD=`cat function.ini | ./busybox grep "lcd:" | ./busybox grep -v grep | sed 's/lcd://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
SCREEN=`cat function.ini | ./busybox grep "screen:" | ./busybox grep -v grep | sed 's/screen://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLEBUTTONALARM=`cat function.ini | ./busybox grep "BleButtonAlarm:" | ./busybox grep -v grep | sed 's/BleButtonAlarm://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
UPLOADOEMPID=`cat function.ini | ./busybox grep "UploadOEMPID:" | ./busybox grep -v grep | sed 's/UploadOEMPID://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
#MIRROR FLIP CONTROL

./playOrRecord -p 0 audio/FactoryTestStart.pcm

if [ "$WIRESCHEDULE" = "yes" ];then
	echo WIRECHEDULE begin >> test
	if [ ! -f /mnt/mtd/Flags/Disable4gEth0Flag ]; then
		touch /mnt/mtd/Flags/Disable4gEth0Flag
	fi
else
	if [ -f /mnt/mtd/Flags/Disable4gEth0Flag ]; then
		rm -rf /mnt/mtd/Flags/Disable4gEth0Flag
	fi
fi


if [ "$CLEAR" = "yes" ];then
	echo CLEAR begin >>test
	$script_path/clear/clear.sh
	sleep 1

	if [ ! -d $FLAGS_DIR ]; then
		mkdir $FLAGS_DIR
	fi
	
	if [ ! -d $LOG_DIR ]; then
		mkdir $LOG_DIR
	fi
fi



if [ "$UPGRADE" = "yes" ];then
	echo UPGRADE begin >>test
	$script_path/upgrade/upgrade.sh
fi

if [ "$MIRRORXFLIP" = "yes" ];then
	echo mirrorXflip begin >> test
	$script_path/mirrorXflip/mirrorXflip.sh
fi

if [ "$LCD" = "yes" ]; then
	echo LCD begin >>test
	$script_path/lcd/lcd.sh
fi

if [ "$SCREEN" = "yes" ]; then
	echo SCREEN begin >>test
	$script_path/screen/screen.sh
fi

if [ "$LANG" = "yes" ];then
	echo LANG begin >>test
	$script_path/language/language.sh
fi

if [ "$WIFISTARTUP" = "yes" ];then
	echo WIFISTARTUP begin >>test
	$script_path/wifiStartupType/wifiStartupType.sh
fi

if [ "$MOTOR" = "yes" ];then
	echo MOTOR begin >>test
	$script_path/motor/motor.sh
fi
if [ "$AUDIO" = "yes" ];then
	echo AUDIO begin >>test
	$script_path/audio/audio.sh
fi
if [ "$CLOUDSERVER" = "yes" ];then
	echo CLOUDSERVER begin >>test
	$script_path/cloudServer/cloudServer.sh
fi
if [ "$TRACK" = "yes" ];then
	echo TRACK begin >>test
	$script_path/track/trackset.sh
fi
if [ "$IRSWITCH" = "yes" ];then
	echo IRSWITCH begin >>test
	$script_path/irSwitch/irSwitch.sh
fi
if [ "$INFRAREDREVERSE" = "yes" ];then
	echo INFRAREDREVERSE begin >>test
	$script_path/InfraredReverse/InfraredReverse.sh
fi
if [ "$DAYNIGHT" = "yes" ];then
	echo DAYNIGHT begin >>test
	$script_path/dayNight/dayNight.sh
fi
if [ "$DOUBLELIGHT" = "yes" ];then
	echo DOUBLELIGHT begin >>test
	$script_path/doubleLight/doubleLight.sh
fi
if [ "$IRCUTREVERSE" = "yes" ];then
	echo IRCUTREVERSE begin >>test
	$script_path/IRCutReverse/IRCutReverse.sh
fi
if [ "$TALK" = "yes" ];then
	echo TALK begin >>test
	$script_path/talk/talk.sh
fi
if [ "$PTZDIRECTION" = "yes" ];then
	echo PTZDIRECTION begin >>test
	$script_path/ptzDirection/ptzDirection.sh
fi
if [ "$PTZCTRLCLOSE" = "yes" ];then
	echo PTZCTRLCLOSE begin >>test
	$script_path/ptzCtrlClose/ptzCtrlClose.sh
fi
if [ "$LAMPCAMERA" = "yes" ];then
	echo LAMPCAMERA begin >>test
	$script_path/lampCamera/lampCamera.sh
fi

if [ "$CUSTOMAPNLIST" = "yes" ];then
	echo CUSTOMAPNLIST begin >>test
	$script_path/customApnList/customApnList.sh
fi

if [ "$PRODUCTIONTESTMODE" = "yes" ];then
	echo PRODUCTIONTESTMODE begin >>test
	$script_path/productionTestMode/productionTestMode.sh
fi

if [ "$BLEBUTTONALARM" = "yes" ];then
	echo BLEBUTTONALARM begin >>test
	$script_path/BleButtonAlarm/BleButtonAlarm.sh
fi

rm -rf /mnt/mtd/Config/automaintainreboot.cfg

if [ "$BLE" = "yes" ];then
	echo BLE begin >>test
	$script_path/ble/ble.sh
fi

if [ "$WIFI" = "yes" ];then
	echo WIFI begin >>test
	$script_path/wifiAll/wifiAll.sh
fi

if [ "$IPERF" = "yes" ];then
	echo IPERF begin >>test
	$script_path/iperf/iperf.sh
fi

if [ "$TEST4G" = "yes" ];then
	echo TEST4G begin >>test
	$script_path/4g/4gAll.sh
fi

if [ "$UPLOADOEMPID" = "yes" ];then
	echo UPLOADOEMPID begin >>test
	$script_path/uploadOEMPID/uploadOEMPID.sh
fi

sleep 1

./playOrRecord -p 0 audio/FactoryTestFinished.pcm