#!/bin/sh

if [ -d /var/ftp/pub/target ];then 
	 echo "FTP or USB is upgrading ..."
	 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 [ -d /var/ftp/pub/target/lib ]; then
			cp /var/ftp/pub/target/lib/* /lib/
			chmod 777 /lib/*
	fi
	
	if [ -f /var/ftp/pub/target/main/2200P.bin ];then
		if [ -d /var/ftp/pub/target/display_upgradestatus ];  then
			cp -R /var/ftp/pub/target/display_upgradestatus   /home
		fi
	fi
	
	if [ -f /var/ftp/pub/target/main/2200P.bin ];then
		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
				
			fi
		fi
	else
		rm -r -f /var/ftp/pub/target
		rm /var/ftp/pub/target.tgz
		/home/display_upgradestatus/lcd 3
		echo "the upgrade file is not matched! please upgrade the right file"
#		usleep 200
        sleep 2
		exit 1
	fi
	
	if [ -f /var/ftp/pub/target/ntpclient ]; then
	    cp /var/ftp/pub/target/ntpclient /home
	fi
	if [ -f /var/ftp/pub/target/main/2200P.bin ];then	
		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 LINE5
				do
#				echo $LINE5
				cat $fn4 | while read LINE6
				do
#				echo $LINE6
				if [ "$LINE5" -ne "$LINE6" ];then	
					echo "Modules is upgrading ..."
#					echo "lcd 0"
					/home/display_upgradestatus/lcd 0    
#                    echo "rm module"
					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 upgrading ..."
#				echo "lcd 00"
				/home/display_upgradestatus/lcd 0
#                echo "rm module"
				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/main/2200P.bin ];then
			echo "App is upgrading ..."
			/home/display_upgradestatus/lcd_1 0
			rm /root/2200P.bin	
			mv /var/ftp/pub/target/main/*  /root/
			#boa and apache
			#echo "boa and apache"
			#if [ -d /var/ftp/pub/target/web && -d /var/ftp/pub/target/web_conf ];then	
			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
				#echo "apache"
				if [ -d /usr/local/apache ];then
					echo "The apache is discovered !"
					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					
					if [ -d /usr/local/apache/cgi-bin ];then
						mv /var/ftp/pub/target/apache/cgi-bin/* /usr/local/apache/cgi-bin/						
					else
						mv /var/ftp/pub/target/apache/* /usr/local/apache/
					fi	
                    
                    if [ -d /usr/local/apache/oem-cgi-bin ];then
						mv /var/ftp/pub/target/apache/oem-cgi-bin/*.cgi /usr/local/apache/oem-cgi-bin/						
					else
                        mkdir /usr/local/apache/oem-cgi-bin
						mv /var/ftp/pub/target/apache/oem-cgi-bin/* /usr/local/apache/oem-cgi-bin/
					fi	
                    
					if [ -d /usr/local/apache/htdocs ];then
												 		if [ -f /var/ftp/pub/target/apache/htdocs/.oem-passwd ];then
                                mv /var/ftp/pub/target/apache/htdocs/.oem-passwd /usr/local/apache/htdocs/
                            fi
                            
                            if [ -f /var/ftp/pub/target/apache/htdocs/.passwd ];then
                                mv /var/ftp/pub/target/apache/htdocs/.passwd /usr/local/apache/htdocs/
                            fi
                            
                            if [ -d /var/ftp/pub/target/apache/htdocs/css ];then
                            		 if [ -d /usr/local/apache/htdocs/css ];then
                            		 		mv /var/ftp/pub/target/apache/htdocs/css/* /usr/local/apache/htdocs/css/
                            		 else
                            		 		mkdir /usr/local/apache/htdocs/css
                            		 		mv /var/ftp/pub/target/apache/htdocs/css/* /usr/local/apache/htdocs/css/
                            		 fi                               
                            fi
                            
                            if [ -d /var/ftp/pub/target/apache/htdocs/js ];then
                            		 if [ -d /usr/local/apache/htdocs/js ];then
                            		 		mv /var/ftp/pub/target/apache/htdocs/js/* /usr/local/apache/htdocs/js/
                            		 else
                            		 		mkdir /usr/local/apache/htdocs/js
                            		 		mv /var/ftp/pub/target/apache/htdocs/js/* /usr/local/apache/htdocs/js/
                            		 fi                               
                            fi
                            mv /var/ftp/pub/target/apache/htdocs/*.jpg /usr/local/apache/htdocs/
														mv /var/ftp/pub/target/apache/htdocs/*.html /usr/local/apache/htdocs/

					fi					
					if [ -d /usr/local/apache/conf ];then
						mv /var/ftp/pub/target/apache/conf/* /usr/local/apache/conf/
					fi
				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 /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
                    
                    if [ -d /usr/local/apache/oem-cgi-bin ];then
						chmod 777 /usr/local/apache/oem-cgi-bin/*
					fi
				fi
			fi
			#end boa and apache
			
			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 /var/ftp/pub/target/snmp ];then
					if [ -f /var/ftp/pub/target/snmp/snmpd ];then
						cp /var/ftp/pub/target/snmp/snmpd /usr/sbin
						chmod 777 /usr/sbin/snmpd
						
						if [ -d /usr/local/share/snmp ];then
							cp /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp
							chmod 777 /usr/local/share/snmp/snmpd.conf
						else
							mkdir /usr/local/share/snmp
							cp /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp
							chmod 777 /usr/local/share/snmp/snmpd.conf
						fi
					fi				
					
			fi
			
			
		fi
		if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
                
                if [ -f /root/kernelversion ];then                    
                    LINE8=$(sed -n '1,1p' /root/kernelversion)
                else
                    LINE8=0
                fi
               
               
                if [ -f /var/ftp/pub/target/kernel/kernelversion ];then                    
                    LINE9=$(sed -n '1,1p' /var/ftp/pub/target/kernel/kernelversion)
                else
                     LINE9=1000
                fi
                
                    echo "version"
                    echo $LINE8
                    echo $LINE9
                
                if [ "$LINE8" -ne "$LINE9" ];then
                    if [ -f /var/ftp/pub/target/kernel/mount-usb.sh ];then
                        echo "mount-usb.sh"
                        cp /var/ftp/pub/target/kernel/mount-usb.sh /sbin/
                        chmod 777 /sbin/mount-usb.sh
                    fi 
                    if [ -f /var/ftp/pub/target/kernel/kernelversion ];then
                        echo "kernelversion"
                        cp /var/ftp/pub/target/kernel/kernelversion /root/                        
                    fi  
                
                    echo "Kernel is upgrading ..."
                    /home/display_upgradestatus/lcd_2 0
                        flash_eraseall  /dev/mtd1
                    usleep 200
                    flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
                fi                
		fi
		/home/display_upgradestatus/lcd 2
	#	usleep 100
        sleep 2
		reboot
       else
		 rm -r -f /var/ftp/pub/*
       fi
 #elif [ -b /dev/sda1 ]; then
 elif [ -b /dev/sd*1 ]; 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/2200P.bin ];then
			if [ -d /var/ftp/pub/target/display_upgradestatus ];  then
			cp -R /var/ftp/pub/target/display_upgradestatus   /home
			fi
		fi
		if [ -d /var/ftp/pub/target/lib ]; then
		cp /var/ftp/pub/target/lib/* /lib/
		chmod 777 /lib/*
	fi
	if [ -f /var/ftp/pub/target/main/2200P.bin ];then
		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
	else
		rm -r -f /var/ftp/pub/target
		rm /var/ftp/pub/target.tgz
		/home/display_upgradestatus/lcd 3
		echo "the upgrade file is not matched! please upgrade the right file"
		sleep 2
		exit 1
	fi
		if [ -f /var/ftp/pub/target/ntpclient ]; then
			cp /var/ftp/pub/target/ntpclient /home
		fi
            if [ -f /var/ftp/pub/target/main/2200P.bin ]; then
			echo "Upgrade From USB device ..."
			
	
			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 [ -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 LINE5
					do
					cat $fn4 | while read LINE6
					do
					if [ "$LINE5" -ne "$LINE6" ];then	
						echo "Modules is upgrading ..."
#						echo "lcd 1"
						/home/display_upgradestatus/lcd 1
#                        echo "rm module"
						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 upgrading ..."
#                    echo "rm module"
					rm /lib/modules/2.6.23.17_stm23_A26-pdk7105/*                   
#					echo "lcd 11"
					/home/display_upgradestatus/lcd 1
					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/target/main/2200P.bin ];then
				echo "App is upgrading  ..."	
				/home/display_upgradestatus/lcd_1 1
				rm /root/2200P.bin
				mv /var/ftp/pub/target/main/*  /root/
				#boa and apache
				#if [ -d /var/ftp/pub/target/web && -d /var/ftp/pub/target/web_conf ];then
				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 /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
						if [ -d /usr/local/apache/cgi-bin ];then
							mv /var/ftp/pub/target/apache/cgi-bin/* /usr/local/apache/cgi-bin/
						else
							mv /var/ftp/pub/target/apache/* /usr/local/apache/
						fi
                        if [ -d /usr/local/apache/oem-cgi-bin ];then
                            mv /var/ftp/pub/target/apache/oem-cgi-bin/*.cgi /usr/local/apache/oem-cgi-bin/						
                        else
                            mkdir /usr/local/apache/oem-cgi-bin
                            mv /var/ftp/pub/target/apache/oem-cgi-bin/* /usr/local/apache/oem-cgi-bin/
                        fi	
						if [ -d /usr/local/apache/htdocs ];then
                            if [ -f /var/ftp/pub/target/apache/htdocs/.oem-passwd ];then
                                mv /var/ftp/pub/target/apache/htdocs/.oem-passwd /usr/local/apache/htdocs/
                            fi
                            
                            if [ -f /var/ftp/pub/target/apache/htdocs/.passwd ];then
                                mv /var/ftp/pub/target/apache/htdocs/.passwd /usr/local/apache/htdocs/
                            fi
                            
                            if [ -d /var/ftp/pub/target/apache/htdocs/css ];then
                            		 if [ -d /usr/local/apache/htdocs/css ];then
                            		 		mv /var/ftp/pub/target/apache/htdocs/css/* /usr/local/apache/htdocs/css/
                            		 else
                            		 		mkdir /usr/local/apache/htdocs/css
                            		 		mv /var/ftp/pub/target/apache/htdocs/css/* /usr/local/apache/htdocs/css/
                            		 fi                               
                            fi
                            
                            if [ -d /var/ftp/pub/target/apache/htdocs/js ];then
                            		 if [ -d /usr/local/apache/htdocs/js ];then
                            		 		mv /var/ftp/pub/target/apache/htdocs/js/* /usr/local/apache/htdocs/js/
                            		 else
                            		 		mkdir /usr/local/apache/htdocs/js
                            		 		mv /var/ftp/pub/target/apache/htdocs/js/* /usr/local/apache/htdocs/js/
                            		 fi                               
                            fi
                            mv /var/ftp/pub/target/apache/htdocs/*.jpg /usr/local/apache/htdocs/
														mv /var/ftp/pub/target/apache/htdocs/*.html /usr/local/apache/htdocs/
						fi
						if [ -d /usr/local/apache/conf ];then
							mv /var/ftp/pub/target/apache/conf/* /usr/local/apache/conf/
						fi
					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 /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
                        if [ -d /usr/local/apache/oem-cgi-bin ];then
							chmod 777 /usr/local/apache/oem-cgi-bin/*
						fi
					fi
				fi
				#end boa and apache
				
				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 /var/ftp/pub/target/snmp ];then
						if [ -f /var/ftp/pub/target/snmp/snmpd ];then
							cp /var/ftp/pub/target/snmp/snmpd /usr/sbin
							chmod 777 /usr/sbin/snmpd
							
							if [ -d /usr/local/share/snmp ];then
								cp /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp
								chmod 777 /usr/local/share/snmp/snmpd.conf
							else
								mkdir /usr/local/share/snmp
								cp /var/ftp/pub/target/snmp/snmpd.conf /usr/local/share/snmp
								chmod 777 /usr/local/share/snmp/snmpd.conf
							fi
						fi				
						
				fi
			fi
			if [ -f /var/ftp/pub/target/kernel/vmlinux.ub ];then
                
                if [ -f /root/kernelversion ];then
                  LINE8=$(sed -n '1,1p' /root/kernelversion)
                else
                  LINE8=0 
                fi
                
                
                if [ -f /var/ftp/pub/target/kernel/kernelversion ];then                    
                    LINE9=$(sed -n '1,1p' /var/ftp/pub/target/kernel/kernelversion)
                else
                    LINE9=1000
                fi                
                
                    echo "version"
                    echo $LINE8
                    echo $LINE9
                
                if [ "$LINE8" -ne "$LINE9" ];then
                    if [ -f /var/ftp/pub/target/kernel/mount-usb.sh ];then
                        echo "mount-usb.sh"
                        cp /var/ftp/pub/target/kernel/mount-usb.sh /sbin/
                        chmod 777 /sbin/mount-usb.sh
                    fi 
                    if [ -f /var/ftp/pub/target/kernel/kernelversion ];then
                        echo "kernelversion"
                        cp /var/ftp/pub/target/kernel/kernelversion /root/                        
                    fi  
                
                    echo "Kernel is upgrading ..."
                    /home/display_upgradestatus/lcd_2 1
                        flash_eraseall  /dev/mtd1
                    usleep 200
                    flashcp /var/ftp/pub/target/kernel/vmlinux.ub  /dev/mtd1
                fi
                
			fi
				/home/display_upgradestatus/lcd 2
#				usleep 100
                sleep 2
	     		reboot
			else
				rm -r -f /var/ftp/pub/*
			fi
			done
			done
             else #endif 2200P.bin
			rm -r -f /var/ftp/pub/*
             fi
	 else #endif target.tgz
			echo " USB device is loading Now , Please wait a moment !"	
	 fi	
 else
            /home/display_upgradestatus/lcd 6
            sleep 2
			echo "No upgrade file Or Please check upgrade file !"	
 fi

