#!/bin/sh

#sleep 5

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"

export LD_LIBRARY_PATH=/${script_path}/lib/:$LD_LIBRARY_PATH

sleep 1

# GPIO50 ---> 1 for spk
XmGpio sethigh 0 4

# GPIO00 wifi enable
XmGpio setlow 1 0


#count=0
#istrue=true
#while [ "$istrue" = true ]
#do
#ps > /tmp/ps.txt
#ProcNumber=`ps | ./busybox grep "/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

killall App
auto_feed_watchdog.sh &

if [ -f /${script_path}/mac ]; then
	rm -rf /${script_path}/mac
fi

./getMacAddr ${script_path}

cd $script_path/
Volume=`cat function.ini | ./busybox grep "volume:" | ./busybox grep -v grep | sed 's/volume://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
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"`
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"`

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"`
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"`
DEFAULTVOLUME=`cat function.ini | ./busybox grep "DefaultVolume:" | ./busybox grep -v grep | sed 's/DefaultVolume://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
MCUUPGRADE=`cat function.ini | ./busybox grep "mcuUpgrade:" | ./busybox grep -v grep | sed 's/mcuUpgrade://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LAMPMCUUPGRADE=`cat function.ini | ./busybox grep "lampMcuUpgrader:" | ./busybox grep -v grep | sed 's/lampMcuUpgrader://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ROADLAMP=`cat function.ini | ./busybox grep "roadLamp:" | ./busybox grep -v grep | sed 's/roadLamp://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ALARMOUTENABLE=`cat function.ini | ./busybox grep "alarmOutEnable:" | ./busybox grep -v grep | sed 's/alarmOutEnable://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
STORAGESPACEUSEPERCENT=`cat function.ini | ./busybox grep "storageSpaceUsePercent:" | ./busybox grep -v grep | sed 's/storageSpaceUsePercent://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LUATUPGRADE=`cat function.ini | ./busybox grep "luatUpgrade:" | ./busybox grep -v grep | sed 's/luatUpgrade://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
VIDEOMIRRORFLIP=`cat function.ini | ./busybox grep "videoMirrorFlip:" | ./busybox grep -v grep | sed 's/videoMirrorFlip://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"`
LED=`cat function.ini | ./busybox grep "led:" | ./busybox grep -v grep | sed 's/led://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
INSMODMOTOR=`cat function.ini | ./busybox grep "insmodMotor:" | ./busybox grep -v grep | sed 's/insmodMotor://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WIRENETWORK=`cat function.ini | ./busybox grep "wireNetwork:" | ./busybox grep -v grep | sed 's/wireNetwork://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
AOVWORKMODE=`cat function.ini | ./busybox grep "AovWorkMode:" | ./busybox grep -v grep | sed 's/AovWorkMode://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LOWELECTRMODE=`cat function.ini | ./busybox grep "LowElectrMode:" | ./busybox grep -v grep | sed 's/LowElectrMode://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
AUDIOPOWERCONTROL=`cat function.ini | ./busybox grep "audioPowerControl:" | ./busybox grep -v grep | sed 's/audioPowerControl://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
HUMANTRACK=`cat function.ini | ./busybox grep "HumanTrack:" | ./busybox grep -v grep | sed 's/HumanTrack://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WHITELIGHT=`cat function.ini | ./busybox grep "WhiteLight:" | ./busybox grep -v grep | sed 's/WhiteLight://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ALARMLED=`cat function.ini | ./busybox grep "alarmLed:" | ./busybox grep -v grep | sed 's/alarmLed://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DEFAULTVOLUMEPERCENTAGE=`cat function.ini | ./busybox grep "defaultVolumePercentage:" | ./busybox grep -v grep | sed 's/defaultVolumePercentage://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
AGINGTEST=`cat function.ini | ./busybox grep "agingtest:" | ./busybox grep -v grep | sed 's/agingtest://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DUALSIM=`cat function.ini | ./busybox grep "dualSim:" | ./busybox grep -v grep | sed 's/dualSim://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DEVICEINFO=`cat function.ini | ./busybox grep "deviceInfo:" | ./busybox grep -v grep | sed 's/deviceInfo://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ROADLAMPWORKPARAM=`cat function.ini | ./busybox grep "roadLampWorkParam:" | ./busybox grep -v grep | sed 's/roadLampWorkParam://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
AUDIOTIP=`cat function.ini | ./busybox grep "audioTip:" | ./busybox grep -v grep | sed 's/audioTip://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`

#MIRROR FLIP CONTROL

if [[ "$UPGRADE" != "yes" && "$MCUUPGRADE" != "yes" && "$LUATUPGRADE" != "yes" ]];then
	./simple_adec_bind_ao -r 8000  -v $Volume -t g711a -i audio/FactoryTestStart.pcm
fi

if [ "$CLEAR" = "yes" ];then
	echo CLEAR begin >>test
	$script_path/clear/clear.sh $AUDIOTIP
	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 $AUDIOTIP
fi

if [ "$LAMPMCUUPGRADE" = "yes" ];then
	echo LAMPMCUUPGRADE begin >>test
	$script_path/lampMcuUpgrader/lampMcuUpgrader.sh $AUDIOTIP
fi

if [ "$MCUUPGRADE" = "yes" ];then
	echo MCUUPGRADE begin >>test
	$script_path/mcuUpgrade/mcuUpgrade.sh $AUDIOTIP
fi

if [ "$ROADLAMP" = "yes" ];then
	echo ROADLAMP begin >>test
	$script_path/roadLamp/roadLamp.sh $AUDIOTIP
fi

if [ "$LUATUPGRADE" = "yes" ]; then
	echo LUATUPGRADE Enter >> test
	$script_path/luat_upgrade/luat_upgrade.sh $AUDIOTIP
fi

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

if [ "$INSMODMOTOR" = "yes" ]; then
	echo INSMODMOTOR Enter >> test
	$script_path/insmodMotor/insmodMotor.sh $AUDIOTIP
fi

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

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

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

if [ "$PRODUCTIONTESTMODE" = "yes" ];then
	echo PRODUCTIONTESTMODE begin >>test
	$script_path/ProductionTestMode/ProductionTestMode.sh $AUDIOTIP
fi
if [ "$CUSTOMAPNLIST" = "yes" ];then
	echo CUSTOMAPNLIST begin >>test
	$script_path/CustomApnList/CustomApnList.sh $AUDIOTIP
fi
if [ "$DEFAULTVOLUME" = "yes" ];then
	echo DEFAULTVOLUME begin >>test
	$script_path/DefaultVolume/DefaultVolume.sh $AUDIOTIP
fi
if [ "$ALARMOUTENABLE" = "yes" ];then
	echo ALARMOUTENABLE begin >>test
	$script_path/alarmOutEnable/alarmOutEnable.sh $AUDIOTIP
fi
if [ "$STORAGESPACEUSEPERCENT" = "yes" ];then
	echo STORAGESPACEUSEPERCENT begin >>test
	$script_path/storageSpaceUsePercent/storageSpaceUsePercent.sh $AUDIOTIP
fi
if [ "$VIDEOMIRRORFLIP" = "yes" ];then
	echo VIDEOMIRRORFLIP begin >>test
	$script_path/videoMirrorFlip/videoMirrorFlip.sh $AUDIOTIP
fi
if [ "$LED" = "yes" ];then
	echo led begin >>test
	$script_path/led/led.sh $AUDIOTIP
fi
if [ "$WIRENETWORK" = "yes" ];then
	echo WIRENETWORK begin >>test
	$script_path/wireNetwork/wireNetwork.sh $AUDIOTIP
fi
if [ "$AOVWORKMODE" = "yes" ];then
	echo AOVWORKMODE begin >>test
	$script_path/AovWorkMode/AovWorkMode.sh $AUDIOTIP
fi
if [ "$LOWELECTRMODE" = "yes" ];then
	echo LOWELECTRMODE begin >>test
	$script_path/LowElectrMode/LowElectrMode.sh $AUDIOTIP
fi
if [ "$AUDIOPOWERCONTROL" = "yes" ];then
	echo AUDIOPOWERCONTROL begin >>test
	$script_path/audioPowerControl/audioPowerControl.sh $AUDIOTIP
fi
if [ "$HUMANTRACK" = "yes" ];then
	echo HUMANTRACK begin >>test
	$script_path/humanTrack/humanTrack.sh $AUDIOTIP
fi
if [ "$WHITELIGHT" = "yes" ];then
	echo WHITELIGHT begin >>test
	$script_path/whiteLight/whiteLight.sh $AUDIOTIP
fi
if [ "$ALARMLED" = "yes" ];then
	echo ALARMLED begin >>test
	$script_path/alarmLed/alarmLed.sh $AUDIOTIP
fi
if [ "$DEFAULTVOLUMEPERCENTAGE" = "yes" ];then
	echo DEFAULTVOLUMEPERCENTAGE begin >>test
	$script_path/defaultVolumePercentage/defaultVolumePercentage.sh $AUDIOTIP
fi
if [ "$AGINGTEST" = "yes" ];then
	echo AGINGTEST begin >>test
	$script_path/agingTest/agingTest.sh $AUDIOTIP
fi
if [ "$DUALSIM" = "yes" ];then
	echo DUALSIM begin >>test
	$script_path/dualSim/dualSim.sh $AUDIOTIP
fi
if [ "$DEVICEINFO" = "yes" ];then
	echo DEVICEINFO begin >>test
	$script_path/deviceInfo/deviceInfo.sh $AUDIOTIP
fi
if [ "$ROADLAMPWORKPARAM" = "yes" ];then
	echo ROADLAMPWORKPARAM begin >>test
	$script_path/roadLampWorkParam/roadLampWorkParam.sh $AUDIOTIP
fi

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

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

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

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

sleep 1

./simple_adec_bind_ao -r 8000 -t g711a -v $Volume -i audio/FactoryTestFinished.pcm

sync
