#!/bin/sh
#####################################################################
#history
#1. ver1
#2. 2015/8/24 ver 02 
#3. 2016/1/6 ver 03 
#4. 2016/3/21 ver 04 
#5. add eventlog
#6. dtbļ
#7. 2018/7/5 main_verҲΪжЩûа汾ſƵģ
#8. 2018/10/31 sync˳ʱδ
#####################################################################
#ERROR CODE 
#  0   success
#  1   failed to unzip file
#  2   file dis-match with the device
#  3   can't found update file
#  4   no version file in update packet
#  5   upgrade failure
#  6   upgrade nothing
#####################################################################
FTP=/var/ftp/pub

COUNT=0
NOW=1
ERR=0

####################################################################
#search update file
####################################################################
if [ -f $FTP/target.tgz ]; then
	if [ -f $FTP/target/version ]; then
		fn1=/home/root/version
		fn2=$FTP/target/version
		
		#force upgrade
		if [[ $1 == 1 ]];
		then	
			if [ -f $FTP/target/go.bin ];
			then
				echo -n "upgrading ..app.."
				mv $FTP/target/go.bin /home/root/go.bin
				if [ $? == 0 ]
				then
					chmod 777 /home/root/go.bin
					echo "done"
				else 
					echo "error"
					ERR=1
				fi

				if [ -f $FTP/target/soc_system.rbf ];
				then
					echo -n "upgrading ..fpga.."	
					cp $FTP/target/soc_system.rbf /home/root/sd/soc_system.rbf
					if [ $? == 0 ]
					then	
						sync
						echo "done"
					else 
						echo "error"
						ERR=1
					fi		
				fi
				if [ -f $FTP/target/pages.tgz ];
				then
					echo -n "upgrading ..web.."
					rm -rf /home/root/http/*
					cp $FTP/target/pages.tgz /home/root/http/
					sync
					echo "done"	
				fi
				if [ $ERR == 1 ];
				then
					echo "upgrade failure."	
					exit 5	
				else 		
					mv $FTP/target/version /home/root/version
					if [ $? == 0 ]
					then	
						sync
						echo "update version file....done"
					else 
						echo "update version file....error"
						rm -rf /var/ftp/pub/*
						exit 5
					fi
				fi

				echo "system reboot"
				exit 0
			fi	
		else 
			cat $fn1 | while read BOARD_TYPE SCRIPT KERNEL APP FPGA WEB EVENTLOG MAIN_VER
			do	
				echo "Board Type:${BOARD_TYPE:11:5 }"
				cat $fn2 | while read BOARD_TYPE1 SCRIPT1 KERNEL1 APP1 FPGA1 WEB1 EVENTLOG1 MAIN_VER1
				do
					###count update file number###
					if [ $APP != $APP1 ];
					then
						if [ -f $FTP/target/go.bin ];then
							echo found $((COUNT += 1)) app
						fi
					fi

					if [ $FPGA != $FPGA1 ];
					then
						if [ -f $FTP/target/soc_system.rbf  ];then
							echo found $((COUNT += 1)) fpga
						fi
					fi
				
					if [ $KERNEL != $KERNEL1 ]
					then
						if [ -f $FTP/target/kernel/zImage ];then
							echo found $((COUNT += 1)) zImage
						fi
						if [ -f $FTP/target/kernel/socfpga.dtb ];then
							echo found $((COUNT += 1)) dtb
						fi
						if [ -f $FTP/target/kernel/u-boot.img ];then
							echo found $((COUNT += 1)) uboot
						fi
						if [ -f $FTP/target/kernel/preloader-mkpimage.bin ];then
							echo found $((COUNT += 1)) preloader
						fi
						if [ -f $FTP/target/config_fpga.sh ];then
							echo found $((COUNT += 1)) config_fpga.sh
						fi

					fi
					if [ $WEB != $WEB1 ]
					then
						if [ -f $FTP/target/pages.tgz ];then
							echo found $((COUNT += 1))  web
						fi
					fi	
					if [ $EVENTLOG != $EVENTLOG1 ]
					then
						if [ -f $FTP/target/eventlog/devif.properties ];then
							echo found $((COUNT += 1))  eventlog devif.properties
						fi
						if [ -f $FTP/target/eventlog/eventaction.properties ];then
							echo found $((COUNT += 1))  eventlog eventaction.properties
						fi
						if [ -f $FTP/target/eventlog/functype.properties ];then
							echo found $((COUNT += 1))  eventlog functype.properties
						fi
					fi	
					if [ $MAIN_VER != $MAIN_VER1 ];
					then
						echo found $((COUNT += 1)) main_ver
					fi
					################################################################################
					#update main app
					if [ $APP != $APP1 ];
					then
						if [ -f $FTP/target/go.bin ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..app.."
							mv $FTP/target/go.bin /home/root/go.bin
							if [ $? == 0 ]
							then
								chmod 777 /home/root/go.bin
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
						fi
					fi
					################################################################################
					if [ $FPGA != $FPGA1 ];
					then
						if [ -f $FTP/target/soc_system.rbf ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..fpga.."	
							cp $FTP/target/soc_system.rbf /home/root/sd/soc_system.rbf
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi		
						fi
					fi
					################################################################################
					if [ $KERNEL != $KERNEL1 ]
					then
						if [ -f $FTP/target/kernel/zImage ]
						then
							echo -n "upgrading $((NOW++))  of $COUNT..image.."
							cp $FTP/target/kernel/zImage /home/root/sd/zImage
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi			
						fi

						
						if [ -f $FTP/target/kernel/socfpga.dtb ];
						then
							echo -n "upgrading $((NOW++))  of $COUNT..dtb.."
							cp $FTP/target/kernel/socfpga.dtb /home/root/sd/socfpga.dtb
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
						fi
					
					
						if [ -f $FTP/target/kernel/u-boot.img ];
						then
							echo -n "upgrading $((NOW++))  of $COUNT..uboot.."	
							dd if=$FTP/u-boot.img of=/dev/mmcblk0p3 bs=64k seek=4
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
						fi		
					
						if [ -f $FTP/target/kernel/preloader-mkpimage.bin ];
						then
							echo -n "upgrading $((NOW++))  of $COUNT..preloader.."	
							dd if=$FTP/target/kernel/preloader-mkpimage.bin of=/dev/mmcblk0p3 bs=64k seek=0
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
						fi		

						if [ -f $FTP/target/config_fpga.sh ];
						then
							echo -n "upgrading $((NOW++))  of $COUNT..config_fpga.sh.."	
							cp $FTP/target/config_fpga.sh /home/root/sd/config_fpga.sh
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
						fi
						if [ -f /home/root/sd/u-boot.scr ];
						then
							echo "found  u-boot.scr and modify u-boot.scr_bk"
							mv /home/root/sd/u-boot.scr /home/root/sd/u-boot.scr_bk
							if [ $? == 0 ]
							then	
								sync
								echo "done"
							else 
								echo "error"
								ERR=1
							fi
							
						fi
					fi

					################################################################################
				
					if [ $WEB != $WEB1 ];
					then
					
						if [ -f $FTP/target/pages.tgz ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..web.."
							rm -rf /home/root/http/*
							cp $FTP/target/pages.tgz /home/root/http/
							sync
							echo "done"	
						fi		
					fi
					
					################################################################################
				
					if [ $EVENTLOG != $EVENTLOG1 ];
					then
						if [ -f $FTP/target/eventlog/devif.properties ];
						then
							rm -rf /home/root/eventlog/devif.properties
							echo -n "upgrading $((NOW++)) of $COUNT..eventlog devif.."
							cp $FTP/target/eventlog/devif.properties /home/root/eventlog/devif.properties
							sync
							echo "done"
						fi
						if [ -f $FTP/target/eventlog/eventaction.properties ];
						then
							rm -rf /home/root/eventlog/eventaction.properties
							echo -n "upgrading $((NOW++)) of $COUNT..eventlog eventaction.."
							cp $FTP/target/eventlog/eventaction.properties /home/root/eventlog/eventaction.properties
							sync
							echo "done"
						fi
						if [ -f $FTP/target/eventlog/functype.properties ];
						then
							rm -rf /home/root/eventlog/functype.properties
							echo -n "upgrading $((NOW++)) of $COUNT..eventlog functype.."
							cp $FTP/target/eventlog/functype.properties /home/root/eventlog/functype.properties
							sync
							echo "done"	
						fi
							
					fi
					################################################################################
					echo -n "upgrading $((NOW++))  of $COUNT..main_ver.. from $MAIN_VER to $MAIN_VER1 "
					sync
					#update board_card.xml
					if [ -f $FTP/target/board_card.xml ];
					then
						echo -n "upgrading ..board_card.xml.."
						rm -rf /home/root/board_card.xml
						cp $FTP/target/board_card.xml /home/root/board_card.xml
						chmod 777 /home/root/board_card.xml
						sync
						echo "done"	
					fi	
					
					#update rcs
					if [ -f $FTP/target/S90rund ];
					then
						echo -n "upgrading ..rcS S90rund.."
						rm -rf /etc/rc5.d/S90rund 
						cp $FTP/target/S90rund /etc/rc5.d/S90rund 
						chmod 777 /etc/rc5.d/S90rund 
						sync
						echo "done"	
					fi	
					
						#update board_card_t2mi.xml
					if [ -f $FTP/target/board_card_t2mi.xml ];
					then
						echo -n "upgrading ..board_card_t2mi.xml.."
						rm -rf /home/root/board_card_t2mi.xml
						cp $FTP/target/board_card_t2mi.xml /home/root/board_card_t2mi.xml
						chmod 777 /home/root/board_card_t2mi.xml
						sync
						echo "done"	
					fi	
					
						#update soc_system_t2mi.rbf
					if [ -f $FTP/target/soc_system_t2mi.rbf ];
					then
						echo -n "upgrading ..soc_system_t2mi.rbf.."
						rm -rf /home/root/soc_system_t2mi.rbf
						cp $FTP/target/soc_system_t2mi.rbf /home/root/sd/soc_system_t2mi.rbf
						chmod 777 /home/root/sd/soc_system_t2mi.rbf
						sync
						echo "done"	
					fi		
					
					################################################################################

					if [ $ERR == 1 ];
					then
						echo "upgrade failure."	
						exit 5	
					else 	


						if [[ $COUNT == 0 ]];
						then
							echo "nothing to update"
							exit 6
						else 
							mv $FTP/target/version /home/root/version
							if [ $? == 0 ]
							then	
								sync
								echo "update version file....done"
							else 
								echo "update version file....error"
								rm -rf /var/ftp/pub/*
								exit 5
							fi
						fi	
					fi
					################################################################################

					echo "system reboot"
					sync
					exit 0

				done
			done
		fi

		
	else
		echo "The unit has not version file!"
		exit 4
	fi
else
		echo "Can not found the upgrade file."
		exit 3
fi



