#!/bin/sh

lAudioTip=$1

script_path=`echo $0 | awk -F '/' '{for(i=2;i<NF;i++) printf"/"$i}'`
echo $script_path

LED_CTRL_FLICKER=`cat $script_path/gpioctrl.ini | ./busybox grep "LED_CTRL_FLICKER:" | ./busybox grep -v grep | sed 's/LED_CTRL_FLICKER://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO1=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO1:" | ./busybox grep -v grep | sed 's/GPIO1://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Group1=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Group1:" | ./busybox grep -v grep | sed 's/GPIO_Group1://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Num1=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Num1:" | ./busybox grep -v grep | sed 's/GPIO_Num1://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO2=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO2:" | ./busybox grep -v grep | sed 's/GPIO2://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Group2=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Group2:" | ./busybox grep -v grep | sed 's/GPIO_Group2://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Num2=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Num2:" | ./busybox grep -v grep | sed 's/GPIO_Num2://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO3=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO3:" | ./busybox grep -v grep | sed 's/GPIO3://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Group3=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Group3:" | ./busybox grep -v grep | sed 's/GPIO_Group3://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO_Num3=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO_Num3:" | ./busybox grep -v grep | sed 's/GPIO_Num3://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
GPIO4=`cat $script_path/gpioctrl.ini | ./busybox grep "GPIO4:" | ./busybox grep -v grep | sed 's/GPIO4://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ONLYPLAYAUDIO=`cat $script_path/gpioctrl.ini | ./busybox grep "bOnlyPlayAudio:" | ./busybox grep -v grep | sed 's/bOnlyPlayAudio://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`

function audio_play_1KHZ()
{
	while true
	do
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/1KHz.pcm
		sleep 3;
	done
	
	return 0;
}

if [ $? != 0 ]; then
	echo "cann't found gpioctrl ini config file"
	exit -1
fi

if [ "$lAudioTip" != "no" ];then
	./simple_adec_bind_ao -r 8000 -t g711a -i audio/agingtest.pcm
fi

if [ "$ONLYPLAYAUDIO" = "yes" -a "$lAudioTip" != "no" ];then
	audio_play_1KHZ
fi

./agingTest/motor &

while [ 1 ]
do
	if [ "$lAudioTip" != "no" ];then
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/ageTestInProgress.pcm
	fi

	if [ "$GPIO1" = "yes" ];then
		XmGpio sethigh $GPIO_Group1 $GPIO_Num1
	fi
	
	if [ "$GPIO3" = "yes" ];then
		XmGpio sethigh $GPIO_Group3 $GPIO_Num3
	fi
	
	if [ "$LED_CTRL_FLICKER" = "yes" ];then
		if [ "$GPIO2" = "yes" ];then
			XmGpio setlow $GPIO_Group2 $GPIO_Num2
		fi
		
		if [ "$GPIO4" = "yes" ];then
			io -4 0xFF538008 0x00070000
			io -4 0xFF530008 0x01000100
			io -4 0xFF530000 0x01000000
		fi
	fi
	
	sleep 10
	
	if [ "$LED_CTRL_FLICKER" = "yes" ];then
		if [ "$GPIO1" = "yes" ];then
			XmGpio setlow $GPIO_Group1 $GPIO_Num1
		fi
		
		if [ "$GPIO3" = "yes" ];then
			XmGpio setlow $GPIO_Group3 $GPIO_Num3
		fi
	fi
	
	if [ "$GPIO2" = "yes" ];then
		XmGpio sethigh $GPIO_Group2 $GPIO_Num2
	fi
	
	if [ "$GPIO4" = "yes" ];then
		io -4 0xFF538008 0x00070000
		io -4 0xFF530008 0x01000100
		io -4 0xFF530000 0x01000100
	fi
	
	sleep 10
done

