#!/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
			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 [ ! -d "/usr/local/apache" ]; then   
		mkdir "/usr/local/apache" 
        fi 
         
        	
	if [ -f /var/ftp/pub/target/main/CB7102.bin ];then
		if [ -f /var/ftp/pub/target/main/forceupgrade ];then
			mv /var/ftp/pub/target/main/forceupgrade  /bin
			chmod 777 /bin/forceupgrade
		fi

		if [ -f /var/ftp/pub/target/main/webupgrade ];then
			mv /var/ftp/pub/target/main/webupgrade /bin
			chmod 777 /bin/webupgrade
		fi

		fn1=/root/version
		fn2=/var/ftp/pub/target/main/version			
		for LINE1 in `cat ${fn1}` 
		do
		device1=${LINE1:0:5}
		version1=${LINE1:5:3}
		for LINE2 in `cat ${fn2}` 
		do
		device2=${LINE2:0:5}
		version2=${LINE2:5:3}
		if [ $device1 == $device2 ];then	
		#	if [ "$version1" -lt "$version2" ];	then
		#		echo "CB7102.bin  Upgrade  . . ."
		#		rm /root/CB7102.bin
		#		mv /var/ftp/pub/target/main/*  /root/	
		#		cp -rf /var/ftp/pub/target/web/apache/*  /usr/local/apache/
		#		mv /var/ftp/pub/target/config/rcS  /etc/init.d/
		#		mv /var/ftp/pub/target/config/libiconv.so.0  /lib/
		#		chmod 777 /lib/libiconv.so.0
		#		chmod 777 /usr/local/apache/cgi-bin/*
		#		chmod 777 /usr/local/apache/htdocs/*
		#	else
		#		echo "This software version is lower.Are you sure to upgrade?"
				echo "Are you sure to upgrade?"	
				echo -n "Input your select[Y or N]:" 
				read choice
		#		echo "we chose $choice "
				case $choice in
				     Y | y)
				       echo "CB7102.bin Upgrade..."
				       rm /root/CB7102.bin
				       mv /var/ftp/pub/target/main/* /root/
					 cp /usr/local/apache/htdocs/.passwd	/root/
					 cp /usr/local/apache/htdocs/img/Logo_1.jpg /root/
					 cp /usr/local/apache/preset /root/ -R
					 rm -rf /home/httpd/cgi-bin/*
				       rm -rf /usr/local/apache/*
				       cp -rf /var/ftp/pub/target/web/apache/*  /usr/local/apache
					 mv /var/ftp/pub/target/snmp/snmpd /usr/sbin/
					 mv /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp/
					 mv /root/.passwd	/usr/local/apache/htdocs/.passwd
					 mv /root/Logo_1.jpg /usr/local/apache/htdocs/img/
					 cp /root/preset /usr/local/apache/ -R
					 rm -rf /root/preset
				       mv /var/ftp/pub/target/config/rcS  /etc/init.d/	
				       mv /var/ftp/pub/target/config/libiconv.so.0  /lib/
					 chmod 777 /usr/sbin/snmpd
				       chmod 777 /lib/libiconv.so.0
				       #chmod 777 /usr/local/apache/cgi-bin/*
				       #chmod 777 /usr/local/apache/htdocs/*
					 #chmod 777 /usr/local/apache/oem-cgi-bin/*
					 chmod 777 -R /usr/local/apache;;
				     N | n)
				       echo "Reboot Now!";;
				     *)
				       echo "Invalid choice!"
				       exit 1;; 				          
				esac			
		#	fi
		else
			rm -r -f /var/ftp/pub/*
			echo "Upgrade file not match the machine type!"
		fi
		done
		done
	else
		rm -r -f /var/ftp/pub/*
	fi #endif CB7102.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 
		
			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/
			fi
			done
			done
		else
			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/
		fi # endif version
	fi #endif load_modules.sh
	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
	fi #endif vmlinux.ub
	reboot

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
				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/CB7102.bin ];then
			echo "CB7102 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
	
			if [ -f /var/ftp/pub/target/main/webupgrade ];then
				mv /var/ftp/pub/target/main/webupgrade /bin
				chmod 777 /bin/webupgrade
			fi

			fn1=/root/version
			fn2=/var/ftp/pub/target/main/version			
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:5}
			version1=${LINE1:5:3}
			cat $fn2 | while read LINE2
			do
			device2=${LINE2:0:5}
			version2=${LINE2:5:3}
			if [ $device1 == $device2 ];then
			#	if [ "$version1" -lt "$version2" ];	then
			#		echo "CB7102.bin  Upgrade  . . ."
			#		touch /var/ftp/pub/upgradeflag
			#		echo "$version1" >upgradeflag
			#		rm /root/CB7102.bin
			#		mv /var/ftp/pub/target/main/*  /root/
			#		cp /usr/local/apache/htdocs/.passwd	/root/
			#		cp -rf /var/ftp/pub/target/web/apache/*  /usr/local/apache
			#		mv /root/.passwd	/usr/local/apache/htdocs/.passwd
			#		mv /var/ftp/pub/target/config/rcS  /etc/init.d/
			#		mv /var/ftp/pub/target/config/libiconv.so.0  /lib/
			#	    chmod 777 /lib/libiconv.so.0
			#		chmod 777 /usr/local/apache/cgi-bin/*
			#		chmod 777 /usr/local/apache/htdocs/*
			#		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
			#		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!"
			fi
			done
			done
		else 
			rm -r -f /var/ftp/pub/*
		fi #endif CB7102.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/
				fi
				done
				done
			else
				echo "Modules is Upgrade Now ..."
				rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*
				/home/display_upgradestatus/lcd
				usleep 100
				mv /var/ftp/pub/target/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
			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

