#!/bin/sh

lAudioTip=$1

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

filename=$script_path/trackset.ini

getconf()
{
	val=`cat $1 | grep ^\\s*$2: | sed -e "s/\\s*$2://g" -e "s/\r//g" -e "s/\n//g" -e "s/$(echo -e '\015')//g"`
	if [ ! $val ]; then
		echo "getconf $2 failed" >> $script_path/test
	fi

	echo -n $val
}

TrackSet=`cat $script_path/trackset.ini | ./busybox grep "trackset:" | ./busybox grep -v grep | sed 's/trackset://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
MotorLeft=`cat $script_path/trackset.ini | ./busybox grep "motor_left:" | ./busybox grep -v grep | sed 's/motor_left://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
MotorUp=`cat $script_path/trackset.ini | ./busybox grep "motor_up:" | ./busybox grep -v grep | sed 's/motor_up://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
WidthSteps=`cat $script_path/trackset.ini | ./busybox grep "width_steps:" | ./busybox grep -v grep | sed 's/width_steps://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
HeightSteps=`cat $script_path/trackset.ini | ./busybox grep "height_steps:" | ./busybox grep -v grep | sed 's/height_steps://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
VerticalDegrees=`cat $script_path/trackset.ini | ./busybox grep "vertical_degrees:" | ./busybox grep -v grep | sed 's/vertical_degrees://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi

if [ "$TrackSet" = "yes" ];then
	rm -rf /mnt/mtd/Log/trackset
	touch /mnt/mtd/Log/trackset
	echo "motor_left:$MotorLeft" >> /mnt/mtd/Log/trackset
	echo "motor_up:$MotorUp" >> /mnt/mtd/Log/trackset
	echo "width_steps:$WidthSteps" >> /mnt/mtd/Log/trackset
	echo "height_steps:$HeightSteps" >> /mnt/mtd/Log/trackset
	echo "vertical_degrees:$VerticalDegrees" >> /mnt/mtd/Log/trackset
	if [ "$lAudioTip" != "no" ];then
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/track_ok.pcm
	fi
fi

[ -e "/mnt/mtd/Log/PtzLocateSet" ] && rm /mnt/mtd/Log/PtzLocateSet
PtzLocateSet=$(getconf $filename PtzLocateSet)
if [ "$PtzLocateSet" = "yes" ];then
	touch /mnt/mtd/Log/PtzLocateSet

	PtzLocate_CenterX=$(getconf $filename PtzLocate_CenterX)
	PtzLocate_CenterY=$(getconf $filename PtzLocate_CenterY)
	PtzLocate_WidthSteps=$(getconf $filename PtzLocate_WidthSteps)
	PtzLocate_HeightSteps=$(getconf $filename PtzLocate_HeightSteps)
	PtzAdjust_WidthSteps=$(getconf $filename PtzAdjust_WidthSteps)
	PtzAdjust_HeightSteps=$(getconf $filename PtzAdjust_HeightSteps)

	echo "PtzLocate_CenterX:$PtzLocate_CenterX" > /mnt/mtd/Log/PtzLocateSet
	echo "PtzLocate_CenterY:$PtzLocate_CenterY" >> /mnt/mtd/Log/PtzLocateSet
	echo "PtzLocate_WidthSteps:$PtzLocate_WidthSteps" >> /mnt/mtd/Log/PtzLocateSet
	echo "PtzLocate_HeightSteps:$PtzLocate_HeightSteps" >> /mnt/mtd/Log/PtzLocateSet
	echo "PtzAdjust_WidthSteps:$PtzAdjust_WidthSteps" >> /mnt/mtd/Log/PtzLocateSet
	echo "PtzAdjust_HeightSteps:$PtzAdjust_HeightSteps" >> /mnt/mtd/Log/PtzLocateSet
#	if [ "$lAudioTip" != "no" ];then
#		./simple_adec_bind_ao -r 8000 -t g711a -i audio/track_ok.pcm
#	fi
fi

MotorSet=`cat $script_path/trackset.ini | ./busybox grep "motorset:" | ./busybox grep -v grep | sed 's/motorset://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi
MotorParams=`cat $script_path/trackset.ini | ./busybox grep "motor_params:" | ./busybox grep -v grep | sed 's/motor_params://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
if [ $? != 0 ]; then
	echo "cann't found trackset ini config file"
	exit -1
fi

PtzLocate_Motor_Set=$(getconf $filename PtzLocate_Motor_Set)
PtzLocate_Motor_flip=$(getconf $filename PtzLocate_Motor_flip)
PtzLocate_Motor_mirror=$(getconf $filename PtzLocate_Motor_mirror)
if [ "$PtzLocate_Motor_Set" = "yes" ]; then
	if [ "$PtzLocate_Motor_flip" = "no" ] && [ "$PtzLocate_Motor_mirror" = "no" ]; then
		pin_antitone=0
	elif [ "$PtzLocate_Motor_flip" = "no" ] && [ "$PtzLocate_Motor_mirror" = "yes" ]; then
		pin_antitone=1
	elif [ "$PtzLocate_Motor_flip" = "yes" ] && [ "$PtzLocate_Motor_mirror" = "no" ]; then
		pin_antitone=2
	elif [ "$PtzLocate_Motor_flip" = "yes" ] && [ "$PtzLocate_Motor_mirror" = "yes" ]; then
		pin_antitone=3
	fi
else
	pin_antitone=0
fi
		
if [ "$MotorSet" = "yes" ];then
	echo -n "$MotorParams pin_antitone=$pin_antitone " > /mnt/mtd/Log/motorset
	if [ -e "/mnt/mtd/Log/motorset" ] && [ -e /usr/lib/modules/XmMotor.ko ];then
		sleep 1
		motor_params=`cat /mnt/mtd/Log/motorset`
		rmmod XmMotor
		sleep 1
		insmod /usr/lib/modules/XmMotor.ko ${motor_params}
	fi
	if [ "$lAudioTip" != "no" ];then
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/motor_param_ok.pcm
	fi
fi

if [ "$MotorSet" = "no" ];then
	rm -rf /mnt/mtd/Log/motorset
fi
