#!/bin/sh

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

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
	./playOrRecord -p 0 audio/track_ok.pcm
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

if [ "$MotorSet" = "yes" ];then
	echo $MotorParams > /mnt/mtd/Log/motorset
	./playOrRecord -p 0 audio/motor_param_ok.pcm
fi

