#!/bin/sh

if [ -f /var/ftp/pub/target.tar.bz2 ]; then
	cd /var/ftp/pub
	tar -jxvf  /var/ftp/pub/target.tar.bz2
	echo "Upgrade From FTP ..."
	chmod 777 -R /var/ftp/pub/target

	
	if [ -f /var/ftp/pub/target/version ];then
		  echo ""
	else
		  rm -r -f /var/ftp/pub/*	
		  echo "No find /var/ftp/pub/target/version ..."
		  exit 1
	fi

	fn2=/var/ftp/pub/target/version
	cat $fn2 | while read LINE2
	do
	device2=${LINE2:0:6}
	version2=${LINE2:6:3}
	echo " upgrade version=$device2$version2"
	
	if [ $device2 != 1522EC ];	then		#如果是5200EC则判断版本号		
		if [ -f /root/version ];then
			echo ""
			fn1=/root/version
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:6}
			version1=${LINE1:6:3}
				echo " device version5=$device1$version1"
			
			if [ $device1 != $device2 ];	then
				rm -r -f /var/ftp/pub/*	
				echo "Upgrade file not match the machine type!"	
				exit 1
			fi
			#if [ "$version1" -lt "$version2" ];	then			#	Cannot identify the hex
			if [ $version1 \> $version2 ];	then						# < Identifies as a redirect,So add the escape character \
				rm -r -f /var/ftp/pub/*	
				echo "The version is lower, not upgrade the software!"
				exit 1
			fi
			done
		else
			rm -r -f /var/ftp/pub/*	
			echo "No find /root/version ..."
			exit 1
		fi
	else									#如果是1520EC则忽略版本是否存在判断		
		if [ -f /usr/sbin/version ];then	#1520EC新版版本号放在此处
			echo ""
			fn1=/usr/sbin/version
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:6}
			version1=${LINE1:6:3}
			echo " device version1=$device1$version1"
		
			if [ $device1 != $device2 ];	then
				rm -r -f /var/ftp/pub/*	
				echo "Upgrade file not match the machine type!"	
				exit 1
			fi
			#if [ "$version1" -lt "$version2" ];	then			#	Cannot identify the hex
			if [ $version1 \> $version2 ];	then						# < Identifies as a redirect,So add the escape character \
				rm -r -f /var/ftp/pub/*	
				echo "The version is lower, not upgrade the software!"
				exit 1
			fi
			done
		else
			echo "No find /usr/sbin/version ..."
			echo "This's lower 1522EC, no version, Mandatory upgrade!"
		fi
	fi

	if [ -f /var/ftp/pub/target.tar.bz2 ]; then
		echo ""
	else
		exit 1
	fi	
	
	pkill 6000ec.bin
	if [ -f /var/ftp/pub/target/upgrade ];then
		diff /var/ftp/pub/target/upgrade /usr/sbin/upgrade > /dev/null	# Compare files different then restart the upgrade	2012.12.20	zjnie
		if [ $? -eq 0 ];then
			echo "'upgrade' is new!"
			#if [ -f /var/ftp/pub/flag ];then
			#	echo "upgrade is new!"
		else
				#touch /var/ftp/pub/flag
				echo "'upgrade' Update Now..."
				rm /usr/sbin/upgrade
				mv /var/ftp/pub/target/upgrade	  /usr/sbin/upgrade	
				chmod 777 /usr/sbin/upgrade	
				rm -rf /var/ftp/pub/target
				/usr/sbin/upgrade
				exit 1
		fi
	fi	
	
	if [ -f  /var/ftp/pub/target/version ];then
		echo "version is Upgrade Now ..."
		if [ $device2 != 1522EC ];	then		#如果是5200EC则判断版本号		
			mv /var/ftp/pub/target/version	  /root/version	
		else
			mv /var/ftp/pub/target/version	  /usr/sbin/version	
		fi
	fi
	
	if [ -f /var/ftp/pub/target/fsram ];then
	   echo "fsram is Upgrade Now ..."
		rm /usr/sbin//fsram	
		mv /var/ftp/pub/target/fsram	  /usr/sbin/fsram	
		chmod 777 /usr/sbin/fsram	
		fi	
	if [ -f /var/ftp/pub/target/main/6000ec.bin ];then
	  echo "6000ec is Upgrade Now ..."
		rm /usr/sbin/6000ec.bin	
		mv /var/ftp/pub/target/main/6000ec.bin  /usr/sbin/
		chmod 777 /usr/sbin/6000ec.bin	
		fi
	if [ -f /var/ftp/pub/target/fpga/fpga.rbf ];then
	  echo "fpga is Upgrade Now ..."
		rm /root/fpga.rbf	
		mv /var/ftp/pub/target/fpga/fpga.rbf  /root/
		fi
	if [ -f /var/ftp/pub/target/fpga/fpga1.rbf ];then
	  echo "fpga1 is Upgrade Now ..."
		rm /root/fpga1.rbf	
		mv /var/ftp/pub/target/fpga/fpga1.rbf  /root/
		fi
	if [ -f /var/ftp/pub/target/fpga/fpga2.rbf ];then
	  echo "fpga2 is Upgrade Now ..."
		rm /root/fpga2.rbf	
		mv /var/ftp/pub/target/fpga/fpga2.rbf  /root/
		fi	
 	if [ -f /var/ftp/pub/target/run/run.bin ];then
	  echo "run is Upgrade Now ..."
		rm /root/run/*	
		mv /var/ftp/pub/target/run/*  /root/run/
		chmod 777 /root/run/*
	  fi			
	if [ -f /var/ftp/pub/target/firmware/encoderfirmware.bin ];then
	  echo "firmware is Upgrade Now ..."
		rm /root/encoderfirmware.bin	
		mv /var/ftp/pub/target/firmware/encoderfirmware.bin  /root/
		fi	
	if [ -f /var/ftp/pub/target/gigabit/gigabitnios.bin ];then
	  echo "nios is Upgrade Now ..."
		rm /root/gigabitnios.bin	
		mv /var/ftp/pub/target/gigabit/gigabitnios.bin  /root/
		fi
	if [ -f /var/ftp/pub/target/gigabit/gigabitniosm.bin ];then
	  echo "niosm is Upgrade Now ..."
		rm /root/gigabitniosm.bin	
		mv /var/ftp/pub/target/gigabit/gigabitniosm.bin  /root/
		fi	
	if [ -f /var/ftp/pub/target/firmware/code.bin ];then
	  echo "code is Upgrade Now ..."
		rm /root/code.bin	
		mv /var/ftp/pub/target/firmware/code.bin  /root/
		fi		
	if [ -f /var/ftp/pub/target/web/device.cgi ];then
	  echo "boa cgi is Upgrade Now ..."
		rm /home/httpd/cgi-bin/*	
		mv /var/ftp/pub/target/web/*  /home/httpd/cgi-bin/
		chmod 777 /home/httpd/cgi-bin/*
		rm -rf /usr/local/apache/
	fi	
	# >>> Upgraded the New Apache		2015.08.21  zjnie
	if [ -f /var/ftp/pub/target/libuuid.so.1.0.0 ];then
		echo "libuuid.so is Upgrade Now ..."
		rm /lib/libuuid.so.1.0.0
		mv /var/ftp/pub/target/libuuid.so.1.0.0 /lib/libuuid.so.1.0.0
		chmod 777 /lib/libuuid.so.1.0.0
		rm /usr/lib/libiconv.so
		rm /usr/lib/libiconv.so.0
		mv /var/ftp/pub/target/libiconv.so     /usr/lib/
		mv /var/ftp/pub/target/libiconv.so.0   /usr/lib/
		chmod 777 /usr/lib/libiconv.so  
		chmod 777 /usr/lib/libiconv.so.0 
		ln -sf /lib/libuuid.so.1.0.0 /lib/libuuid.so.1
	fi

	
	if [ -f /var/ftp/pub/target/apache.tar.bz2 ];then
		echo "apache cgi is Upgrade Now ..."
		mv -f /usr/local/apache/htdocs/logo3.jpg /usr/local/logo3.jpg
		rm -rf /usr/local/apache
		tar -jxvf /var/ftp/pub/target/apache.tar.bz2 -C /usr/local/
		mv -f /usr/local/logo3.jpg /usr/local/apache/htdocs/logo3.jpg
		chmod 777 -R /usr/local/apache
		chmod 777 /usr
		rm -rf /home/httpd/cgi-bin/*
	fi
	

	if [ -f /var/ftp/pub/target/rcS ];then
		diff /var/ftp/pub/target/rcS /etc/init.d/rcS > /dev/null	# Compare files different	2012.12.20	zjnie
		if [ $? -eq 0 ];then
			echo "rcS is new!"
		else
			echo "rcS is Upgrade Now ..."
			mv -f /etc/init.d/rcS /etc/init.d/rcS_bak			# Backup 
			mv /var/ftp/pub/target/rcS /etc/init.d/rcS
			chmod 777 /etc/init.d/rcS
		fi
	fi
	# <<< FTP		2015.08.21  zjnie
	if [ -f /var/ftp/pub/target/vsftpd.conf ];then
	  echo "vsftpd.conf is Upgrade Now ..."
		rm /etc/vsftpd.conf	
		mv /var/ftp/pub/target/vsftpd.conf  /etc/
		fi	
	if [ -f /var/ftp/pub/target/snmp/snmpd ];then
	   echo "snmpd is Upgrade Now ..."
		rm /usr/sbin/snmpd	
		mv /var/ftp/pub/target/snmp/snmpd  /usr/sbin/
		chmod 777 /usr/sbin/snmpd	
		fi
	if [ -f /var/ftp/pub/target/run/run.bin ];then
	  echo "run is Upgrade Now ..."
		rm /root/run/*	
		mv /var/ftp/pub/target/run/*  /root/run/
		chmod 777 /root/run/*
	  fi		
	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/*
		mv /var/ftp/pub/target/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
    fi
	if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
		echo "Kernel is Upgrade Now ..."
	      flash_eraseall  /dev/mtd1
		flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
	fi
	echo ""
	echo "     Upgrade Complete."
	echo ""
	
	reboot
	done	
	
	
 elif [ -b /dev/sda ]; then
	if [ -f /mnt/target.tar.bz2 ]; then
		echo "Upgrade From USB device ..."
		upgradelcd "Upgrade From USB device ..."
		tar -jxvf /mnt/target.tar.bz2 -C /var/ftp/pub/
		chmod 777 -R /var/ftp/pub/target

	if [ -f /root/version ];then
		  echo ""
	else
		  rm -r -f /var/ftp/pub/*	
		  echo "No find /root/version "
		  upgradelcd  "Upgrade file not match" " the machine type!"
		  exit 1
	fi
	if [ -f /var/ftp/pub/target/version ];then
		  echo ""
	else
		  rm -r -f /var/ftp/pub/*	
		  echo "No find /var/ftp/pub/target/version"
		  upgradelcd  "Upgrade file not match" " the machine type!"	
		  exit 1
	fi

			fn1=/root/version		#USB升级只能是5200EC，所以不用区分/root/version和/usr/sbin/version
			fn2=/var/ftp/pub/target/version			
			cat $fn1 | while read LINE1
			do
			device1=${LINE1:0:6}
			version1=${LINE1:6:3}
			cat $fn2 | while read LINE2
			do
			device2=${LINE2:0:6}
			version2=${LINE2:6:3}
		
	if [ $device1 == $device2 ];	then	
	#if [ "$version1" -lt "$version2" ];	then			#	Cannot identify the hex
	if [ $version1 \< $version2 ];	then						# < Identifies as a redirect,So add the escape character \
			pkill 6000ec.bin
			
			if [ -f /var/ftp/pub/target/upgrade ];then
				diff /var/ftp/pub/target/upgrade /usr/sbin/upgrade > /dev/null	# Compare files different then restart the upgrade	2012.12.20	zjnie
				if [ $? -eq 0 ];then
				#if [ -f /var/ftp/pub/flag ];then
					echo "'upgrade' is new!"
					upgradelcd "upgrade is new!"
				else
					#touch /var/ftp/pub/flag
					echo "'upgrade' Update Now..."
					rm /usr/sbin/upgrade
					mv /var/ftp/pub/target/upgrade	  /usr/sbin/upgrade	
					chmod 777 /usr/sbin/upgrade	
					rm -rf /var/ftp/pub/target
					/usr/sbin/upgrade
					exit 1
				fi
			fi	
				
	         	mv /var/ftp/pub/target/version	  /root/version	
						if [ -f /var/ftp/pub/target/fsram ];then
						   echo "fsram is Upgrade Now ..."
						   upgradelcd "fsram is Upgrade Now ..."
							rm /usr/sbin//fsram	
							mv /var/ftp/pub/target/fsram	  /usr/sbin/fsram	
							chmod 777 /usr/sbin/fsram	
							fi	
						if [ -f /var/ftp/pub/target/main/6000ec.bin ];then
						  echo "6000ec is Upgrade Now ..."
						   upgradelcd "6000ec is Upgrade Now ..."
							rm /usr/sbin/6000ec.bin	
							mv /var/ftp/pub/target/main/6000ec.bin  /usr/sbin/
							chmod 777 /usr/sbin/6000ec.bin	
							fi
						if [ -f /var/ftp/pub/target/fpga/fpga.rbf ];then
						  echo "fpga is Upgrade Now ..."
						  upgradelcd "fpga is Upgrade Now ..."
							rm /root/fpga.rbf	
							mv /var/ftp/pub/target/fpga/fpga.rbf  /root/
							fi
					  if [ -f /var/ftp/pub/target/fpga/fpga1.rbf ];then
						  echo "fpga1 is Upgrade Now ..."
						  upgradelcd "fpga1 is Upgrade Now ..."
							rm /root/fpga1.rbf	
							mv /var/ftp/pub/target/fpga/fpga1.rbf  /root/
							fi 
					 if [ -f /var/ftp/pub/target/fpga/fpga2.rbf ];then
						  echo "fpga2 is Upgrade Now ..."
						  upgradelcd "fpga2 is Upgrade Now ..."
							rm /root/fpga2.rbf	
							mv /var/ftp/pub/target/fpga/fpga2.rbf  /root/
							fi 	
						if [ -f /var/ftp/pub/target/snmp/snmpd ];then
						  echo "snmpd is Upgrade Now ..."
						  upgradelcd "snmpd is Upgrade Now ..."
							rm /usr/sbin/snmpd	
							mv /var/ftp/pub/target/snmp/snmpd  /usr/sbin/
							chmod 777 /usr/sbin/snmpd	
							fi			
						if [ -f /var/ftp/pub/target/firmware/encoderfirmware.bin ];then
						  echo "firmware is Upgrade Now ..."
						  upgradelcd "firmware is Upgrade Now ..."
							rm /root/encoderfirmware.bin	
							mv /var/ftp/pub/target/firmware/encoderfirmware.bin  /root/
							fi	
						if [ -f /var/ftp/pub/target/gigabit/gigabitnios.bin ];then
						  echo "gigabitnios.bin is Upgrade Now ..."
						  upgradelcd "gigabitnios.bin is Upgrade Now ..."
							rm /root/gigabitnios.bin	
							mv /var/ftp/pub/target/gigabit/gigabitnios.bin  /root/
							fi
						if [ -f /var/ftp/pub/target/gigabit/gigabitniosm.bin ];then
						 echo "gigabitniosm.bin is Upgrade Now ..."
						  upgradelcd "gigabitniosm.bin is Upgrade Now ..."
							rm /root/gigabitniosm.bin	
							mv /var/ftp/pub/target/gigabit/gigabitniosm.bin  /root/
							fi	
						if [ -f /var/ftp/pub/target/firmware/code.bin ];then
						  echo "code is Upgrade Now ..."
							rm /root/code.bin	
							mv /var/ftp/pub/target/firmware/code.bin  /root/
							fi		
						if [ -f /var/ftp/pub/target/web/device.cgi ];then
						  echo "boa cgi is Upgrade Now ..."
						  upgradelcd "cgi is Upgrade Now ..."
							rm /home/httpd/cgi-bin/*	
							mv /var/ftp/pub/target/web/*  /home/httpd/cgi-bin/
							chmod 777 /home/httpd/cgi-bin/*
							rm -rf /usr/local/apache/
						fi
						
						# >>> Upgraded the New Apache		2015.08.21  zjnie
						if [ -f /var/ftp/pub/target/libuuid.so.1.0.0 ];then
						 	echo "libuuid.so is Upgrade Now ..."
							rm /lib/libuuid.so.1.0.0
							mv /var/ftp/pub/target/libuuid.so.1.0.0 /lib/libuuid.so.1.0.0
							chmod 777 /lib/libuuid.so.1.0.0
							ln -sf /lib/libuuid.so.1.0.0 /lib/libuuid.so.1
						fi

	
						if [ -f /var/ftp/pub/target/apache.tar.bz2 ];then
						  echo "apache cgi is Upgrade Now ..."
						  upgradelcd "cgi is Upgrade Now ..."
							mv -f /usr/local/apache/htdocs/logo3.jpg /usr/local/logo3.jpg
							rm -rf /usr/local/apache
							tar -jxvf /var/ftp/pub/target/apache.tar.bz2 -C /usr/local/
							mv -f /usr/local/logo3.jpg /usr/local/apache/htdocs/logo3.jpg
							chmod 777 -R /usr/local/apache
							chmod 777 /usr
							rm -rf /home/httpd/cgi-bin/*
						fi
	

						if [ -f /var/ftp/pub/target/rcS ];then
							diff /var/ftp/pub/target/rcS /etc/init.d/rcS > /dev/null	# Compare files different	2012.12.20	zjnie
							if [ $? -eq 0 ];then
								echo "rcS is new!"
							else
								echo "rcS is Upgrade Now ..."
						 		upgradelcd "rcS is Upgrade Now ..."
								mv -f /etc/init.d/rcS /etc/init.d/rcS_bak			# Backup 
								mv /var/ftp/pub/target/rcS /etc/init.d/rcS
								chmod 777 /etc/init.d/rcS
							fi
						fi
						# <<< FTP		2015.08.21  zjnie
						if [ -f /var/ftp/pub/target/vsftpd.conf ];then
						  echo "vsftpd.conf is Upgrade Now ..."
							rm /etc/vsftpd.conf	
							mv /var/ftp/pub/target/vsftpd.conf  /etc/
							fi
						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/*
							mv /var/ftp/pub/target/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
					    fi
						if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
							echo "Kernel is Upgrade Now ..."
							 upgradelcd "   Kernel  " "is Upgrade Now ..."
							 upgradelcd "   Kernel  " "is Upgrade Now ..."
						      flash_eraseall  /dev/mtd1
							flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
						fi
						echo ""
						echo "     Upgrade Complete."
						upgradelcd "   Upgrade Complete."
						echo ""
	  					reboot
				else			#if [ $version1 \< $version2 ];	then
					rm -r -f /var/ftp/pub/*
					echo "Upgrade file not match the machine type!"
					upgradelcd  "The version is lower " "not upgrade the software!"
				fi
			else 				#if [ $device1 == $device2 ];	then	
				rm -r -f /var/ftp/pub/*	
				upgradelcd  "Upgrade file not match" " the machine type!"
			fi	
		done					#device2=${LINE2:0:6}
		done					#device1=${LINE1:0:6}
	else 						#if [ -f /mnt/target.tar.bz2 ]; then
		echo " USB device is loading Now , Please wait a moment !"	
	fi			
else							#if [ -f /var/ftp/pub/target.tar.bz2 ]; then		elif [ -b /dev/sda ]; then
	echo "No find upgrade file"	
	upgradelcd  "No find upgrade file"	
fi
