#!/bin/sh
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
	  break;
else
    echo "====================================================> "
    echo "	    -=*[ NEOBOOT - BOOT UP ]*=-     "
    echo "====================================================> "
    sleep 2
	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 $*	  
                   exit 0
            else
                echo " "
                break;
            fi
	fi      
    ########################################  DM    
    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 auto 
       /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   

    if [ `uname -r | grep 2.6.12 | wc -l` -lt 1 ]; then
       if [ ! -e /dev/.udev -a -e /etc/init.d/udev ]; then
          /etc/init.d/udev start > /dev/null 2>&1
       fi
    fi
    if [ ! -e /media/squashfs-images  ]; then
        ln -sf /media/hdd/squashfs-images /media/squashfs-images 
    fi
    MODPROBE=/sbin/modprobe
    $MODPROBE /sbin/modprobe fp > /dev/null 2>&1
    $MODPROBE /sbin/modprobe dreambox_rc2 > /dev/null 2>&1
    $MODPROBE /sbin/modprobe dreambox_keyboard > /dev/null 2>&1
    $MODPROBE /sbin/modprobe lcd > /dev/null 2>&1
    $MODPROBE /sbin/modprobe --force dreambox_keyboard > /dev/null 2>&1
    $MODPROBE /sbin/modprobe --force lcd > /dev/null 2>&1
          if [ -f $LIBDIR/extra/head.ko ]; then
             /sbin/insmod $LIBDIR/extra/head.ko > /dev/null 2>&1
          else
             $MODPROBE xilleon > /dev/null 2>&1
             # for dm800
		     $MODPROBE bcm7401 > /dev/null 2>&1
		     $MODPROBE --force bcm7401 > /dev/null 2>&1
             # for dm8000
             $MODPROBE bcm7400 > /dev/null 2>&1
             # for dm500hd and probably dm800se
             $MODPROBE bcm7405 > /dev/null 2>&1
          fi                                 
          while true; do                                                          
              if [ -e /dev/dvb/adapter0/video0 ]; then                        
                     break;                                                  
              fi;                                                             
          done;                                                                  
             cat /proc/stb/avs/0/colorformat > /proc/stb/avs/0/colorformat
             [ -e /etc/dropbear/dropbear_rsa_host_key ] && /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/neostart.mvi
         
       if [ -f $LIBDIR/kernel/fs/autofs4/autofs4.ko ]; then
          /sbin/insmod $LIBDIR/kernel/fs/autofs4/autofs4.ko > /dev/null 2>&1
       fi
       #######################  DM END
	echo "NeoBoot is starting mdev and mout HDD USB ..."	
	sleep 5
	mnttest="nouuid"
	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 | sed "/$selectinstall/!d" | cut -d":" -f1`
			if [ $selectmnt == $item ]; then
				echo "selectmnt is:" $selectmnt
                        	mnttest="uuid"
				mount $selectmnt `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: " $selectmnt
				umount `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location` > /dev/null 2>&1 
			else
				echo "NeoBoot has been installed on: " $selectmnt
				break;
			fi 			
		done
	fi	
	if [ ! -e `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG  ]; then
            mkdir -p /media/hdd > /dev/null 2>&1 
            mkdir -p /media/usb > /dev/null 2>&1
	fi
	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
	fi
	if [ $mnttest == "nouuid" ] ; then
	        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
				break;
			fi 
		done
	elif [ $mnttest == "uuid" ]; then	
		echo "NeoBoot mount by UUID"
	fi
	if [ ! -e $NEODEVICE$IMAGEKATALOG/.neonextboot  ]; then
	    if [ -e /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh  ]; then
                /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh
                echo "___________________neoMountpoint_______________________ " 
                sleep 1
	    fi                

	fi
	if [ $HOSTNAME  = "et5x00" ] ; then
            break;
	else
	    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
	fi
	# Hack for loading modules which are not loaded on diffrent kernel images
	/etc/init.d/modutils.sh
	#
	echo "Set BOOTLOGO ..."
        sleep 1
	cat /etc/videomode > /proc/stb/video/videomode
	#we must wait until udev has created the video device
	(
	[ -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 [ -e /tmp/init_reboot ]; then
		  break;
	fi      	
        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
                echo "Error mounting, Return to image Flash..."
                ln -sfn /sbin/init.sysvinit /sbin/init
                echo " Sending all processes the TERM- KILL signal..."
                sleep 2
                break;
	fi
	TARGET=Flash
	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/sdb1" ] ; then
	            /bin/mount /dev/sdb1 /media/usb > /dev/null 2>&1
		elif [ $NEODEVICE = "/media/usb/" ] && [ $INSTALLDEVICE = "/dev/sda1" ] ; then
	            /bin/mount /dev/sda1 /media/usb > /dev/null 2>&1	        
	fi
	#/bin/mount /dev/sda1 /media/hdd > /dev/null 2>&1
	#/bin/mount /dev/sdb1 /media/usb > /dev/null 2>&1
	
	if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/.neonextboot ]; then
	  TARGET=`cat $NEODEVICE$IMAGEKATALOG/.neonextboot`
	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
		/usr/bin/showiframe /usr/share/bootlogo.mvi > /dev/null 2>&1
		ln -sfn /sbin/init.sysvinit /sbin/init; 
                /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh
                echo "----------------------------------"
	        echo "     BOOT UP IMAGE FROM FLASH     "
                echo "----------------------------------"		
	else
            echo "----------------------------------";
		    #ln -sfn /sbin/init.sysvinit /sbin/init
		    
        ######### test boot new imaga>>>>
	if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/.control_ok ]; then
                    echo " "
	                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 " "
                        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
                                /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/error.mvi > /dev/null 2>&1
                                echo " "
                                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 " "

		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 [ -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

			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
			
			ln -sf /media/hdd/squashfs-images /media/squashfs-images
			
			$NEODEVICE$IMAGEKATALOG/$TARGET/squashfs-images/dreambox-dvb-modules-sqsh-img
			$NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/dreambox-squashfs-support start > /dev/null 2>&1
			$NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/udev start > /dev/null 2>&1
			$NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/.mmodprobe start > /dev/null 2>&1
			$NEODEVICE$IMAGEKATALOG/$TARGET/etc/init.d/alsa-state
			
			if [ ! -e $NEODEVICE$IMAGEKATALOG/$TARGET/.control_ok]; then
				rm -r $NEODEVICE$IMAGEKATALOG/$TARGET/etc/enigma2
			fi			

			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                      
                                                    
                            orgimag=`mount | sed '/sd/!d' | cut -d" " -f1`
                               for item in $orgimag; do
                                   ohdd=`echo  $item | cut -d"/" -f3`
                                   nhdd=`mount | sed "/\$ohdd/!d" | sed q | cut -d" " -f3`
                                   if [ $nhdd == '$NEODEVICE' ]; then
                                      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 
                                   else
                                      echo "umount "$nhdd  >> $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh
                                      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 
                                      echo ok 
                                   fi    
                               done	
                       	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 
                       	fi
                        chmod 755 `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh                                                                        
                        echo "NeoBOOT created the file mount enigma2_pre_start.sh " 
                        
                        echo " [NEOBOOT] Use chroot ... exec /usr/sbin/chroot..."
                                                                      
			if [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/sbin/init.sysvinit ]; then   
                                    echo "----------------------------------------"
	                            echo "   -=*[ BOOT UP IMAGE FROM DISK ]*=-    "
                                    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 ]; then                                    
                                    exec /usr/sbin/chroot `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET /sbin/init
			elif [ -f `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET/lib/systemd/systemd ]; then
                                    echo " start file - systemd" >> $neoLOG
                                    exec /usr/sbin/chroot `cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`$IMAGEKATALOG/$TARGET /lib/systemd/systemd
					echo "====================================================> "				    
			else
                                    echo " "                  
                                    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 
                                    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
# boot Flash 
UNIONFS=0
	if [ $TARGET = "Flash" ]; then
		ln -sfn /sbin/init.sysvinit /sbin/init		
	fi
   if [ -f /lib/modules/`uname -r`/kernel/fs/unionfs.ko ]; then
      /sbin/insmod /lib/modules/`uname -r`/kernel/fs/unionfs.ko >> /dev/null 2>&1
   fi
   if [ -f /boot/lib/modules/`uname -r`/kernel/fs/unionfs.ko ]; then
      /sbin/insmod /boot/lib/modules/`uname -r`/kernel/fs/unionfs.ko >> /dev/null 2>&1
   fi
   if [ `lsmod | grep unionfs | wc -l` -gt 0 ]; then
      UNIONFS=1
   fi
if [ -f /sbin/init.sysvinit ]; then
   # for enigma2 support
   if [ $UNIONFS -eq 1 -a -f /sbin/freeze ]; then
      exec /sbin/freeze $1
   else
      exec /sbin/init.sysvinit $1
   fi
else
   # for Neutrino and Debian Support
   exec /sbin/init $1
fi
exit 0
;;
esac
exit 0
