#!/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/CB7102.bin ];then
		if [ -f /var/ftp/pub/target/main/upgrade ];then
			mv /var/ftp/pub/target/main/upgrade /bin
			chmod 777 /bin/upgrade
		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}
		cat $fn2 | while read LINE2
		do
		device2=${LINE2:0:5}
		if [ $device1 == $device2 ];	then	
			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 -rf /var/ftp/pub/target/web/apache/*  /usr/local/apache/
			mv /root/.passwd	/usr/local/apache/htdocs/.passwd
			mv /root/Logo_1.jpg /usr/local/apache/htdocs/img/
			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/*
			chmod 777 /usr/local/apache/oem-cgi-bin/*
			touch /var/ftp/pub/upgradeflag
		else
			rm -r -f /var/ftp/pub/*
			echo "Upgrade file not match the machine type!"
			exit 1
		fi
		done
		done	
	fi #endif CB7102.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/CB7102.bin ];then
			if [ -f /var/ftp/pub/target/main/upgrade ];then
				mv /var/ftp/pub/target/main/upgrade /bin
				chmod 777 /bin/upgrade
			fi

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

			echo "7102CB Upgrade From USB device ..."
			fn1=/root/version
			fn2=/var/ftp/pub/target/main/version			
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:5}
			cat $fn2 | while read LINE2
			do
			device2=${LINE2:0:5}
			if [ $device1 == $device2 ];	then	
				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/
				rm -rf /home/httpd/cgi-bin/*
				rm -rf /usr/local/apache/*
				cp -rf /var/ftp/pub/target/web/apache/*  /usr/local/apache/
				mv /root/.passwd	/usr/local/apache/htdocs/.passwd
				mv /root/Logo_1.jpg /usr/local/apache/htdocs/img/
				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/*
				chmod 777 /usr/local/apache/oem-cgi-bin/*
				touch /var/ftp/pub/upgradeflag
			else
				rm -r -f /var/ftp/pub/*
				echo "Upgrade file not match the machine type!"
				exit 1
			fi
			done
			done
		fi #endif CB7102.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

