#!/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=`sed -n "s/ethaddr:\(.*\).*/\1\n/p" /proc/xm/xminfo`

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"
	./playOrRecord -p 0 audio/upgradefile_fail.pcm
	exit -1
fi

if [ "$isupgraded" = "no" ];then
	$script_path/upgrader file $script_path/$SD_UPGRADE_FILE
	if [ $? != 0 ]; then
		./playOrRecord -p 0 audio/upgrade_fail.pcm
	else
		echo "eth0mac:$ETH0MAC" > $script_path/$ETH0MAC_FILE
		sync
		sleep 5
		./playOrRecord -p 0 audio/upgrade_ok.pcm
		reboot
	fi
fi