if [ $# != 2 ];then
   echo "usage:upload.sh filename serverIP"  
   exit 1
else    
     tftp -l /tmp/SayHi.jf -r $1 -g $2
     if [ $? == 0 ];then	
			 tar -zxvf /tmp/SayHi.jf -C /tmp/
			 mkdir /mnt/Msystem
			 ubiattach /dev/ubi_ctrl -m 5
			 ubimkvol /dev/ubi1 -s 100MiB -N my_ubi_vol
			 mount -t ubifs ubi1_0 /mnt/Msystem
			 echo "start upgrade......"
			 killall -9 syslogd
			 killall -9 klogd
			 killall -9 getty
			 killall monitor
			 killall -9 SayHi
			 killall lldpd
			 killall -9 udhcpc
			 killall eeprom-update
			 /mnt/system/sbin/cordless-ctrl unload
			 sync
			 echo 1 > /proc/sys/vm/drop_caches
			 sleep 3
			 		 
			 mv /tmp/upload.sh /mnt/Msystem/usr/sbin/
			 chmod 777 /usr/sbin/upload.sh
			 mv /tmp/*.txt /mnt/Msystem/mnt/system/
			 mv /tmp/Ring* /mnt/Msystem/mnt/system/
			 mv /tmp/monitor /mnt/Msystem/mnt/system/monitor
			 mv /tmp/sip/SONiX_UVC_TestAP /mnt/Msystem/mnt/sip/SONiX_UVC_TestAP
			 mv /tmp/sip/ftp /mnt/Msystem/mnt/sip/ftp
			 mv /tmp/SayHi /mnt/Msystem/mnt/system/SayHi
			 mv /tmp/setmac /mnt/Msystem/mnt/system/setmac
			 mv /tmp/SN /mnt/Msystem/mnt/system/SN
			 mv /tmp/start.sh /mnt/Msystem/mnt/system/start.sh
			 mv /tmp/modules/*.ko /mnt/Msystem/mnt/system/modules/
			 mv /tmp/wpa_supplicant /mnt/Msystem/mnt/system/wpa_supplicant
			 mv /tmp/css/app_dsp /mnt/Msystem/usr/bin/app_dsp
			 mv /tmp/css/css-loader-ip /mnt/Msystem/lib/firmware/css-loader-ip
			 mv /tmp/udhcpc /mnt/Msystem/mnt/system/udhcpc
			 mv /tmp/iptables /mnt/Msystem/usr/sbin/
			 mkdir /mnt/Msystem/mnt/system/l2tp/
			 mv /tmp/l2tp/* /mnt/Msystem/mnt/system/l2tp/
			 
			 remote=$(md5sum /tmp/boot/uImage|cut -d ' ' -f1)
			 local=$(md5sum /boot/uImage|cut -d ' ' -f1)
			 
			 if [ "$remote" == "$local" ];then
			 	echo uImage md5 same
			 else
			 	echo uImage md5 change
			 	mv /tmp/boot/uImage /mnt/Msystem/boot/uImage
			 fi
			 
			 remote=$(md5sum /tmp/boot/dvf99-evb.dtb|cut -d ' ' -f1)
			 local=$(md5sum /boot/dvf99-evb.dtb|cut -d ' ' -f1)
			 
			 if [ "$remote" == "$local" ];then
			 	echo dvf99-evb.dtb md5 same
			 else
			 	echo dvf99-evb.dtb md5 change
			 	mv /tmp/boot/dvf99-evb.dtb /mnt/Msystem/boot/dvf99-evb.dtb
			 fi
			 
			 
			 rm -rf /tmp/*
			 
			 reboot
			 sleep 3
			 echo "upgrade  succeed and run reboot"
			 echo "upgrade  succeed and run reboot" >> /mnt/sip/flashcp.txt
			 exit 0
	 else 
			 echo tftp get file $1 failed
	         exit 1
	 fi
fi
