#!/bin/sh

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

SET_INFRAREDREVERSE="initInfraredReverse"
SET_INFRAREDRECOVER="initInfraredRecover"
SETTED_INFRAREDREVERSE="InfraredReverseFlag"
SETTED_INFRAREDRECOVER="InfraredRecoverFlag"
FLAGS_DIR="/mnt/mtd/Flags"

INFRARED_REVERSE=`cat $script_path/InfraredReverse.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"`
if [ $? != 0 ]; then
	echo "cann't found InfraredReverse ini config file"
	exit -1
fi
echo isInfraredReverse:$INFRARED_REVERSE


if [ "$INFRARED_REVERSE" = "yes" ] ||
	[ "$INFRARED_REVERSE" = "no" ]
then
	if [ "$INFRARED_REVERSE" = "yes" ]; then
		if [ -f $FLAGS_DIR/$SET_INFRAREDRECOVER ];then
			rm -f $FLAGS_DIR/$SET_INFRAREDRECOVER
		fi
		if [ -f $FLAGS_DIR/$SETTED_INFRAREDRECOVER ];then
			rm -f $FLAGS_DIR/$SETTED_INFRAREDRECOVER
		fi
		if [ -f $FLAGS_DIR/$SETTED_INFRAREDREVERSE ];then
			rm -f $FLAGS_DIR/$SETTED_INFRAREDREVERSE
		fi		
		
		touch $FLAGS_DIR/$SET_INFRAREDREVERSE
		if [ -f /mnt/mtd/Flags/initInfraredReverse ];then
			./playOrRecord -p 0 audio/initInfraredReverse.pcm
		fi
	else
		if [ -f $FLAGS_DIR/$SET_INFRAREDREVERSE ];then
			rm -f $FLAGS_DIR/$SET_INFRAREDREVERSE
		fi
		if [ -f $FLAGS_DIR/$SETTED_INFRAREDREVERSE ];then
			rm -f $FLAGS_DIR/$SETTED_INFRAREDREVERSE
		fi
		if [ -f $FLAGS_DIR/$SETTED_INFRAREDRECOVER ];then
			rm -f $FLAGS_DIR/$SETTED_INFRAREDRECOVER
		fi
		touch $FLAGS_DIR/$SET_INFRAREDRECOVER
		if [ -f /mnt/mtd/Flags/initInfraredRecover ];then
			./playOrRecord -p 0 audio/initInfraredRecover.pcm
		fi
	fi
fi
