#!/bin/sh 
#DESCRIPTION = This script mod. by gutosie
#NeoBoot INIT Version: 6.00
#Scrypt Unix Shell - init for neoboot 

IMAGE=/media/neoboot/ImageBoot
IMAGEBOOT=/media/neoboot/ImageBoot/.neonextboot
NEO=/media/neoboot
NEOBOOT=/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot
MODPROBE=/sbin/modprobe
BOXNAME=`cat /etc/hostname`
FINDBOOT=/media/*/ImageBoot/.neonextboot


if [ -e /tmp/.init_reboot ]; then
          rm /tmp/.init_reboot
	  break;

else        
	if [ -e $FINDBOOT ]  ; then 	    
            CELBOOT=`cat $FINDBOOT`
	    if [ $CELBOOT = "Flash" ]; then 
                   exec /sbin/init.sysvinit $*	  
                   exit 0
            else
                echo " "
                break;
            fi
	fi      

        echo "====================================================> "
        echo " "
        echo "	    -=*[ NEOBOOT - BOOT UP ]*=- "                      	                      
        echo " NEO BOOT INIT Version: 6.00 Update 6.15 16-11-2017   "
        echo "	             q(-_-)p           "   
        echo " "
        echo "====================================================> "
	
	/etc/init.d/sysfs.sh
	/etc/init.d/modutils.sh
	
	echo "NEOBOOT is starting mdev"
        echo " "
	echo "/sbin/mdev" > /proc/sys/kernel/hotplug
	/etc/init.d/mdev > /dev/null 2>&1
	
	mknod /dev/sda b  8 0
	mknod /dev/sda1 b 8 1
	mknod /dev/sda2 b 8 2
	mknod /dev/sdb b  8 16
	mknod /dev/sdb1 b 8 17
	mknod /dev/sdb2 b 8 18
	mknod /dev/sdc b  8 32
	mknod /dev/sdc1 b 8 33
	mknod /dev/sdc2 b 8 34
	mknod /dev/sdd b  8 48
	mknod /dev/sdd1 b 8 49
	mknod /dev/sdd2 b 8 50

	mkdir /dev/input
	mknod /dev/input/event0 c 13 64

	mknod /dev/fb0 c 29 0
	mkdir /dev/fb
	ln -s ../fb0 /dev/fb/0

	sleep 1
	
	if [ -e /etc/videomode ]; then
	    cat /etc/videomode > /proc/stb/video/videomode
	fi
	if [ -e /etc/videomode2 ]; then
	    cat /etc/videomode2 > /proc/stb/video/videomode
	fi

	(
	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 $NEOBOOT/neowait.mvi;

	) &
	
	DEVICES1=`find /dev/sd??`
	for DEVICE in $DEVICES1;
	do
		if [ ! -e /media/neoboot/ImageBoot/.neonextboot  ]; then
     		mount $DEVICE /media/neoboot > /dev/null 2>&1
    	fi                    
   		if [ ! -e /media/neoboot/ImageBoot/.neonextboot  ]; then
       		umount /media/neoboot > /dev/null 2>&1 
    	fi 
	done

	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

	sleep 1
        echo " "
        echo "Search NEOBOOT... "
        #sed=grep
	mnttest="nouuid"
	if [ -e $NEOBOOT/bin/install  ]; then
        	orgimag1=`mount | grep '/sd/!d' | cut -d" " -f1`
		selectinstall=`cat $NEOBOOT/bin/install`
		for item in $orgimag1; do
			selectmnt=`blkid | grep "/$selectinstall/!d" | cut -d":" -f1`
			if [ $selectmnt == $item ]; then
				echo "selectmnt=" $selectmnt
                        	mnttest="uuid"
				mount $selectmnt $NEO > /dev/null 2>&1
			fi
			if [ ! -e $IMAGE  ]; then
				echo "NEOBOOT hasn't been installed on: " $selectmnt
				umount $NEO > /dev/null 2>&1 
			else                                
				echo "NEOBOOT has been installed on: " $selectmnt
				break;
			fi 			
		done
	fi	

	if [ ! -e $NEO  ]; then
		mkdir $NEO > /dev/null 2>&1
	fi

	if [ $mnttest == "nouuid" ]; then
	        echo "NEOBOOT mount by Device"
		DEVICES1=`find /dev/sd??`
		for DEVICE in $DEVICES1;
		do
			if [ -e $NEO  ]; then
				echo "NEOBOOT checking installation on: " $DEVICE
				mount $DEVICE $NEO > /dev/null 2>&1 
			fi
			if [ ! -e $IMAGE  ]; then
				echo "NEOBOOT hasn't been installed on:" $DEVICE
				umount $NEO > /dev/null 2>&1 
			else
				echo "NEOBOOT has been installed on: " $DEVICE
				break;
			fi 
		done
	elif [ $mnttest == "uuid" ]; then	
		echo "NEOBOOT mount by UUID"
	fi	

        #dodatkowe szukanie neoboota
        #mount /dev/sdb1 $NEO > /dev/null 2>&1  # <<< tu mozesz zmienic sdb1 i wpisac Twoje montowanie dysku dla neoboot

        echo "NEOBOOT is loading modules."
        for mod in dvb; do
                  echo "=======================> NeoBoot is loading modules"
              $MODPROBE $mod
        done

	if [ -f $LIBDIR/kernel/fs/autofs4/autofs4.ko ]; then
		/sbin/insmod $LIBDIR/kernel/fs/autofs4/autofs4.ko > /dev/null 2>&1
	fi
		          
        sleep 1

	if [ -e $NEOBOOT/bin/fbclear ]; then
	        $NEOBOOT/bin/fbclear; $NEOBOOT/bin/fbclear; $NEOBOOT/bin/fbclear; $NEOBOOT/bin/fbclear > /dev/null 2>&1
        fi


	if [ -e $NEOBOOT/bin/neobm ]; then
            echo "  Neo-Boot start  boot manager  "
            echo "  Waiting to change image - 20s "
            echo " "
	    chmod 0755 $NEOBOOT/bin/neobm
	    $NEOBOOT/neologo.mvi > /dev/null 2>&1
	    sleep 1
	    $NEOBOOT/bin/neobm > /dev/null 2>&1
	fi
	
	if [ ! -e $IMAGEBOOT ]; then
		  ln -sfn /sbin/init.sysvinit /sbin/init
                  echo "Flash "  > $IMAGEBOOT
                  echo "Used Kernel: Reinstall kernel !"  > /media/neoboot/ImagesUpload/.kernel/used_flash_kernel
		  reboot -f
	fi


	TARGETIMAGE=`cat $IMAGEBOOT`
        if [ $TARGETIMAGE = "BlackHoleVU" ] || [ $TARGETIMAGE = "BlackHoleVU+" ] ; then       	    
            if ! [ -e $NEO/ImagesUpload/.kernel/used_bh_kernel ]; then
                rm -f $IMAGEBOOT
                echo "Flash "  >> $IMAGEBOOT 
                echo "Halt init image - " $TARGETIMAGE 
                echo "Start image Flash...  "  
                $NEOBOOT/bh.mvi > /dev/null 2>&1
                sync; sleep 1
                exec /sbin/init.sysvinit $* 
                exit 0
            else
                 break;
            fi
	fi

        sync
	
	TARGET=Flash
	if [ -f $IMAGEBOOT ]; then
        	TARGET=`cat $IMAGEBOOT`
	fi
		
	if [ $TARGET = "Flash" ]; then
                echo "____________________________________"
                echo "____________________________________"
                echo " "
		echo "NEOBOOT is booting image from " $TARGET
                echo "____________________________________"
                echo "____________________________________"
		/bin/umount $NEO > /dev/null 2>&1
		/bin/umount /media/usb > /dev/null 2>&1
		/bin/umount /media/hdd > /dev/null 2>&1              
                /usr/bin/showiframe /usr/share/bootlogo.mvi > /dev/null 2>&1
                echo "----------------------------------"
	        echo "     BOOT UP IMAGE FROM FLASH     "
                echo "----------------------------------"
	else
		        echo " " 
		        echo "NEOBOOT is booting image" $TARGET " from " $DEVICE
		        echo " " 
		        if [ -d $IMAGE/$TARGET ]; then
			        if [ -f $IMAGE/$TARGET/etc/init.d/udev ]; then
				       sed -ei s!"\$4"!"\$3"!g $IMAGE/$TARGET/etc/init.d/udev
			        fi

			        if [ -f $IMAGE/$TARGET/usr/share/bootlogo.mvi ]; then
				        /usr/bin/showiframe $IMAGE/$TARGET/usr/share/bootlogo.mvi > /dev/null 2>&1
			        fi
                                                			
			        sleep 1

			        /bin/mount -o bind /dev $IMAGE/$TARGET/dev
			        /bin/mount -o bind /proc $IMAGE/$TARGET/proc
			        /bin/mount -o bind /sys $IMAGE/$TARGET/sys
			        rm $IMAGE/$TARGET/media/neoboot > /dev/null 2>&1
			        mkdir $IMAGE/$TARGET/media/neoboot > /dev/null 2>&1
                                orgimag=`mount | sed '/sd/!d' | cut -d" " -f1`
                                rm -f $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh
                                   for item in $orgimag; do
                                       ohdd=`echo  $item | cut -d"/" -f3`
                                       nhdd=`mount | sed "/\$ohdd/!d" | sed q | cut -d" " -f3`
                                       if [ $nhdd == '$NEO' ]; then
                                          echo $nhdd
                                          echo "mkdir "$nhdd  >> $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh
                                          echo "mount "$item $nhdd  >> $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh 
                                       else
                                          echo "umount "$nhdd  >> $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh
                                          echo "mkdir "$nhdd  >> $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh
                                          echo "mount "$item $nhdd  >> $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh 
                                       fi    
                                   done	
                           
                                if [ -f $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh ]; then 
                                    chmod 755 $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh	
                                fi                                                                                                                              
                                echo "----------------------------------------"
	                        echo "   -=*[ BOOT UP IMAGE FROM DISK ]*=-    "
                                echo "----------------------------------------"


			        if [ ! -f /media/neoboot/ImageBoot/$TARGET/media/neoboot/ImageBoot/.neonextboot ]; then
                                    /bin/mount -o bind /media/neoboot /media/neoboot/ImageBoot/$TARGET/media/neoboot
			        fi

			        DEVICES1=`ls /media`
			        for DEVICE in $DEVICES1;
			        do
			                if [ -f /media/$DEVICE/ImageBoot/.neonextboot ]; then
				                /bin/mount -o bind /media/neoboot /media/neoboot/ImageBoot/$TARGET/media/$DEVICE
			        fi
			        done

		                cd /media/neoboot/ImageBoot/$TARGET;

                                if [ -f $IMAGE/$TARGET/sbin/init.sysvinit ]; then                                    
                                    exec /usr/sbin/chroot . /sbin/init.sysvinit $*
                                elif [ -f $IMAGE/$TARGET/sbin/init ]; then                                    
                                    exec /usr/sbin/chroot . /sbin/init $*       
                                else
                                    echo " "
                                    echo "                       NEOBOOT - ERROR !!!                                 "
                                    echo "Nie odnaleziono poprawnie rozpakowanego image "$TARGET" z partycji " $DEVICE
                                    echo " Sprawdz poprawnosc rozpakowanych katalogow w wybranym image.              "
                                    echo "                      Powrot do image Flash...                             "
                                    rm -f $IMAGEBOOT; touch $IMAGEBOOT; echo "Flash "  >> $IMAGEBOOT 
                                    /usr/bin/showiframe $NEOBOOT/error.mvi > /dev/null 2>&1
                                    sync; sleep 10; exec /sbin/init.sysvinit $*

                                fi
			        exit 0

		        fi
	fi
fi

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

exit 0
;;
esac
exit
 