#!/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=`sed -n "s/ethaddr0:\(.*\).*/\1\n/p" /proc/xm/xminfo`
ETH0MAC_DEFAULT="00:00:23:34:45:66"

if [ -f upgrade/eth0mac.cfg ];then
	ETH0MAC4FILE1=`cat upgrade/eth0mac.cfg | ./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"`
	echo "ETH0MAC4FILE1:$ETH0MAC4FILE1"
	ETH0MAC=$ETH0MAC4FILE1
fi
	
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
	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
