
#!/bin/sh

TOP_DIR=$PWD
FS_DIR="/mnt/usb/"
VERSION_DIR="/xwmt/version/"

#sd_update_xm800_v1.0.0.tar.gz
UPDATE_PREFIX="sd_update_XM1001_tx_"
UPDATE_PREFIX_TX="sd_update_XM1001_tx_"
UPDATE_PREFIX_RX="sd_update_XM1001_rx_"
TX_DIR="/xwmt/xm800_tx"
RX_DIR="/xwmt/xm800_rx"

echo "waiting xwmt update version file,will sleep 2 second!"

#sleep 2

echo "******************************************************"
echo  "welcome to use sd card update function"

if [ -f $TX_DIR ];then
	echo "update for tx ..."
	tmp=`find $FS_DIR -name *.tar.gz | grep $UPDATE_PREFIX_TX`
elif [ -f $RX_DIR ];then
	echo "update for rx ..."
	tmp=`find $FS_DIR -name *.tar.gz | grep $UPDATE_PREFIX_RX`
else
	echo "update unknow ..."
	tmp=`find $FS_DIR -name *.tar.gz | grep $UPDATE_PREFIX`
fi 

##tmp=`find $FS_DIR -name *.tar.gz | grep $UPDATE_PREFIX`
if [ x"$tmp" = x ]; then
	echo "can not find the update file,stop it"
	exit 0
else
	echo "update file is exist,start"
fi

UPDATE_DIR=`echo ${tmp%/*}`
#echo "$UPDATE_DIR"
#UPDATE_DIR=`echo ${UPDATE_DIR#*.}`
echo "update dirction : $UPDATE_DIR"
UPDATE_FILE=`echo ${tmp##*/}`
echo "update file : $UPDATE_FILE"
cd $UPDATE_DIR

UPDATE_VERSION=`ls $UPDATE_FILE | cut -d"_" -f 5- | cut -d"." -f 1-3`
echo "update version : $UPDATE_VERSION"
if [ ! $UPDATE_VERSION ]; then
	echo "version is null"
	exit
else
	echo "update version : $UPDATE_VERSION"
fi

if [ ! -d $VERSION_DIR ];then
	mkdir -p $VERSION_DIR
	echo "===mkdir $VERSION_DIR ===="
fi

OLD_VERSION=`ls $VERSION_DIR`
echo "old version : $OLD_VERSION"

if [ "$UPDATE_VERSION" != "$OLD_VERSION" ];then
	echo "device was old version system,default allow upgrade"
	echo "==========================ready to update form sd card!!"
#	rm -rf $VERSION_DIR/*
#	killall -15 xm800_*
	#通知正在升级
	mkfifo /tmp/daemon_update
	sleep 1
	#tar -zxvf $UPDATE_DIR/$UPDATE_FILE -C ./xwmt
#	rm -rf /xwmt/xm800_*
	tar -zxvf ./$UPDATE_FILE -C /xwmt
	
	echo "==========================update sucess!!"
#	rm -rf $VERSION_DIR/*
#	touch $VERSION_DIR/$UPDATE_VERSION
	
	cp -rf /xwmt/wifi/sbin/*   /sbin/
	cp -rf /xwmt/wifi/hw3.0/*  /lib/firmware/ath6k/AR6004/hw3.0/
#	cp -rf /xwmt/bdata_haoyiyuan.bin /lib/firmware/ath6k/AR6004/hw3.0/bdata.bin
	sleep 1
	reboot -f
else
	echo "product type not match.upgrade process gave up!!"
	exit
fi

