#!/bin/sh
if [ -e /tmp/.opd.inf ]; then
          rm /tmp/.opd.inf
	  break;
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 -a
		/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 "OPDBoot is starting mdev"
	echo "/sbin/mdev" > /proc/sys/kernel/hotplug
	/etc/init.d/mdev > /dev/null 2>&1

	sleep 5
	mnttest="nouuid"
	if [ -e /usr/lib/enigma2/python/Plugins/Extensions/OPDBoot/bin/install  ]; then
        	orgimag1=`mount | sed '/sd/!d' | cut -d" " -f1`
		selectinstall=`cat /usr/lib/enigma2/python/Plugins/Extensions/OPDBoot/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 /media/opdboot > /dev/null 2>&1
			fi
			if [ ! -e /media/opdboot/OPDBootI  ]; then
				echo "OPDBoot hasn't been installed on: " $selectmnt
				umount /media/opdboot > /dev/null 2>&1 
			else
				echo "OPDBoot has been installed on: " $selectmnt
				break;
			fi 			
		done
	fi	

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

	MODPROBE=/sbin/modprobe

	echo "OPDBoot is loading modules"
	for mod in dvb; do
		$MODPROBE $mod
	done

	# Hack for loading modules which are not loaded on diffrent kernel images
	/etc/init.d/modutils.sh
	#

	cat /etc/videomode2 > /proc/stb/video/videomode

	#we must wait until udev has created the video device
	(
	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 /usr/share/bootlogo.mvi || /usr/bin/showiframe /usr/share/bootlogo_wait.mvi;
	) &
        
        sleep 10
        echo "Driverload end"

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

	if [ -e /tmp/.opd.inf ]; then
		  break;
	fi

	/usr/lib/enigma2/python/Plugins/Extensions/OPDBoot/bin/fbclear > /dev/null 2>&1

	TARGET=Flash
	if [ -f /media/opdboot/OPDBootI/.opdboot ]; then
	  TARGET=`cat /media/opdboot/OPDBootI/.opdboot`
	fi
        #TARGET=Flash
	if [ $TARGET = "Flash" ]; then
		echo "OPDBoot is booting image from " $TARGET
		/bin/umount /media/opdboot > /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
	else
		echo "OPDBoot is booting" $TARGET " from " $DEVICE
		if [ -d /media/opdboot/OPDBootI/$TARGET ]; then
			if [ -f /media/opdboot/OPDBootI/$TARGET/etc/init.d/udev ]; then
				sed -ie s!"\$4"!"\$3"!g /media/opdboot/OPDBootI/$TARGET/etc/init.d/udev
			fi
			if [ -f /media/opdboot/OPDBootI/$TARGET/usr/share/bootlogo.mvi ]; then
				/usr/bin/showiframe /media/opdboot/OPDBootI/$TARGET/usr/share/bootlogo.mvi
			else
				/usr/bin/showiframe /media/opdboot/OPDBootI/$TARGET/usr/share/bootlogo.mvi
			fi

			sleep 1

			/bin/mount -o bind /dev /media/opdboot/OPDBootI/$TARGET/dev
			/bin/mount -o bind /proc /media/opdboot/OPDBootI/$TARGET/proc
			/bin/mount -o bind /sys /media/opdboot/OPDBootI/$TARGET/sys
			rm /media/opdboot/OPDBootI/$TARGET/media/opdboot > /dev/null 2>&1
			mkdir /media/opdboot/OPDBootI/$TARGET/media/opdboot > /dev/null 2>&1
                        orgimag=`mount | sed '/sd/!d' | cut -d" " -f1`
                        rm -f media/opdboot/OPDBootI/$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 == '/media/opdboot' ]; then
                                  echo $nhdd
                                  echo "mkdir "$nhdd  >> media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh
                                  echo "mount "$item $nhdd  >> media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh 
                               else
                                  echo "umount "$nhdd  >> media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh
                                  echo "mkdir "$nhdd  >> media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh
                                  echo "mount "$item $nhdd  >> media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh 
                                  echo ok 
                               fi    
                           done	
                        chmod 755 media/opdboot/OPDBootI/$TARGET/usr/bin/enigma2_pre_start.sh   
			#/bin/mount /dev/sda1 /media/opdboot/OPDBootI/$TARGET/media/hdd
			if [ -f /media/opdboot/OPDBootI/$TARGET/sbin/init.sysvinit ]; then
				exec /usr/sbin/chroot /media/opdboot/OPDBootI/$TARGET /sbin/init.sysvinit
			fi
			exit 0
		fi
	fi
fi
# boot Flash 

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

exit 0
