#!/bin/sh

if [ -d /var/ftp/pub/target ];then 
	 echo "FTP or USB Upgrade Now ..."
	 exit 1
fi
if [ -f /mnt/target.tgz ];then 
	 cp /mnt/target.tgz /var/ftp/pub/
	
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/upgrade ];then
		if [ -f /var/ftp/pub/flag ];then
			echo "upgrade is new!"
			fn1=/var/ftp/pub/target/main/version 
 			fn2=/root/version 
 			cat $fn1 | while read MAIN_VERSION 
  			do 
  		 	cat $fn2 | while read MAIN_VERSION1 
			do
			 if [ ${MAIN_VERSION:0:1} -eq 8 -a ${MAIN_VERSION1:0:1} -eq 8 ];then
				echo "update cimax+ firmware"
			 elif [ ${MAIN_VERSION:0:1} -eq 0 -a ${MAIN_VERSION1:0:1} -eq 0 ]; then
				echo "update cimax firmware"
			 else
				echo "The firmware is different,can't upgrade,quit now!"
				touch /var/ftp/pub/exit
				exit 1
			 fi
			done
			done
			if [ -f /var/ftp/pub/exit ];then
				rm -r -f /var/ftp/pub/*
				exit 1
			fi
		else
###################################################
			 fn1=/var/ftp/pub/target/main/version 
 			 fn2=/root/version 
 			 cat $fn1 | while read MAIN_VERSION 
  		 do 
  		 cat $fn2 | while read MAIN_VERSION1 
  		 echo ${MAIN_VERSION:0:1} 
  		 do 
  		 if [ ${MAIN_VERSION:0:1} -eq 8 -a ${MAIN_VERSION1:0:1} -eq 8 ];then
  		 	 	echo "upgrade cimax+ firmware"
					touch /var/ftp/pub/flag
					cp /var/ftp/pub/target/upgrade  /bin
					chmod 777 /bin/upgrade
					rm -r -f /var/ftp/pub/target
					touch /var/ftp/pub/flag
					exit 1			
				elif [ ${MAIN_VERSION:0:1} -eq 0 -a ${MAIN_VERSION1:0:1} -eq 0 ]; then
					echo "upgrade cimax firmware"
					touch /var/ftp/pub/flag
					cp /var/ftp/pub/target/upgrade  /bin
					chmod 777 /bin/upgrade
					rm -r -f /var/ftp/pub/target  
					touch /var/ftp/pub/flag
					exit 1
				else
					touch /var/ftp/pub/exit
					exit 1
				fi
			done
		done
###################################################		
		if [ -f /var/ftp/pub/flag ];then
			echo "update the upgrade file,and do it now!"
			/bin/upgrade
			exit 1
		else
			echo "The firmware is different,can't upgrade,quit now!"
			rm -r -f /var/ftp/pub/*
			exit 1
		fi
	fi
fi

	fn1=/var/ftp/pub/target/main/version
		cat $fn1 | while read LINE1
		do
		    if [ "$LINE1" -lt 0027 ];then
			    rm /usr/local/apache/cgi-bin/*
				cp /var/ftp/pub/target/init.d/rcS2 /etc/init.d/rcS
				echo "web server is boa"
				else
				echo "web server is apache"
				rm /home/httpd/cgi-bin/*
				cp /var/ftp/pub/target/init.d/rcS1 /etc/init.d/rcS
				chmod 777 /etc/init.d/rcS
		    fi
		done
	fn2=/var/ftp/pub/target/main/version
		cat $fn2 | while read LINE2
		do
		 if [ "$LINE2" -le 0033 ];then
			rm /lib/modules/modules.tar
			rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/* -rf
			echo "umount to flash"
			usleep 100
			
			/bin/umount /lib/modules/2.6.23.17_stm23_A26-pdk7105
		 fi
		done
	if [ -d /var/ftp/pub/target/display_upgradestatus ];  then
           cp -R /var/ftp/pub/target/display_upgradestatus   /home
	fi
	if [ -f /var/ftp/pub/target/test_access ]; then
	   cp /var/ftp/pub/target/test_access /root/
	fi
	if [ -f /var/ftp/pub/target/test_check ];then
	   cp /var/ftp/pub/target/test_check /root/ 
        fi
	if [ -f /var/ftp/pub/target/watchdog ];then
	  cp /var/ftp/pub/target/watchdog /root/
	fi
	if [ -d /usr/local/share/snmp ]; then
	     rm -rf /usr/local/share/snmp/mibs -rf
	fi
	if [ -f /var/ftp/pub/target/mount-usb.sh ];then
		mv /var/ftp/pub/target/mount-usb.sh /sbin/
		chmod 777 /sbin/mount-usb.sh
	fi
	
	if [ -d /var/ftp/pub/target/snmp ]; then
        rm /usr/sbin/snmpd -f
        mv /var/ftp/pub/target/snmp/snmpd /usr/sbin/
		mv /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp/
		echo "copy snmpd"
	fi
	

	if [ -f /var/ftp/pub/target/ntpclient ]; then
	    cp /var/ftp/pub/target/ntpclient /home
	fi
	if [ -f /var/ftp/pub/target/main/3100P.bin ];then
		if [ -f /lib/modules/modules.tar ];then
		tar -xvf /lib/modules/modules.tar -C /lib/modules/2.6.23.17_stm23_A26-pdk7105/
		mv /lib/modules/2.6.23.17_stm23_A26-pdk7105/modules/* /lib/modules/2.6.23.17_stm23_A26-pdk7105/
		cp /lib/modules/version /lib/modules/2.6.23.17_stm23_A26-pdk7105/
		fi
		if [ -f /var/ftp/pub/target/modules/version ];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 LINE5
				do
#				echo $LINE5
				cat $fn4 | while read LINE6
				do
#				echo $LINE6
				if [ "$LINE5" -ne "$LINE6" ];then	
					echo "Modules is Upgrade Now ..."
					pkill /root/test_check
					pkill /root/test_access
					/home/display_upgradestatus/ftp
					rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*
					rm /lib/modules/modules.tar
					rm /lib/modules/version
					usleep 100
					mv /var/ftp/pub/target/modules/* /lib/modules/
				fi
				done
				done
			else
				echo "Modules is Upgrade Now ..."
				pkill /root/test_check
				pkill /root/test_access
				/home/display_upgradestatus/ftp
				rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*
				rm /lib/modules/modules.tar
				rm /lib/modules/version
				usleep 100
				mv /var/ftp/pub/target/modules/* /lib/modules/
			fi # endif version
   	    fi #endif load_modules.sh
			usleep 100
		if [ -f /var/ftp/pub/target/main/3100P.bin ];then
			echo "3100P.bin Upgrade ..."
			pkill /root/test_check
			pkill /root/test_access
			/home/display_upgradestatus/ftp
			rm /root/3100P.bin	
			mv /var/ftp/pub/target/main/*  /root/
			#********
			if [ -d /var/ftp/pub/target/web ] && [ -d /var/ftp/pub/target/web_conf ];then
				chmod 777 /var/ftp/pub/target/web_conf/*
				mv /var/ftp/pub/target/web_conf/boa /bin/
				mv /var/ftp/pub/target/web_conf/* /home/httpd
				mv /var/ftp/pub/target/web/*  /home/httpd/cgi-bin/
			else
				if [ -d /home/httpd ];then
					rm -rf /home/httpd/cgi-bin/*
				#else
					#echo "/home/httpd is undiscovered !"
				fi
					
				if [ -d /usr/local/apache ];then
					rm -rf /usr/local/apache/cgi-bin/*
				else
					echo "The apache is undiscovered !"
					mkdir /usr/local/apache
				fi
				
				if [ -d /var/ftp/pub/target/apache ];then
				    cp /usr/local/apache/oem-cgi-bin/logo4.jpg /var/ftp/pub/target/apache/oem-cgi-bin/
					chmod 777 /var/ftp/pub/target/apache/oem-cgi-bin/*
					rm /usr/local/apache/* -rf
					mv /var/ftp/pub/target/apache/* /usr/local/apache/
					
				fi	
				
				if [ -d /usr/local/apache ];then
					if [ -d /usr/local/preset ];then
						echo "The preset of file have existed !"
					else
						mkdir /usr/local/preset		
					fi
				else
					echo "NO upgrade apache_file Or Please check upgrade apache_file !"
				fi
			
				#if [ -d /var/ftp/pub/target/init.d ];then
				#	if [ -f /var/ftp/pub/target/init.d/rcS ];then
				#		cp /var/ftp/pub/target/init.d/rcS /etc/init.d
				#		chmod 777 /etc/init.d/rcS
				#	fi
			#	fi
			
				if [ -d /usr/local/apache ];then
					if [ -d /usr/local/apache/bin ];then
						chmod 777 /usr/local/apache/bin/*
					fi
				
					if [ -d /usr/local/apache/cgi-bin ];then
						chmod 777 /usr/local/apache/cgi-bin/*
					fi
				fi
			fi
			#********
		fi
		if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
			echo "Kernel is Upgrade Now . . ."
			pkill /root/test_check
			pkill /root/test_access
			/home/display_upgradestatus/ftp
	    	flash_eraseall  /dev/mtd1
			usleep 200
			flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
		fi
		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 [ -d /var/ftp/pub/target/display_upgradestatus ];  then
       		 cp -R /var/ftp/pub/target/display_upgradestatus   /home
		fi
		if [ -f /var/ftp/pub/target/test_access ]; then
		    cp /var/ftp/pub/target/test_access /root/
		fi
		if [ -f /var/ftp/pub/target/test_check ]; then
		    cp /var/ftp/pub/target/test_check /root/
		fi
		if [ -f /var/ftp/pub/target/watchdog ]; then
		   cp /var/ftp/pub/target/watchdog  /root/
		fi
		if [ -d /usr/local/share/snmp ]; then
	     rm -rf /usr/local/share/snmp/mibs -rf
	    fi
		if [ -f /var/ftp/pub/target/mount-usb.sh ];then
			mv /var/ftp/pub/target/mount-usb.sh /sbin/
			chmod 777 /sbin/mount-usb.sh
		fi
		   fn3=/var/ftp/pub/target/main/version
		   fn4=/root/version
		   cat $fn3 | while read LINE3
	   	   do
			cat $fn4 | while read LINE4
			do
			if [ "$LINE3" -le "$LINE4" ];then
				echo "rm target"
				rm /var/ftp/pub/target -rf
				usleep 100
				exit 1
			fi
			done
			if [ "$LINE3" -lt 0034 ];then
			rm /lib/modules/modules.tar
			rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/* -rf
			echo "umount to flash"
			usleep 100
			/bin/umount /lib/modules/2.6.23.17_stm23_A26-pdk7105
			fi
		    	if [ "$LINE3" -lt 0027  ];then
			    rm /usr/local/apache/cgi-bin/* -rf
				cp /var/ftp/pub/target/init.d/rcS2 /etc/init.d/rcS
				echo "web server is boa"
				else
				cp /var/ftp/pub/target/init.d/rcS1 /etc/init.d/rcS
				echo "web server is apache"
		        fi
				chmod 777 /etc/init.d/rcS
		   done
		if [ -f /var/ftp/pub/target/versionc ];then
					cp /var/ftp/pub/target/versionc /bin
					chmod 777 /bin/versionc

					versionc $MAIN_VERSION1
					ret=$?
					echo "Check the CHIP TYPE!" 
					echo $ret
					if [ $ret != 1 ] 
					then
						
						echo "Upgrade file not match the chip type!"
						reboot
					fi

		fi
	if [ -f /var/ftp/pub/target/upgrade ];then
		if [ -f /var/ftp/pub/flag ];then
			echo "upgrade is new!"
		else
			touch /var/ftp/pub/flag
			cp /var/ftp/pub/target/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/ntpclient ]; then
			cp /var/ftp/pub/target/ntpclient /home
		fi
		if [ -d /var/ftp/pub/target/snmp ]; then
        rm /usr/sbin/snmpd -f
        mv /var/ftp/pub/target/snmp/snmpd /usr/sbin/
		mv /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp/
		fi
    
    if [ -f /var/ftp/pub/target/main/3100P.bin ]; then
			echo "3100P Upgrade From USB device ..."
			pkill /root/test_check
			pkill /root/test_access
			/home/display_upgradestatus/usb_progress_bar0
			rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/* -rf
			rm /lib/modules/version
			
			mv /var/ftp/pub/target/modules/* /lib/modules/
			fn1=/root/version
			fn2=/var/ftp/pub/target/main/version
			
			cat $fn2 | while read LINE2
			do
			echo $LINE2
			cat $fn1 | while read LINE1
			do
			echo $LINE1
			if [ "$LINE1" -lt "$LINE2" ];
			then	
			if [ -d /var/ftp/pub/target/apache ];then
				cp /var/ftp/pub/target/init.d/rcS1 /etc/init.d/rcS
			else
				cp /var/ftp/pub/target/init.d/rcS2 /etc/init.d/rcS
			fi
			chmod 777 /etc/init.d/rcS
			rm -rf /usr/local/apache/cgi-bin/* 
			rm -rf /home/httpd/cgi-bin/*	
			if [ -f /var/ftp/pub/target/main/3100P.bin ];then
				echo "3100P.bin  Upgrade  . . ."
				pkill /root/test_check
				pkill /root/test_access	
				/home/display_upgradestatus/usb_progress_bar1
				rm /root/3100P.bin
				mv /var/ftp/pub/target/main/*  /root/
				#********
				if [ -d /var/ftp/pub/target/web && -d /var/ftp/pub/target/web_conf ];then
					chmod 777 /var/ftp/pub/target/web_conf/*
					mv /var/ftp/pub/target/web_conf/boa /bin/
					mv /var/ftp/pub/target/web_conf/* /home/httpd
					mv /var/ftp/pub/target/web/*  /home/httpd/cgi-bin/
				else
					if [ -d /home/httpd ];then
						rm -rf /home/httpd/cgi-bin/*
					else
						echo "/home/httpd is undiscovered !"
					fi
						if [ -d /usr/local/apache ];then
						rm -rf /usr/local/apache/cgi-bin/*
						else
						echo "The apache is undiscovered !"
						mkdir /usr/local/apache
						fi
					if [ -d /var/ftp/pub/target/apache ];then
				        cp /usr/local/apache/oem-cgi-bin/logo4.jpg /var/ftp/pub/target/apache/oem-cgi-bin/
					    chmod 777 /var/ftp/pub/target/apache/oem-cgi-bin/*
						rm /usr/local/apache/*  -rf
						mv /var/ftp/pub/target/apache/* /usr/local/apache/
						
					fi
					if [ -d /usr/local/apache ];then
						if [ -d /usr/local/preset ];then
							echo "The preset of file have existed !"
						else
							mkdir /usr/local/preset		
						fi
					else
						echo "NO upgrade apache_file Or Please check upgrade apache_file !"
					fi
			
				#	if [ -d /var/ftp/pub/target/init.d ];then
				#		if [ -f /var/ftp/pub/target/init.d/rcS ];then
				#			cp /var/ftp/pub/target/init.d/rcS /etc/init.d
				#			chmod 777 /etc/init.d/rcS
				#		fi
				#	fi
			
					if [ -d /usr/local/apache ];then
						if [ -d /usr/local/apache/bin ];then
							chmod 777 /usr/local/apache/bin/*
						fi
				
						if [ -d /usr/local/apache/cgi-bin ];then
							chmod 777 /usr/local/apache/cgi-bin/*
						fi
					fi
				fi
				#********				
			fi
			if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
				echo "Kernel is Upgrade Now ..."
				pkill /root/test_check
				pkill /root/test_access
				/home/display_upgradestatus/usb_progress_bar2
	     		 	flash_eraseall  /dev/mtd1
			 	usleep 200
			 	flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
			fi
					/home/display_upgradestatus/usb_upgrade_success
					sleep 2
	     		reboot
			else
					pkill /root/test_check
					pkill /root/test_access
					/home/display_upgradestatus/usb_wrong_file
					sleep 1
					/home/display_upgradestatus/usb_upgrade_fail
					sleep 1
					rm -r -f /var/ftp/pub/*
			fi
			done
			done
             else 													#endif 3100P.bin
									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

