#!/bin/sh

echo "Check the upgrade file, please wait ..."
if [ -f /var/3800D.tgz ]; then
	cd /var
	tar -xf  /var/3800D.tgz
	chmod 777 -R /var/target
	
	if [ -f /var/target/version ]; then
		fn1=/root/version
		fn2=/var/target/version
		cat $fn1 | while read MAIN_VERSION PBIAPP FPGA KERNEL MODULES
		do	
			cat $fn2 | while read MAIN_VERSION1 PBIAPP1 FPGA1 KERNEL1 MODULES1
			do
				if [ ${MAIN_VERSION:13:4} != ${MAIN_VERSION1:13:4} ]
				then
					echo "Upgrade file not match the machine type!"
					echo " "
					rm -r -f /var/*
					exit 1
					reboot
				else
					echo Upgrade software from ${MAIN_VERSION:13:8} to ${MAIN_VERSION1:13:8}, please wait...
				fi
				
				if [ -f /var/target/versionc ];
				then
					echo "versionc is exist!" 
					cp /var/target/versionc /bin
					chmod 777 /bin/versionc

					versionc $MAIN_VERSION1
					ret=$?
					echo "Check the CHIP TYPE!" 
					echo $ret
					if [ $ret == -1 ] 
					then
						
						echo "Upgrade file not match the chip type!"
						echo "Remove the kernel!"
						rm /var/target/kernel/vmlinux.ub 
						touch /var/flag
					fi
				else
					echo "versionc is not exist!" 
					echo "Upgrade file not match the chip type!"
					echo "Remove the kernel!"
					rm /var/target/kernel/vmlinux.ub 
					touch /var/flag	

				fi
				
				if [ -f /var/target/upgrade ];then
					if [ -f /var/flag ];then
						echo "upgrade is new!"
					else
						touch /var/flag
						cp /var/target/upgrade  /bin
						chmod 777 /bin/upgrade
						rm -r -f /var/target
						echo "Upgrade upgrade file OK"
						/bin/upgrade
						exit 1
					fi
				fi
				
				#if [ $PBIAPP != $PBIAPP1 ]
				#then
					if [ -f /var/target/main/5200p.bin ]
					then
						rm /root/5200p.bin	
						mv /var/target/main/5200p.bin  /root/
						echo "Upgrade Main Part OK"
					else
						echo "5200p.bin no exsit"
					fi
				#fi
				
			#	if [ $FPGA != $FPGA1 ]
			#	then
					if [ -f /var/target/fpga/fpga.rbf ]
					then
						rm /root/fpga.rbf	
						mv /var/target/fpga/fpga.rbf /root/
						echo "Upgrade FPGA OK"
					else
						echo "fpga.rbf no exsit"
					fi
			#	fi
											
				if [ $KERNEL != $KERNEL1 ]
				then
					if [ -f /var/target/kernel/vmlinux.ub ]
					then
						echo "Upgrade Kernel Now ..."
						flash_eraseall  /dev/mtd1
						flashcp /var/target/kernel/vmlinux.ub  /dev/mtd1
						echo "Upgrade Kernel OK"
					else
						echo "Kernel no exsit"
					fi
				fi
				
				if [ $MODULES != $MODULES1 ]
				then
					if [ -f /var/target/modules/load_modules.sh ]
					then
						echo "Upgrade Modules Now ..."
						rm /root/modules/*
						usleep 100
						mv /var/target/modules/* /root/modules/

						echo "Upgrade Modules OK"
					else
						echo "Modules no exsit"
					fi
				fi
				
				rm /root/version
				mv /var/target/version /root/
				
				rm -r -f /var/*		
				reboot		
			done
		done
		
	else
		echo "The unit has not version file!"
		echo " "
		mv /var/target/version /root/
		
		rm -r -f /var/*
		exit 1
	fi

else
		echo "Can't find upgrade file"	
fi
