#!/bin/sh

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

MCU_UPGRADE_FILE=mcu_ac1003_aov.bin
ismcuupgraded=no
ETH0MAC_FILE=mcu_eth0mac.cfg
ETH0MAC=`cat ./mac | head -n 1`


ISPLAYLOOP=`cat $script_path/mcuUpgrade.ini | ./busybox grep "bPlayLoop:" | ./busybox grep -v grep | sed 's/bPlayLoop://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`

function audio_play_mcu_upgrade_success()
{
	while true
	do
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/mcuUpgrade_ok.pcm
		sleep 3;
	done
	
	return 0;
}
	
if [ -f $script_path/$ETH0MAC_FILE ]; then
	ETH0MAC4FILE=`cat $script_path/$ETH0MAC_FILE | ./busybox grep "eth0mac:" | ./busybox grep -v grep | sed 's/eth0mac://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
	if [ "$ETH0MAC" = "$ETH0MAC4FILE" ];then
		ismcuupgraded=yes
		if [ "${ISPLAYLOOP}" = "yes" ];then
			audio_play_mcu_upgrade_success
		fi
	fi
fi

echo "ETH0MAC:$ETH0MAC ETH0MAC4FILE:$ETH0MAC4FILE "
if [ ! -f $script_path/$MCU_UPGRADE_FILE ]; then
	echo "cann't found mcu upgrade file"
	./simple_adec_bind_ao -r 8000 -t g711a -i audio/mcuUpgradeflie_fail.pcm
	exit -1
fi
if [ "$ismcuupgraded" = "no" ];then
	./simple_adec_bind_ao -r 8000 -t g711a -i audio/start_mcuUpgrade.pcm
	sleep 5
	$script_path/mcu_upgrader $script_path/$MCU_UPGRADE_FILE
	if [ $? != 0 ]; then
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/mcuUpgrade_fail.pcm
	else
		echo "eth0mac:$ETH0MAC" > $script_path/$ETH0MAC_FILE
		sync
		sleep 5
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/mcuUpgrade_ok.pcm
	fi
fi
