#!/bin/sh
#####################################################################
#history
#1. ver1
#2. 2015/8/24 ver 02 
#3. 2016/1/6  ver 03 
#4. 2017/1/6  ver 04  add soc_system_cofdm.rbf config_fpga.sh
#5. 2017/4/21 ver 05  Adjust the upgrade order and distribution
#####################################################################
#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/config_fpga.sh ];
				then
					echo -n "upgrading ..config_fpga.."	
					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 $FTP/target/soc_system_cofdm.rbf ];
				then
					echo -n "upgrading ..fpga_cofdm.."	
					cp $FTP/target/soc_system_cofdm.rbf /home/root/sd/soc_system_cofdm.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

				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	
				
				
				echo "system reboot"
				exit 0
			fi	
		else 
			cat $fn1 | while read BOARD_TYPE SCRIPT KERNEL APP FPGA WEB FPGA_COFDM EVENTLOG
			do	
				echo "Board Type:${BOARD_TYPE:11:6}"
				cat $fn2 | while read BOARD_TYPE1 SCRIPT1 KERNEL1 APP1 FPGA1 WEB1 FPGA_COFDM1 EVENTLOG1
				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/board_card.xml ];then
							echo found $((COUNT += 1)) board_card
						fi		
						if [ -f $FTP/target/soc_system.rbf  ];then
							echo found $((COUNT += 1)) fpga
						fi
					fi
				
					if [[ $FPGA_COFDM != $FPGA_COFDM1 ]];
					then
						if [ -f $FTP/target/board_card_cofdm.xml ];then
							echo found $((COUNT += 1)) board_card_cofdm.xml
						fi					
						if [ -f $FTP/target/soc_system_cofdm.rbf  ];then
							echo found $((COUNT += 1)) fpga_cofdm
						fi
					fi					
				
				
					if [[ $KERNEL != $KERNEL1 ]];
					then
						if [ -f $FTP/target/kernel/zImage ];then
							echo found $((COUNT += 1)) zImage
						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/kernel/config_fpga.sh ];then
							echo found $((COUNT += 1)) config_fpga.sh
						fi	
						if [ -f $FTP/target/kernel/socfpga.dtb ];then
							echo found $((COUNT += 1)) socfpga.dtb
						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 [ -f /home/root/eventlog ];
					then
						echo -n "eventlog have ready"
					else
						echo -n "mkdir eventlog"
						mkdir /home/root/eventlog
					 # mv $FTP/target/devif.properties /home/root/eventlog/
					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/board_card.xml ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..board_card.."
						  mv $FTP/target/board_card.xml /home/root/board_card.xml
						fi
						
						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 [[ $FPGA_COFDM != $FPGA_COFDM1 ]];
					then

						if [ -f $FTP/target/board_card_cofdm.xml ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..board_card_cofdm.."
						  mv $FTP/target/board_card_cofdm.xml /home/root/board_card_cofdm.xml
						fi	
						
						if [ -f $FTP/target/soc_system_cofdm.rbf ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..fpga_cofdm.."	
							cp $FTP/target/soc_system_cofdm.rbf /home/root/sd/soc_system_cofdm.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/u-boot.img ];
						then
							echo -n "upgrading $((NOW++))  of $COUNT..uboot.."	
							dd if=$FTP/target/kernel/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/kernel/config_fpga.sh ];
						then
							echo -n "upgrading $((NOW++)) of $COUNT..config_fpga.sh.."
							cp $FTP/target/kernel/config_fpga.sh /home/root/sd/config_fpga.sh
							if [ $? == 0 ]
							then
								chmod 777 /home/root/sd/config_fpga.sh
								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 /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							
						
						if [ -f /etc/rc5.d/S90rund~ ];
						then
							echo -n "delete ..S90rund~.."	
							rm /etc/rc5.d/S90rund~
							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
					################################################################################
					
					#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	
					
					
					
					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"
					exit 0

				done
			break #	modify main ver				
			done
		fi

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



