#!/bin/sh

if [ -d /var/ftp/pub/target ];then 
	 echo "FTP or USB Upgrade Now ..."
	 exit 1
fi

if [ -f /var/ftp/pub/target.tgz ]; then   
	cd /var/ftp/pub
	tar -xvf  /var/ftp/pub/target.tgz	
	chmod 777 -R /var/ftp/pub/target

		
	    
	if [ -f /var/ftp/pub/target/main/upgrade ];then
		
		if [ -f /var/ftp/pub/flag ];then
			rm -r -f /var/ftp/pub/target/main/upgrade
			echo "upgrade is new!"
		else
			touch /var/ftp/pub/flag
			mv /var/ftp/pub/target/main/upgrade  /bin
			chmod 777 /bin/upgrade
			rm -r -f /var/ftp/pub/target
			echo "upgrade is old!"
			/bin/upgrade
			exit 1
		fi
	fi
	if [ -f /var/ftp/pub/target/main/rcS ];then
			echo "upgrade is rcS!"
		   rm /etc/init.d/rcS
		   mv /var/ftp/pub/target/main/rcS  /etc/init.d
	       chmod 777 /etc/init.d/rcS
	    fi
	    
	if [ -f /var/ftp/pub/target/main/forceupgrade ];then
			echo "upgrade is forceupgrade!"
		   rm /bin/forceupgrade
		   mv /var/ftp/pub/target/main/forceupgrade  /bin
	       chmod 777 /bin/forceupgrade
	    fi		

	    
	if [ -f /var/ftp/pub/target/main/1400PM.bin ];then
		if [ -f /var/ftp/pub/target/main/forceupgrade ];then
			mv /var/ftp/pub/target/main/forceupgrade  /bin
			chmod 777 /bin/forceupgrade
		fi
		
		fn1=/root/version
		fn2=/var/ftp/pub/target/main/version			
		cat $fn1 | while read LINE1
		do
		device1=${LINE1:0:4}
		version1=${LINE1:5:3}
		cat $fn2 | while read LINE2
		do
		device2=${LINE2:0:4}
		version2=${LINE2:5:3}
		if [ $device1 == $device2 ];	then	
			if [ "$version1" -lt "$version2" ];	then
				if [ -f /var/ftp/pub/target/main/snmpd ];then
					touch /var/ftp/pub/upgradeflag
					mv /var/ftp/pub/target/main/snmpd  /bin
					chmod 777 /bin/snmpd	
				fi
				echo "1400PM.bin  Upgrade  . . ."
				touch /var/ftp/pub/upgradeflag
				rm /root/1400PM.bin
				mv /var/ftp/pub/target/main/*  /root/
				rm -r -f /usr/local/apache
				mv /var/ftp/pub/target/web/*  /usr/local/				
				
				usleep 100
				if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
					echo "Kernel is Upgrade Now . . ."
			  		flash_eraseall  /dev/mtd1
					usleep 200
					flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
					touch /var/ftp/pub/upgradeflag
				fi #endif vmlinux.ub
			else
				echo "The firmware version is lower, not upgrade the firmware!"
			fi
		else
			rm -r -f /var/ftp/pub/*
			echo "Upgrade file not match the machine type!"
			exit 1
		fi
		done
		done	
	fi #endif 1400PM.bin
	usleep 100
	if [ -f /var/ftp/pub/target/modules/load_modules.sh ];then
		if [ -f /lib/modules/2.6.23.17_stm23_A26-pdk7105/version ];then		
			fn1=/lib/modules/2.6.23.17_stm23_A26-pdk7105/version
			fn2=/var/ftp/pub/target/modules/version			
			cat $fn1 | while read LINE1
			do
			cat $fn2 | while read LINE2
			do
			if [ "$LINE1" -lt "$LINE2" ];then	
				echo "Modules is Upgrade Now ..."
				rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*
				usleep 100
				mv /var/ftp/pub/target/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
				touch /var/ftp/pub/upgradeflag
			fi
			done
			done
		fi # endif version
	fi #endif load_modules.sh
	if [ -f /var/ftp/pub/upgradeflag ];then
		reboot
	else
		rm -r -f /var/ftp/pub/*
	fi

elif [ -b /dev/sda1 ]; then
	if [ -f /mnt/target.tgz ]; then
		tar -xvf /mnt/target.tgz -C /var/ftp/pub/
		chmod 777 -R /var/ftp/pub/target
		
		if [ -f /var/ftp/pub/target/main/upgrade ];then
			if [ -f /var/ftp/pub/flag ];then
				rm -r -f /var/ftp/pub/target/main/upgrade
				echo "upgrade is new!"
			else
				touch /var/ftp/pub/flag
				mv /var/ftp/pub/target/main/upgrade  /bin
				chmod 777 /bin/upgrade
				rm -r -f /var/ftp/pub/target
				/bin/upgrade
				exit 1
			fi
		fi

		if [ -f /var/ftp/pub/target/main/rcS ];then
			echo "upgrade is rcS!"
		   rm /etc/init.d/rcS
		   mv /var/ftp/pub/target/main/rcS  /etc/init.d
	       chmod 777 /etc/init.d/rcS
	    fi
	    
	if [ -f /var/ftp/pub/target/main/forceupgrade ];then
			echo "upgrade is forceupgrade!"
		   rm /bin/forceupgrade
		   mv /var/ftp/pub/target/main/forceupgrade  /bin
	       chmod 777 /bin/forceupgrade
	    fi		

		if [ -f /var/ftp/pub/target/main/1400PM.bin ];then
			echo "1400PM Upgrade From USB device ..."
			if [ -f /var/ftp/pub/target/main/forceupgrade ];then
				mv /var/ftp/pub/target/main/forceupgrade  /bin
				chmod 777 /bin/forceupgrade
			fi
			
			fn1=/root/version
			fn2=/var/ftp/pub/target/main/version			
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:4}
			version1=${LINE1:5:3}
			cat $fn2 | while read LINE2
			do
			device2=${LINE2:0:4}
			version2=${LINE2:5:3}
			if [ $device1 == $device2 ];	then	
				if [ "$version1" -lt "$version2" ];	then
					if [ -f /var/ftp/pub/target/main/snmpd ];then
						touch /var/ftp/pub/upgradeflag
						mv /var/ftp/pub/target/main/snmpd  /bin
						chmod 777 /bin/snmpd						
					fi
					echo "1400PM.bin  Upgrade  . . ."
					touch /var/ftp/pub/upgradeflag
					rm /root/1400PM.bin
					mv /var/ftp/pub/target/main/*  /root/
					rm -r -f /usr/local/apache
					mv /var/ftp/pub/target/web/*  /usr/local/					
					
					usleep 100
					if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
						echo "Kernel is Upgrade Now . . ."
				  		flash_eraseall  /dev/mtd1
						usleep 200
						flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
						touch /var/ftp/pub/upgradeflag
					fi #endif vmlinux.ub
				else
					echo "The firmware version is lower, not upgrade the firmware!"					
				fi
			else
				rm -r -f /var/ftp/pub/*
				echo "Upgrade file not match the machine type!"
				exit 1
			fi
			done
			done
		fi #endif 1400PM.bin
		if [ -f /var/ftp/pub/target/modules/load_modules.sh ]; then
			if [ -f /lib/modules/2.6.23.17_stm23_A26-pdk7105/version ];then 
				fn3=/lib/modules/2.6.23.17_stm23_A26-pdk7105/version
				fn4=/var/ftp/pub/target/modules/version
		
				cat $fn3 | while read LINE3
				do
				cat $fn4 | while read LINE4
				do
				if [ "$LINE3" -lt "$LINE4" ];then	
					echo "Modules is Upgrade Now ..."
					rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*
					usleep 100
					mv /var/ftp/pub/target/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
					touch /var/ftp/pub/upgradeflag
				fi
				done
				done
			fi # endif version
		fi #endif load_modules.sh
		if [ -f /var/ftp/pub/upgradeflag ];then
			reboot
		else
			rm -r -f /var/ftp/pub/*
		fi
	else #endif target.tgz
		echo " USB device is loading Now , Please wait a moment !"	
	fi
else
	echo "No upgrade file Or Please check upgrade file !"	
fi

