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

lAudioTipLanguage=$1

SD_UPGRADE_FILE=sd_upgrade_all.bin
ETH0MAC_FILE=eth0mac.cfg
isupgraded=no
#ETH0MAC=`sed -n "s/ethaddr:\(.*\).*/\1\n/p" /proc/xm/xminfo`
ETH0MAC=`ifconfig eth0|grep eth0|awk '{print $5}'`

led_flicker_upgrade_processing()
{
	if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
		echo "0" > /sys/class/pwm/pwmchip${WLED_PWM}/export
		echo "10000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/period
		echo "5000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/duty_cycle
		echo "normal" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/polarity
		echo "1" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/enable
	else 
		XmGpio sethigh "${WHITELED_GROUP}" "${WHITELED_NUM}"
	fi
	
	XmGpio sethigh "${SHIMMERLED_GROUP}" "${SHIMMERLED_NUM}"
	return 0
}

function led_flicker_upgrade_success()
{
	if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
		echo "0" > /sys/class/pwm/pwmchip${WLED_PWM}/export
		echo "10000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/period
		echo "5000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/duty_cycle
		echo "normal" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/polarity
	fi
	
	while true
	do
		if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
			echo "1" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/enable
		else 
			XmGpio sethigh "${WHITELED_GROUP}" "${WHITELED_NUM}"
		fi
		XmGpio sethigh "${SHIMMERLED_GROUP}" "${SHIMMERLED_NUM}"
		sleep 2
		if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
			echo "0" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/enable
		else 
			XmGpio setlow "${WHITELED_GROUP}" "${WHITELED_NUM}"
		fi
		XmGpio setlow "${SHIMMERLED_GROUP}" "${SHIMMERLED_NUM}"
		sleep 2
	done
	
	return 0;
}

function led_flicker_upgrade_fail()
{
	if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
		echo "0" > /sys/class/pwm/pwmchip${WLED_PWM}/export
		echo "10000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/period
		echo "5000" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/duty_cycle
		echo "normal" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/polarity
	fi
	
	while true
	do
		if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
			echo "1" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/enable
		else 
			XmGpio sethigh "${WHITELED_GROUP}" "${WHITELED_NUM}"
		fi
		XmGpio sethigh "${SHIMMERLED_GROUP}" "${SHIMMERLED_NUM}"
		sleep 0.5
		if [ "${WHITELED_GROUP}" = "255" -o "${WHITELED_NUM}" = "255" ];then
			echo "0" > /sys/class/pwm/pwmchip${WLED_PWM}/pwm0/enable
		else 
			XmGpio setlow "${WHITELED_GROUP}" "${WHITELED_NUM}"
		fi
		XmGpio setlow "${SHIMMERLED_GROUP}" "${SHIMMERLED_NUM}"
		sleep 0.5
	done
	
	return 0;
}

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"`
WHITELED_GROUP=`cat $script_path/upgrade.ini | ./busybox grep "whiteLed_group:" | ./busybox grep -v grep | sed 's/whiteLed_group://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WHITELED_NUM=`cat $script_path/upgrade.ini | ./busybox grep "whiteLed_num:" | ./busybox grep -v grep | sed 's/whiteLed_num://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
SHIMMERLED_GROUP=`cat $script_path/upgrade.ini | ./busybox grep "shimmerLed_group:" | ./busybox grep -v grep | sed 's/shimmerLed_group://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
SHIMMERLED_NUM=`cat $script_path/upgrade.ini | ./busybox grep "shimmerLed_num:" | ./busybox grep -v grep | sed 's/shimmerLed_num://g' | sed 's/\r//g' | sed 's/\n//g' | sed "s/$(echo -e '\015')//g"`
WLED_PWM=`cat $script_path/upgrade.ini | ./busybox grep "WLed_pwm:" | ./busybox grep -v grep | sed 's/WLed_pwm://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
	fi
fi

if [ ! -f $script_path/$SD_UPGRADE_FILE ]; then
	echo "cann't found upgrade file"
	if [ "$ISPLAYLEDFLICKER" = "yes" ];then
		led_flicker_upgrade_fail &
	fi
	if [ $lAudioTipLanguage = "English" ];then
		./playOrRecord -p audio/upgradefile_fail_english.pcm -s 8000 -f 1
	else
		./playOrRecord -p audio/upgradefile_fail.pcm -s 8000 -f 1
	fi
	exit -1
fi

if [ "$isupgraded" = "no" ];then
	if [ "$ISPLAYLEDFLICKER" = "yes" ];then
		led_flicker_upgrade_processing
	fi
	$script_path/upgrader file $script_path/$SD_UPGRADE_FILE
	if [ $? != 0 ]; then
		if [ "$ISPLAYLEDFLICKER" = "yes" ];then
			led_flicker_upgrade_fail &
		fi
		if [ $lAudioTipLanguage = "English" ];then
			./playOrRecord -p audio/upgrade_fail_english.pcm -s 8000 -f 1
		else
			./playOrRecord -p audio/upgrade_fail.pcm -s 8000 -f 1
		fi
	else
		if [ "$ISPLAYLEDFLICKER" = "yes" ];then
			led_flicker_upgrade_success &
		fi
		echo "eth0mac:$ETH0MAC" > $script_path/$ETH0MAC_FILE
		sync
		if [ "$ISUPGRADESUCCREBOOT" = "yes" ];then
			sleep 5
			if [ $lAudioTipLanguage = "English" ];then
				./playOrRecord -p audio/upgrade_ok_english.pcm -s 8000 -f 1
			else
				./playOrRecord -p audio/upgrade_ok_reboot.pcm -s 8000 -f 1
			fi
			reboot
		else
			while true
			do
				sleep 5
				if [ $lAudioTipLanguage = "English" ];then
					./playOrRecord -p audio/upgrade_ok_english.pcm -s 8000 -f 1
				else
					./playOrRecord -p audio/upgrade_ok.pcm -s 8000 -f 1
				fi
			done
		fi
	fi
fi