#!/bin/sh

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

SD_UPGRADE_FILE=sd_upgrade_all.bin
ETH0MAC_FILE=eth0mac.cfg
isupgraded=no
ETH0MAC=`cat ./mac | head -n 1`

MCUUPGRADE=`cat function.ini | ./busybox grep "mcuUpgrade:" | ./busybox grep -v grep | sed 's/mcuUpgrade://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
LAMPMCUUPGRADE=`cat function.ini | ./busybox grep "lampMcuUpgrader:" | ./busybox grep -v grep | sed 's/lampMcuUpgrader://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
ISPLAYLOOP=`cat $script_path/upgrade.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_upgrade_success()
{
	while true
	do
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/upgrade_ok.pcm
		sleep 3;
	done
	
	return 0;
}

function blackLight_led_flicker_upgrade_success()
{
	while true
	do
		XmGpio sethigh "${BLACKLIGHT_WHITELED_GROUP}" "${BLACKLIGHT_WHITELED_NUM}"
		XmGpio sethigh "${BLACKLIGHT_SHIMMERLED_GROUP}" "${BLACKLIGHT_SHIMMERLED_NUM}"
		sleep 2
		XmGpio setlow "${BLACKLIGHT_WHITELED_GROUP}" "${BLACKLIGHT_WHITELED_NUM}"
		XmGpio setlow "${BLACKLIGHT_SHIMMERLED_GROUP}" "${BLACKLIGHT_SHIMMERLED_NUM}"
		sleep 2
	done
	
	return 0;
}

function blackLight_led_flicker_upgrade_fail()
{
	while true
	do
		XmGpio sethigh "${BLACKLIGHT_WHITELED_GROUP}" "${BLACKLIGHT_WHITELED_NUM}"
		XmGpio sethigh "${BLACKLIGHT_SHIMMERLED_GROUP}" "${BLACKLIGHT_SHIMMERLED_NUM}"
		sleep 0.5
		XmGpio setlow "${BLACKLIGHT_WHITELED_GROUP}" "${BLACKLIGHT_WHITELED_NUM}"
		XmGpio setlow "${BLACKLIGHT_SHIMMERLED_GROUP}" "${BLACKLIGHT_SHIMMERLED_NUM}"
		sleep 0.5
	done
	
	return 0;
}

function doubleLight_led_flicker_upgrade_success()
{
	while true
	do
		XmGpio sethigh "${DOUBLELIGHT_WHITELED_GROUP}" "${DOUBLELIGHT_WHITELED_NUM}"
		sleep 2
		XmGpio setlow "${DOUBLELIGHT_WHITELED_GROUP}" "${DOUBLELIGHT_WHITELED_NUM}"
		sleep 2
	done
	
	return 0;
}

function doubleLight_led_flicker_upgrade_fail()
{
	while true
	do
		XmGpio sethigh "${DOUBLELIGHT_WHITELED_GROUP}" "${DOUBLELIGHT_WHITELED_NUM}"
		sleep 0.5
		XmGpio setlow "${DOUBLELIGHT_WHITELED_GROUP}" "${DOUBLELIGHT_WHITELED_NUM}"
		sleep 0.5
	done
	
	return 0;
}


ISPLAYLOOP=`cat $script_path/upgrade.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"`

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"`
ISUPGRADESUCCREBOOT=`cat $script_path/upgrade.ini | ./busybox grep "bUpgradeSuccReboot:" | ./busybox grep -v grep | sed 's/bUpgradeSuccReboot://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"`

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
		isupgraded=yes
		if [ "$ISPLAYLOOP" = "yes" ];then
			audio_play_upgrade_success
		fi
	fi
fi

echo "ETH0MAC:$ETH0MAC ETH0MAC4FILE:$ETH0MAC4FILE "
if [ ! -f $script_path/$SD_UPGRADE_FILE ]; then
	echo "cann't found upgrade file"
	./simple_adec_bind_ao -r 8000 -t g711a -i audio/upgradefile_fail.pcm
	exit -1
fi

if [ "$isupgraded" = "no" ];then
	./simple_adec_bind_ao -r 8000 -t g711a -i audio/start_upgrade.pcm
	
	if [ "$ISPLAYLEDFLICKER" = "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
	
	$script_path/upgrader file $script_path/$SD_UPGRADE_FILE
	if [ $? != 0 ]; then
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/upgrade_fail.pcm
		if [ "$ISPLAYLEDFLICKER" = "yes" ];then
			if [ "$DOUBLELEDMODE" = "yes" ];then
				doubleLight_led_flicker_upgrade_fail
			fi

			if [ "$BLACKLEDMODE" = "yes" ];then
				blackLight_led_flicker_upgrade_fail
			fi
		fi
	else
		echo "eth0mac:$ETH0MAC" > $script_path/$ETH0MAC_FILE
		sync
		sleep 5
		./simple_adec_bind_ao -r 8000 -t g711a -i audio/upgrade_ok.pcm
		if [ "$MCUUPGRADE" = "no" -a "$LAMPMCUUPGRADE" = "no" ];then
			if [ "$ISPLAYLOOP" = "yes" ];then
				if [ "$ISPLAYLEDFLICKER" = "yes" ];then
					audio_play_upgrade_success &
				else
					audio_play_upgrade_success
				fi
			fi
		fi
		
		if [ "$ISPLAYLEDFLICKER" = "yes" -a "$ISUPGRADESUCCREBOOT" = "no" ];then
			if [ "$DOUBLELEDMODE" = "yes" ];then
				doubleLight_led_flicker_upgrade_success
			fi

			if [ "$BLACKLEDMODE" = "yes" ];then
				blackLight_led_flicker_upgrade_success
			fi
		fi
		reboot
	fi
fi
