#!/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`

CPUUPGRADE=`cat function.ini | ./busybox grep "upgrade:" | ./busybox grep -v grep | sed 's/upgrade://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
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"`

ISPLAYLEDBRIGHT=`cat $script_path/upgrade.ini | ./busybox grep "bPlayLedBright:" | ./busybox grep -v grep | sed 's/bPlayLedBright://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ISPLAYLEDFLICKER=`cat $script_path/upgrade.ini | ./busybox grep "bPlayLedFlicker:" | ./busybox grep -v grep | sed 's/bPlayLedFlicker://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DOUBLELEDMODE=`cat $script_path/upgrade.ini | ./busybox grep "DoubleLedMode:" | ./busybox grep -v grep | sed 's/DoubleLedMode://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLACKLEDMODE=`cat $script_path/upgrade.ini | ./busybox grep "BlackLedMode:" | ./busybox grep -v grep | sed 's/BlackLedMode://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DOUBLELIGHT_WHITELED_GROUP=`cat $script_path/upgrade.ini | ./busybox grep "doubleLight_whiteLed_group:" | ./busybox grep -v grep | sed 's/doubleLight_whiteLed_group://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
DOUBLELIGHT_WHITELED_NUM=`cat $script_path/upgrade.ini | ./busybox grep "doubleLight_whiteLed_num:" | ./busybox grep -v grep | sed 's/doubleLight_whiteLed_num://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLACKLIGHT_WHITELED_GROUP=`cat $script_path/upgrade.ini | ./busybox grep "blackLight_whiteLed_group:" | ./busybox grep -v grep | sed 's/blackLight_whiteLed_group://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLACKLIGHT_WHITELED_NUM=`cat $script_path/upgrade.ini | ./busybox grep "blackLight_whiteLed_num:" | ./busybox grep -v grep | sed 's/blackLight_whiteLed_num://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLACKLIGHT_SHIMMERLED_GROUP=`cat $script_path/upgrade.ini | ./busybox grep "blackLight_shimmerLed_group:" | ./busybox grep -v grep | sed 's/blackLight_shimmerLed_group://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
BLACKLIGHT_SHIMMERLED_NUM=`cat $script_path/upgrade.ini | ./busybox grep "blackLight_shimmerLed_num:" | ./busybox grep -v grep | sed 's/blackLight_shimmerLed_num://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`

function mcu_wait_system_reset()
{
	while true
	do
		echo "mcu upgrade success, system wait to be reset"
		sleep 2;
	done
	
	return 0;
}

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 [ "${CPUUPGRADE}" = "no" ];then
			if [ "${ISPLAYLOOP}" = "yes" ];then
				audio_play_mcu_upgrade_success
			fi
		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
	
	if [ "$ISPLAYLEDBRIGHT" = "yes" ];then
		if [ "$DOUBLELEDMODE" = "yes" ];then
			XmGpio sethigh "${DOUBLELIGHT_WHITELED_GROUP}" "${DOUBLELIGHT_WHITELED_NUM}"
		fi

		if [ "$BLACKLEDMODE" = "yes" ];then
			XmGpio sethigh "${BLACKLIGHT_WHITELED_GROUP}" "${BLACKLIGHT_WHITELED_NUM}"
			XmGpio sethigh "${BLACKLIGHT_SHIMMERLED_GROUP}" "${BLACKLIGHT_SHIMMERLED_NUM}"
		fi
	fi

	# 单片机升级前杀掉与单片机心跳工具,避免串口操作冲突
	# 心跳工具实现功能简单, 暂不采用循环检测是否杀掉, 默认等待2秒理论上成功 
	killall Mcu_Not_PowerOff
	sleep 2

	$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
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/mcuUpgrade_ok.pcm
	fi
	
	# 等待重启,单片机升级后会进行重启
	mcu_wait_system_reset
fi
