#! /bin/sh
set -x
need_reboot="false"
killall sboox 
killall mplayer 
killall server 

cd "$(dirname "$0")"

mv ./prcess_info.xml /usr/share/info/prcess_info.xml
#为了与旧版本兼容
if [ `md5sum /usr/bin/upgrade |awk '{print $1}'` == `md5sum ./upgrade |awk '{print $1}'` ]
  then 
    rm ./upgrade
	rm ./huidu-upgrade.sh
  else 
	killall upgrade
	echo "need copy /usr/bin/upgrade!"
	mv ./upgrade /usr/bin/upgrade
	chmod +x /usr/bin/upgrade
#	/usr/bin/upgrade -qws  -nomouse &
	mv ./huidu-upgrade.sh /etc/init.d/huidu-upgrade.sh
	ln -s /etc/init.d/huidu-upgrade.sh /etc/rc5.d/S29huidu-upgrade
	chmod +x /etc/init.d/huidu-upgrade.sh
	sleep 1
fi
ps

if [ `md5sum /usr/bin/mplayer |awk '{print $1}'` == `md5sum ./mplayer |awk '{print $1}'` ]
  then 
    rm ./mplayer
  else 
	ttycmd 23 128
	killall mplayer
	echo "need copy /usr/bin/mplayer!"
	mv ./mplayer /usr/bin/mplayer
	chmod +x /usr/bin/mplayer
fi

if [ `md5sum /usr/bin/fbclear |awk '{print $1}'` == `md5sum ./fbclear |awk '{print $1}'` ]
  then 
    rm ./fbclear
  else 
    ttycmd 23 128
	killall fbclear
	echo "need copy /usr/bin/fbclear!"
	mv ./fbclear /usr/bin/fbclear
	chmod +x /usr/bin/fbclear
fi

if [ `md5sum /usr/bin/write_fpga |awk '{print $1}'` == `md5sum ./write_fpga |awk '{print $1}'` ]
  then 
    rm ./write_fpga
  else 
	echo "need copy /usr/bin/write_fpga!"
	mv ./write_fpga /usr/bin/write_fpga
	chmod +x /usr/bin/write_fpga
fi


if [ `md5sum /boot/fpga.img |awk '{print $1}'` == `md5sum ./normal_fpga.bin |awk '{print $1}'` ]
  then 
	echo ""
#    rm ./fpga.img
  else 
	echo "need copy /bootx/fpga.img!"
	cp ./normal_fpga.bin /boot/fpga.img
	write_fpga
fi

if [ `md5sum /home/root/sboox_data/normal_fpga.bin |awk '{print $1}'` == `md5sum ./normal_fpga.bin |awk '{print $1}'` ]
  then 
    rm ./normal_fpga.bin
  else 
	echo "need copy /bootx/fpga.img!"
	mv ./normal_fpga.bin /home/root/sboox_data/normal_fpga.bin
	write_fpga
fi

if [ `md5sum /home/root/sboox_data/504x_fpga.bin |awk '{print $1}'` == `md5sum ./504x_fpga.bin |awk '{print $1}'` ]
  then 
    rm ./504x_fpga.bin
  else 
	echo "need copy /bootx/504x_fpga.img!"
	mv ./504x_fpga.bin /home/root/sboox_data/504x_fpga.bin
	write_fpga
fi

if [ `md5sum /boot/uImage |awk '{print $1}'` == `md5sum ./uImage |awk '{print $1}'` ]
  then 
    rm ./uImage
  else 
	echo "need copy /bootx/uImage!"
	mv ./uImage /boot
	need_reboot="true"
fi


if [ `md5sum /sbin/mount.ntfs-3g |awk '{print $1}'` == `md5sum ./mount.ntfs-3g |awk '{print $1}'` ]
  then 
    rm ./mount.ntfs-3g
  else 
	echo "need copy /usr/bin/mount.ntfs-3g!"
	mv ./mount.ntfs-3g /sbin/mount.ntfs-3g
    mv ./libntfs-3g.so* /lib/
fi

mv ppp /etc/

if [ `md5sum /usr/sbin/pppd |awk '{print $1}'` == `md5sum pppd |awk '{print $1}'` ]
  then 
    rm ./pppd
  else 
	echo "need copy /usr/sbin/pppd chat!"
	mv ./pppd /usr/sbin/pppd
    mv ./chat /usr/sbin/chat
fi 

tar xvf ppp.tar.gz -C /
#！！！仅添加噪音计版本有用！！！
#if [ `md5sum /etc/inittab |awk '{print $1}'` == `md5sum ./inittab |awk '{print $1}'` ]
#  then 
#    rm ./inittab
#  else 
#	echo "need copy /etc/inittab!"
#	mv ./inittab /etc/inittab
#    need_reboot="true"
#fi

pid=`ps |grep -v grep|grep upgrade|sed -n '1P' | awk '{print $2}'`

if   [ -z $pid ]
	then
	/etc/init.d/huidu-upgrade.sh
fi

ps
touch /usr/share/info/server_upgrade_state
touch /usr/share/info/upgrade_state
echo "0" > /usr/share/info/sboox_state
sleep 1
killall -1 upgrade
rm -r /home/root/sboox.bak 
mkdir /home/root/sboox.bak
mv -f /home/root/sboox/* /home/root/sboox.bak 
mkdir /home/root/sboox
#升级文件拷贝
mv -f ./* /home/root/sboox
chmod +x /home/root/sboox/*.sh

chmod u+x /home/root/sboox/run.sh

rm /usr/share/info/server_upgrade_state
rm /usr/share/info/upgrade_state
echo "1" > /usr/share/info/sboox_state
/home/root/sboox/run.sh
killall -3 upgrade


pid=`ps |grep -v grep|grep upgrade|sed -n '1P' | awk '{print $2}'`

if   [ -z $pid ]
	then
	/etc/init.d/huidu-upgrade.sh
fi

sync

if [ "$need_reboot"x = "true"x ]
	then
	reboot
fi
