#!/bin/sh
#script mod by gutosie
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
CELBOOT=`cat $FINDBOOT`
NEODEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/.location`
INSTALLDEVICE=`cat /usr/lib/enigma2/python/Plugins/Extensions/NeoBoot/bin/install`

if [ $CELBOOT = "Flash" ] || [ -e /tmp/.init_reboot ] ; then 
	exec /sbin/init.sysvinit $1	                               
else         
	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 2
	mnttest="nouuid"
	if [ -e $NEOBOOT/bin/install  ]; then
        	orgimag1=`mount | sed '/sd/!d' | cut -d" " -f1`
		selectinstall=`cat $NEOBOOT/bin/install`
		for item in $orgimag1; do
			selectmnt=`blkid | sed '/$selectinstall/!d' | cut -d":" -f1`
			if [ $selectmnt == $item ]; then
				echo "selectmnt" $selectmnt
                        	mnttest="uuid"
				mount $selectmnt $NEO > /dev/null 2>&1
			fi
			if [ ! -e $NEO/ImageBoot  ]; 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 $NEO/ImageBoot  ]; then
				echo "NeoBoot hasn't been installed on: " $DEVICE
				umount $NEO > /dev/null 2>&1 
			else
				echo "NEOBOOTt has been installed on: " $DEVICE
				break;
			fi 
		done
	elif [ $mnttest == "uuid" ]; then	
		echo "NEOBOOT mount by UUID"
	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
	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 $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    
	TARGET=Flash
	if [ -f $IMAGEBOOT ]; then
	  TARGET=`cat $IMAGEBOOT`
	fi	
	if [ $TARGET = "Flash" ]; then
		echo "NEOBOOT is booting image from " $TARGET
		/bin/umount $NEO > /dev/null 2>&1
		/bin/umount /media/usb > /dev/null 2>&1
		/bin/umount /media/hdd > /dev/null 2>&1
	else
		echo "NEOBOOT is booting" $TARGET " from " $DEVICE
                echo 3 > /proc/sys/vm/drop_caches
		if [ -d $NEO/ImageBoot/$TARGET ]; then
			if [ -f $NEO/ImageBoot/$TARGET/etc/init.d/udev ]; then
				sed -ie s!"\$4"!"\$3"!g $NEO/ImageBoot/$TARGET/etc/init.d/udev
			fi
                	sleep 1		
			/bin/mount -o bind /dev $NEO/ImageBoot/$TARGET/dev
			/bin/mount -o bind /proc $NEO/ImageBoot/$TARGET/proc
			/bin/mount -o bind /sys $NEO/ImageBoot/$TARGET/sys
			rm $NEO/ImageBoot/$TARGET$NEO > /dev/null 2>&1
			mkdir $NEO/ImageBoot/$TARGET$NEO > /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 
                                  echo ok 
                               fi    
                           done	
                        chmod 755 $IMAGE/$TARGET/usr/bin/enigma2_pre_start.sh   

			if [ -f $IMAGE/$TARGET/sbin/init.sysvinit ]; then                                    
                                    exec /usr/sbin/chroot $IMAGE/$TARGET /sbin/init.sysvinit 
			elif [ -f $IMAGE/$TARGET/sbin/init ]; then                                    
                                    exec /usr/sbin/chroot $IMAGE/$TARGET /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 
                                    sync; sleep 10; exec /sbin/init.sysvinit $1
			fi
			exit 0
		fi
	fi
fi
if [ -f /sbin/init.sysvinit ]; then
		exec /sbin/init.sysvinit $1
fi

exit 0
