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

NEODEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`        
IMAGEKATALOG=ImageBoot


if [ -f /tmp/.init_reboot ]; then
          rm /tmp/.init_reboot
	      break;
else
	if [ -f `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 $*	  
                   exit 0
            else
                    echo " "
                    break;
            fi
	fi  
	
	LIBDIR=/lib/modules/`uname -r`
	if [ ! -d $LIBDIR ]; then
		LIBDIR=/lib/modules/`ls /lib/modules | tail -n 1`
	fi
	
	if [ `mount | grep /proc | wc -l` -lt 1 ]; then	        	             
        /bin/mount -t proc proc /proc > /dev/null 2>&1
        /bin/mount -t sysfs sysfs /sys > /dev/null 2>&1
        /sbin/depmod -Ae > /dev/null 2>&1                 
	fi 	

	echo "/sbin/mdev" > /proc/sys/kernel/hotplug
	/etc/init.d/mdev > /dev/null 2>&1
	
	sleep 5
	
	if [ ! -e $NEODEVICE$IMAGEKATALOG/.neonextboot  ]; then
        /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neom; sleep 2               
	fi      	
        	               
    UPLOAD=ImagesUpload
	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
                reboot -f
	fi    

	TARGET=Flash
	if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot ]; then
	  TARGET=`cat $NEODEVICE$IMAGEKATALOG/.neonextboot`
	fi
		        
	if [ $TARGET = "Flash" ]; then
		/bin/umount /media/usb > /dev/null 2>&1
		/bin/umount /media/hdd > /dev/null 2>&1		
		ln -sfn /sbin/init.sysvinit /sbin/init > /dev/null 2>&1	        
	else
	
		if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_ok ]; then
	        echo "No Error - Booting image normal..."
		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... "; 
                rm -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_boot_new_image > /dev/null 2>&1 
            else 
                HOSTNAME=`cat /etc/hostname` 
                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. 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                 	sleep 5; reboot -f              
                fi
            fi
		fi 	                                                                                                                              
		
        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
			
			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
                        			
			rm $NEODEVICE$IMAGEKATALOG/$TARGET$NEODEVICE > /dev/null 2>&1
			mkdir $NEODEVICE$IMAGEKATALOG/$TARGET$NEODEVICE > /dev/null 2>&1			
                        rm -f $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh                      
                        cp -f /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh                         
                        chmod 755 `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh 
                                                                                                                    
			if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/sbin/init.sysvinit ]; then                                                                                                                      
                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 ]; then                                    
                exec /usr/sbin/chroot `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET /sbin/init        
			else                                                                  
                /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/error.mvi > /dev/null 2>&1
                sync; ln -sfn /sbin/init.sysvinit /sbin/init
                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
                sleep 15; reboot -f

			fi
			exit 0
		fi
	fi
fi

if [ -f /sbin/init.sysvinit ]; then
		exec /sbin/init.sysvinit $1
fi

exit 0
;;
esac
exit
