#!/bin/sh

echo "====================================================> "
echo "_________________________ Start inithelp __________________________"
if [ -f /proc/stb/info/vumodel ];  then  
        VUMODEL=$( cat /proc/stb/info/vumodel )
fi
NEOBOOT=/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/
NEODEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`
INSTALLDEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install`
IMAGEKATALOG=ImageBoot
BOXHOSTNAME=`cat /etc/hostname`        
TARGET=Flash       
if [ -f $NEODEVICE$IMAGEKATALOG/.neonextboot ]; then
        TARGET=`cat $NEODEVICE$IMAGEKATALOG/.neonextboot`
fi
if [ $TARGET = "Flash" ]; then
        /bin/umount /media/usb > /dev/null 2>&1
        echo "BOOTNEO is booting image from " $TARGET        
        /bin/umount /media/hdd > /dev/null 2>&1
                    ln -sfn /sbin/init.sysvinit /sbin/init
                    echo "----------------------------------"
                    echo "     BOOT UP IMAGE FROM FLASH     "
                    echo "----------------------------------"
                    echo "====================================================> "
                    sleep 1
                    PATH=/sbin:/bin:/usr/sbin:/usr/bin
                    break;
else
    if [ ! -f  $NEODEVICE$IMAGEKATALOG/.neonextboot ]; then
	    mnttest="nolocation"
	    if [ -f /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 -l $NEODEVICE > /dev/null 2>&1
			        else
				            echo "NEOBOOT has been installed on: " $selectmnt
				            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 [ -f `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
    fi
	if [ ! -f $NEODEVICE$IMAGEKATALOG/.neonextboot  ]; then
	        if [ -f /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh  ]; then
                        /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh
                        echo "_________________________ NeoBoot mount by MOUNTPOINT _________________________ "
	        fi
	elif [ $mnttest == "label" ]; then
		    echo "_________________________ NeoBoot mount by DEVICE_________________________ "
	elif [ $mnttest == "location" ]; then
		    echo "_______________________ NeoBoot mount by LOCATION _______________________  "
	fi
    fi
    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
    fi    
    if [ ! -f  $NEODEVICE$IMAGEKATALOG/.neonextboot ]; then
            ln -sfn /sbin/init.sysvinit /sbin/init
            echo "Flash " > $NEODEVICE$IMAGEKATALOG/.neonextboot
            echo "====================================================> "
            echo "NEOBOOT - does not detect the location file neonextboot. Back to flash..."
            sleep 1
    fi
    echo "====================================================> "  
    echo " ________ test boot new imaga ________ "
    if [ -f $NEODEVICE$IMAGEKATALOG/$TARGET/.control_ok ]; then
            [ $PL ] && echo "Nie ma bledu, normalny start image..." || echo "No Error - Booting image normal..."
            echo "====================================================> "
            sleep 1
    elif [ -f $NEODEVICE$IMAGEKATALOG/$TARGET/.control_boot_new_image ];then
            rm -r $NEODEVICE$IMAGEKATALOG/$TARGET/.control_boot_new_image
            echo "====================================================> "
            echo "Remove file control_boot_new_image"
            echo "First start image - The first attempt to launch a new image... "
            echo "====================================================> "
            sleep 1
    else
	        /usr/bin/showiframe /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/error.mvi > /dev/null 2>&1
            echo "====================================================> "
			echo "ERROR Please reinstalling the kernel for flash image now."
            echo "NeoBoot not run it image Restart STB - Back to Flash..."
			echo "Wrong kernel"  > $NEODEVICE$IMAGEKATALOG/.kernel/used_flash_kernel
			echo "Flash "  > $NEODEVICE$IMAGEKATALOG/.neonextboot
            echo "====================================================> "
			sleep 10
			ln -sfn /sbin/init.sysvinit /sbin/init
			mount -a
			reboot -d -f      
    fi
    if [ $BOXHOSTNAME = "vuduo4k" ] || [ $BOXHOSTNAME = "vuduo4kse" ] || [ $BOXHOSTNAME = "vuzero4k" ] || [ $BOXHOSTNAME = "bm750" ] || [ $BOXHOSTNAME = "vuduo" ] || [ $BOXHOSTNAME = "vusolo" ] || [ $BOXHOSTNAME = "vuuno" ] || [ $BOXHOSTNAME = "vuultimo" ] || [ $BOXHOSTNAME = "vuultimo4k" ] || [ $BOXHOSTNAME = "vusolo4k" ] || [ $BOXHOSTNAME = "vuuno4kse" ] || [ $BOXHOSTNAME = "vuuno4k" ] || [ $BOXHOSTNAME = "vusolo2" ] || [ $BOXHOSTNAME = "vuduo2" ] || [ $BOXHOSTNAME = "vusolose" ] || [ $BOXHOSTNAME = "vuzero" ] ; then
            break;
	else
	            echo "====================================================> "
                echo "  Neo-Boot start  boot manager. Waiting to change image - 20s "
                /usr/bin/showiframe $NEOBOOT/neologo.mvi > /dev/null 2>&1
                chmod 0755 $NEOBOOT/bin/neobm
                $NEOBOOT/bin/neobm > /dev/null 2>&1
    fi
    if [ -f $NEODEVICE$IMAGEKATALOG/.neonextboot ]; then
        if [ ! -f $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh ] ; then
            rm -f $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh
            echo "====================================================> "
            echo "DELETE enigma2_pre_start.sh"
            cp -af /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/mountpoint.sh $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh
            cp -af /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh $NEODEVICE$IMAGEKATALOG/$TARGET/usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/files/neo.sh
            chmod 0755 $NEODEVICE$IMAGEKATALOG/$TARGET/usr/bin/enigma2_pre_start.sh
            echo "NeoBOOT created the file mount enigma2_pre_start.sh "
            echo "====================================================> "
        fi
    fi
fi

