#!/bin/sh
#script mod by gutosie
#neo init arm 

NEODEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`
INSTALLDEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install`
IMAGEKATALOG=ImageBoot
HOSTNAME=`cat /etc/hostname`

if [ -e /tmp/.init_reboot ]; then
        rm /tmp/.init_reboot > /dev/null 2>&1
        break;

else        
	if [ -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot ]  ; then        	    
                    CELBOOT=`cat $NEODEVICE$IMAGEKATALOG/.neonextboot`
	    if [ $CELBOOT = "Flash" ]; then 
                    exec /sbin/init.sysvinit $1	  
                    exit 0
        else
                break;
        fi
	fi
	
        echo "====================================================> "
        echo " "
        echo "	    -=*[ NEOBOOT - BOOT UP ]*=-     "
        echo "     NEO BOOT INIT Version: gutosie   "
        echo "	             q(-_-)p                "
        echo " "
        echo "====================================================> "              

	LIBDIR=/lib/modules/`uname -r`
	if [ ! -d $LIBDIR ]; then
		    LIBDIR=/lib/modules/`ls /lib/modules | tail -n 1`
	fi
	if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then	
            break;
    else 		
	    echo "Mount local filesystems ..."	
	    if [ `mount | grep /proc | wc -l` -lt 1 ]; then	
	            /bin/mount -a          	             
		        /bin/mount -n -t proc proc /proc > /dev/null 2>&1
		        /bin/mount -n -t sysfs sysfs /sys > /dev/null 2>&1
		        /sbin/depmod -Ae > /dev/null 2>&1                 
	    fi
	fi
   
	if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then 
            break;
    else  	
	        echo "NeoBoot is starting mdev fom FLASH..."
	        echo "/sbin/mdev" > /proc/sys/kernel/hotplug
	        /etc/init.d/mdev > /dev/null 2>&1
	fi

	sleep 5
	
	if [ ! -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`  ]; then
	        mkdir `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1
		    echo "_______________________NeoBoot created location media_______________________"
	fi
	
	if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then 	
	    if [ -e /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh  ]; then
                /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh > /dev/null 2>&1
                echo "_______________________NeoBoot mount by MOUNTPOINT_______________________" 
	    fi	             	
    else
	    mnttest="nolocation"	
	    if [ -e /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install  ]; then
        	    orgimag1=`mount | sed '/sd/!d' | cut -d" " -f1`
		    selectinstall=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install`
		    for item in $orgimag1; do        				 			
                selectmnt=`blkid | find "$selectinstall"  | cut -d" " -f1`                        
			    if [ $selectmnt = $selectinstall ]; then
				        echo "selectmnt is:" $selectmnt
                        mnttest="location"
				        mount $selectmnt `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1		
                        echo "Neoboot location detected." 				
			    fi

			    if [ ! -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`ImageBoot  ]; then
				        echo "NeoBoot hasn't been installed on: " $selectinstall
				        umount `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1 
			    else
				        echo "NEOBOOT has been installed on: " $selectmnt
					    echo "_______________________ NeoBoot mount by LOCATION_______________________ "
				        break;
			    fi 			
		    done                    
	    fi	

	    if [ $mnttest = "location" ] ; then
                break;
	    else
	        echo "NeoBoot mount by Device"
		    DEVICES1=`find /dev/sd??`
		    for DEVICE in $DEVICES1;
		    do
			    if [ -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`  ]; then
				        echo "neoboot checking installation on: " $DEVICE
				        mount $DEVICE `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1 
			    fi
			    if [ ! -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`ImageBoot  ]; then
				        echo "NeoBoot hasn't been installed on: " $DEVICE
				        umount `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1 
			    else
				        echo "NeoBoot has been installed on: " $DEVICE
					    echo "_______________________ NeoBoot mount by DEVICE_______________________"
				        break;
			    fi 
		    done                
        fi
    fi					

	if [ ! -e $NEODEVICE$IMAGEKATALOG/.neonextboot  ]; then
	        if [ -e /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh  ]; then
                    /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh
                    echo "_______________________NeoBoot mount by MOUNTPOINT_______________________" 
	        fi
    fi	
	
	neoLOG=$NEODEVICE$IMAGEKATALOG/initneo.log
    if [ -f $neoLOG ]; then
            rm -f $neoLOG 
		    touch $neoLOG           
    fi
	
        echo "                  >>> START NEXT LOG...            >>> "  >> $neoLOG
        echo "..............-=*[ NEOBOOT - BOOT UP ]*=-.............."  >> $neoLOG
        echo "    ____q(-_-)p____START NEXT Image...____q(-_-)p____  "  >> $neoLOG
        echo "......................................................."  >> $neoLOG  	
        echo "Tuner name: " `cat /etc/hostname` >> $neoLOG
        echo "Boot image: " `cat $NEODEVICE$IMAGEKATALOG/.neonextboot` >> $neoLOG   
        echo "Init image, base kernel: " `cat $NEODEVICE$IMAGEKATALOG/.neonextboot` >> $neoLOG

    UPLOAD=ImagesUpload
	if [ $NEODEVICE = "/media/hdd/" ] && [ $INSTALLDEVICE = "/dev/sda1" ] ; then
	        /bin/mount /dev/sda1 /media/hdd > /dev/null 2>&1
	elif [ $NEODEVICE = "/media/hdd/" ] && [ $INSTALLDEVICE = "/dev/sdb1" ] ; then
	        /bin/mount /dev/sdb1 /media/hdd > /dev/null 2>&1
	fi
	if [ $NEODEVICE = "/media/usb/" ] && [ $INSTALLDEVICE = "/dev/sda1" ] ; then
	        /bin/mount /dev/sda1 /media/usb > /dev/null 2>&1
	elif [ $NEODEVICE = "/media/usb/" ] && [ $INSTALLDEVICE = "/dev/sdb1" ] ; then
	        /bin/mount /dev/sdb1 /media/usb > /dev/null 2>&1
	else
                if [ $NEODEVICE = "/media/hdd/" ]; then
	                /bin/mount /dev/sda1 /media/hdd > /dev/null 2>&1
	        fi     
	fi
	#/bin/mount /dev/sda1 /media/hdd > /dev/null 2>&1
	#/bin/mount /dev/sdb1 /media/usb > /dev/null 2>&1    
	if [ ! -e $NEODEVICE$IMAGEKATALOG ]; then
                ln -sfn /sbin/init.sysvinit /sbin/init
                echo "Flash "  > $NEODEVICE$IMAGEKATALOG/.neonextboot 
                echo "Reinstall kernel -Press number 3"  > $NEODEVICE$UPLOAD/.kernel/used_flash_kernel
                echo " Sending all processes the TERM- KILL signal..." 
		        /bin/umount /media/usb > /dev/null 2>&1
		        /bin/umount /media/hdd > /dev/null 2>&1
		        echo -n "<<<END INIT INFO Rebooting...>>>"
                echo " Sending all processes the TERM- KILL signal..." >> $neoLOG	
                echo "Error mounting, Return to image Flash..."; echo "Error mounting, Return to image Flash..." >> $neoLOG				
		        break;
	fi

	TARGET=Flash
	if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot ]; then
	        TARGET=`cat $NEODEVICE$IMAGEKATALOG/.neonextboot`
	fi                                                                                               

	if [ ! -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/boot/zImage.$HOSTNAME ]; then
                echo "Image launched on a base kernel flash. " >> $neoLOG
                echo "Image launched on a base kernel flash. "  > `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/kernel_flash	

	    echo "NeoBoot is starting mdev fom FLASH..."
	    echo "/sbin/mdev" > /proc/sys/kernel/hotplug
	    /etc/init.d/mdev > /dev/null 2>&1			

	    MODPROBE=/sbin/modprobe

	    echo "Load video and frame buffer modules ..."
            #modprobe dvb

	    echo "NeoBoot is loading modules"
	    for mod in dvb; do
		    $MODPROBE $mod
	    done
	
	    # Hack for loading modules which are not loaded on diffrent kernel images
	    /etc/init.d/modutils.sh
	    #
	    echo -n "Set BOOTLOGO ..."
                         
	    cat /etc/videomode > /proc/stb/video/videomode

    	#we must wait until udev has created the video device
	    (
	    if [ -d /proc/stb ]; then
		    while true; do
			    if [ -e /dev/dvb/adapter0/video0 ]; then
				    break;
			    fi;
		    done;
	    fi;
	    [ -e /etc/dropbear/dropbear_rsa_host_key ] && /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/neowait.mvi;
	    ) &
          
        #/etc/init.d/bootlogo > /dev/null 2>&1
        
        sleep 5
        echo "Driverload end"

	    if [ -f $LIBDIR/kernel/fs/autofs4/autofs4.ko ]; then
		    /sbin/insmod $LIBDIR/kernel/fs/autofs4/autofs4.ko > /dev/null 2>&1
	    fi
		
        /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/fbclear > /dev/null 2>&1
	fi

	HARDWARETYPE=`uname -m` 
	if [ $NEODEVICE = "/media/hdd/" ]; then
	        /bin/mount /dev/sda1 /media/hdd > /dev/null 2>&1
	fi
	if [ $TARGET = "Flash" ]; then
		    echo "NEOBOOT is booting image from " $TARGET
		    /bin/umount /media/usb > /dev/null 2>&1
		    /bin/umount /media/hdd > /dev/null 2>&1				
            if [ $HARDWARETYPE != "armv7l" ]; then
		        /usr/bin/showiframe /usr/share/bootlogo.mvi > /dev/null 2>&1
		    fi    
            echo "----------------------------------"
	        echo "     BOOT UP IMAGE FROM FLASH     "
            echo "----------------------------------";		
            echo "     BOOT UP IMAGE FROM FLASH     " >> $neoLOG
		    ln -sfn /sbin/init.sysvinit /sbin/init		
	else                                                                                                                                             
        if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_ok ]; then
	                echo "No Error - Booting image normal..." 
                    echo "No Error - Booting image normal... " >> $neoLOG
		else 
		    if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_boot_new_image ]; then
                        echo " First start image - The first attempt to launch a new image..." >> $neoLOG
                        echo "First start image - The first attempt to launch a new image... " 
						echo "====================================================> "
                        rm -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_boot_new_image > /dev/null 2>&1 						
		    else 		         
                        if [ ! -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_ok ] ; then                                          
                                if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then                                                         			        
                                        dd if=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$UPLOAD/.kernel/flash-kernel-$HOSTNAME.bin of=/dev/mmcblk0p1
                                elif [ $HOSTNAME  = "vuzero4k" ] ; then                                                         			        
                                        dd if=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$UPLOAD/.kernel/flash-kernel-$HOSTNAME.bin of=/dev/mmcblk0p4
                                elif [ $HOSTNAME  = "vuduo4k" ] ; then                                                         			        
                                       dd if=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$UPLOAD/.kernel/flash-kernel-$HOSTNAME.bin of=/dev/mmcblk0p6                                            
                                fi
                                echo " Wrong kernel. FATAL ERROR - Back to image Flash..." >> $neoLOG
                                echo "Wrong kernel. Press number 3"  > `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$UPLOAD/.kernel/used_flash_kernel
                                ln -sfn /sbin/init.sysvinit /sbin/init
                                echo "Flash "  > `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot                 	
		                        /bin/umount /media/usb > /dev/null 2>&1
		                        /bin/umount /media/hdd > /dev/null 2>&1                            
                                echo -n "<<<END INIT INFO Rebooting...>>>"
		                        break;              
                        fi
		    fi
		fi 

		echo "NeoBoot is booting" $TARGET " from " `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`
		
        if [ -d $NEODEVICE$IMAGEKATALOG/$TARGET ]; then
			if [ -f $NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/udev ]; then
				    sed -ie "s#\$4#\$3#g" $NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/udev
			fi     			
                                  
			if [ $HARDWARETYPE != "armv7l" ]; then
			        if [ -f $NEODEVICE$IMAGEKATALOG/$TARGET/usr/share/bootlogo.mvi ]; then
				            /usr/bin/showiframe $NEODEVICE$IMAGEKATALOG/$TARGET/usr/share/bootlogo.mvi
			        else
				            /usr/bin/showiframe $NEODEVICE$IMAGEKATALOG/$TARGET/usr/share/bootlogo.mvi
			        fi
			fi

			sleep 1
                       			
			/bin/mount -o bind /dev $NEODEVICE$IMAGEKATALOG/$TARGET/dev
			/bin/mount -o bind /proc $NEODEVICE$IMAGEKATALOG/$TARGET/proc
			/bin/mount -o bind /sys $NEODEVICE$IMAGEKATALOG/$TARGET/sys
			if [ ! -f /usr/lib/python2.7 ]; then 
			#if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then 
			            $NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/modutils.sh
			fi
			rm $NEODEVICE$IMAGEKATALOG/$TARGET$NEODEVICE > /dev/null 2>&1
			mkdir $NEODEVICE$IMAGEKATALOG/$TARGET$NEODEVICE > /dev/null 2>&1  
			
	        if [ $HOSTNAME  = "vuultimo4k" ] || [ $HOSTNAME  = "vusolo4k" ] || [ $HOSTNAME  = "vuuno4kse" ] || [ $HOSTNAME  = "vuuno4k" ]; then 
                    break;
            else			
                        orgimag=`mount | sed '/sd/!d' | cut -d" " -f1`
                        rm -f $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh 
                           for item in $orgimag; do     
                                    ohdd=`echo  $item | cut -d"/" -f3`
                                    nhdd=`mount | sed "/\$ohdd/!d" | grep '' | cut -d' ' -f3`                               
                                        echo "Copied mount to run image." $nhdd >> $neoLOG
                                        echo $nhdd
                                        echo "mkdir "$nhdd  >> $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh 
                                        echo "mount "$item $nhdd  >> $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh                                    
                            done
                            chmod 755 `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh                         
                            echo "NeoBOOT created the file enigma2_pre_start.sh " 

                        if [ ! -e $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh ]; then
                                cp -f /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh  #nie powinno sie zdarzyc
                        fi	
			fi		
                                                                       
            echo " [NEOBOOT] Use chroot ... exec /usr/sbin/chroot..."
            cp -f -r /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh $NEODEVICE$IMAGEKATALOG/$TARGET/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh                                                                                              
            #/bin/mount /dev/sda1 `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$TARGET/media/hdd                       
			if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/sbin/init ]; then   
                    echo "----------------------------------------"
	                echo "   -=*[ BOOT UP IMAGE FROM DISK ]*=-    "
                    echo "----------------------------------------" 
                    echo " -=*[ BOOT UP IMAGE FROM DISK ]*=- " >> $neoLOG
                    echo "Boot from " `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` >> $neoLOG 
                    echo "start file - init >" $TARGET  >> $neoLOG	
					echo "====================================================> "
            	    exec /usr/sbin/chroot `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET /sbin/init.sysvinit
			elif [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/sbin/init.sysvinit ]; then  
                    echo " start file - init.sysvinit" >> $neoLOG                       
            	    exec /usr/sbin/chroot `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET /sbin/init  
					echo "====================================================> "
			else			                             
                    echo "                       NEOBOOT - ERROR !!!                                 "
                    echo "Not found correctly unpacked image "$TARGET" z partycji " $DEVICE
                    echo "Check the correctness of unpacked catalogs in the selected one image.      "
                    echo "                      Back to image Flash...                             "                                                                    
                    /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/error.mvi > /dev/null 2>&1
                    echo "Flash "  > `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot
                    echo "Used Kernel: Reinstall kernel !"  > `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$UPLOAD/.kernel/used_flash_kernel
		            /bin/umount /media/usb > /dev/null 2>&1
	                /bin/umount /media/hdd > /dev/null 2>&1
				    echo -n " FATAL ERROR - Back to image Flash...\n<<<END INIT INFO Rebooting...>>>" >> $neoLOG
                    echo -n "<<<END INIT INFO Rebooting...>>>"
				    ln -sfn /sbin/init.sysvinit /sbin/init
		            reboot -d -f

			fi
			exit 0
		fi
	fi
fi

# boot Flash 

if [ -f /sbin/init.sysvinit ]; then
		#echo "Running /sbin/init.sysvinit - boot Flash"
		exec /sbin/init.sysvinit $1
fi

exit 0
;;
esac
exit
