#!/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/1400PM.bin ];then
		if [ -f /var/ftp/pub/target/main/upgrade ];then
			mv /var/ftp/pub/target/main/upgrade /bin
	 echo "changed upgrade"
#			rm /var/ftp/pub/target/main/upgrade
		fi

		if [ -f /var/ftp/pub/target/main/forceupgrade ];then
			 if [ -f /var/ftp/pub/forceupgradeflag ];then
			    rm /var/ftp/pub/target/main/forceupgrade
			    else
			    touch /var/ftp/pub/forceupgradeflag
			    mv /var/ftp/pub/target/main/forceupgrade  /bin
	 echo "changed forceupgrade"
			    chmod 777 /bin/forceupgrade
			    rm -r -f /var/ftp/pub/target
			    /bin/forceupgrade
			    exit 1
			    fi
		fi
		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
		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

		
		fn1=/root/version
		fn2=/var/ftp/pub/target/main/version			
		cat $fn1 | while read LINE1
		do
		device1=${LINE1:0:4}
		cat $fn2 | while read LINE2
		do
		device2=${LINE2:0:4}
		if [ $device1 == $device2 ];	then	
			echo "1400PM.bin  Upgrade  . . ."
			touch /var/ftp/pub/upgradeflag
			rm /root/1400PM.bin
			mv /var/ftp/pub/target/main/*  /root/
			mv /usr/local/apache/oem-cgi-bin/logo3.jpg /var/ftp/pub/target/apache/oem-cgi-bin/logo3.jpg
			mv /usr/local/apache/htdocs/logo.jpg /var/ftp/pub/target/apache/htdocs/logo.jpg
			rm -r -f /usr/local/apache
			mv /var/ftp/pub/target/web/*  /usr/local/			
		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
		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 #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
		touch /var/ftp/pub/upgradeflag
	fi #endif vmlinux.ub

	if [ -f /root/upgrade ];then
		rm /root/upgrade
	fi
	if [ -f /root/forceupgrade ];then
		rm /root/forceupgrade
	fi	
	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/1400PM.bin ];then
			echo "1400PM Upgrade From USB device ..."
			if [ -f /var/ftp/pub/target/main/upgrade ];then
			 mv /var/ftp/pub/target/main/upgrade /bin
			#	rm /var/ftp/pub/target/main/upgrade
			fi			
			if [ -f /var/ftp/pub/target/main/forceupgrade ];then
#		rm /var/ftp/pub/target/main/forceupgrade
			if [ -f /var/ftp/pub/forceupgradeflag ];then
			rm /var/ftp/pub/target/main/forceupgrade
			else
			touch /var/ftp/pub/forceupgradeflag
			mv /var/ftp/pub/target/main/forceupgrade  /bin
			chmod 777 /bin/forceupgrade
			rm -r -f /var/ftp/pub/target
			/bin/forceupgrade
			exit 1
			fi
			fi
			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
		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
			
			fn1=/root/version
			fn2=/var/ftp/pub/target/main/version			
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:4}
			cat $fn2 | while read LINE2
			do
			device2=${LINE2:0:4}
			if [ $device1 == $device2 ];	then	
				echo "1400PM.bin  Upgrade  . . ."
				touch /var/ftp/pub/upgradeflag
				rm /root/1400PM.bin
				mv /var/ftp/pub/target/main/*  /root/
			mv /usr/local/apache/oem-cgi-bin/logo3.jpg /var/ftp/pub/target/apache/oem-cgi-bin/logo3.jpg
			mv /usr/local/apache/htdocs/logo.jpg /var/ftp/pub/target/apache/htdocs/logo.jpg
				rm -r -f /usr/local/apache
				mv /var/ftp/pub/target/web/*  /usr/local/				
			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
			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 #endif load_modules.sh
		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
		
		if [ -f /root/upgrade ];then
			rm /root/upgrade
		fi
		if [ -f /root/forceupgrade ];then
			rm /root/forceupgrade
		fi
		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

