#!/bin/sh
# --- SDE-COPYRIGHT-NOTE-BEGIN ---
# This copyright note is auto-generated by ./scripts/Create-CopyPatch.
#
# Filename: package/.../mkinitrd/initrdinit.sh
# Copyright (C) 2006 The OpenSDE Project
# Copyright (C) 2005 - 2006 The T2 SDE Project
#
# More information can be found in the files COPYING and README.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License. A copy of the
# GNU General Public License can be found in the file COPYING.
# --- SDE-COPYRIGHT-NOTE-END ---

echo "Entering early userspace ..."

PATH=/sbin:/bin

echo "Mounting /dev, /proc and /sys ..."
mount -t tmpfs none /dev
mount -t proc  none /proc
mount -t usbfs none /proc/bus/usb
mount -t sysfs none /sys
ln -s /proc/self/fd /dev/fd

if [ ! -c /dev/console ]; then
        mknod /dev/console c 5 1
fi
if [ ! -c /dev/null ]; then
        mknod /dev/null c 1 3
fi
if [ ! -c /dev/zero ]; then
        mknod /dev/zero c 1 5
fi

echo "Populating /dev (u/dev) ..."
echo "" > /sys/kernel/uevent_helper
udevd --daemon

echo "Triggering coldplug ..."
udevtrigger
udevsettle

echo "Loading additional subsystem and filesystem driver ..."
# hack to be removed
#modprobe sbp2

# well some hardcoded help for now ...
#modprobe ide-generic
#modprobe ide-disk
#modprobe ide-cd
#modprobe sd_mod
#modprobe sr_mod
#modprobe sg

# the modular filesystems ...
for x in /lib/modules/*/kernel/fs/{*/,}*.*o ; do
	x=${x##*/} ; x=${x%.*o}
	modprobe $x
done

echo "Mounting rootfs ..."

# get the root device and init
root="root= `cat /proc/cmdline`" ; root=${root##*root=} ; root=${root%% *}
init="init= `cat /proc/cmdline`" ; init=${init##*init=} ; init=${init%% *}

# try best match / detected rootfs first, all the others thereafter
filesystems=`disktype $root 2>/dev/null |
             sed -e '/file system/!d' -e 's/file system.*//' -e 's/ //g' \
                 -e 'y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/' \
                 -e 's/fat32/vfat/'
             sed '1!G ; $p ; h ; d' /proc/filesystems | sed /^nodev/d`

mkdir /rootfs

if [ "$root" ]; then
  i=0
  while [ $i -le 9 ]; do
	for fs in $filesystems ; do
	  if mount -t $fs $root /rootfs -o ro 2> /dev/null; then
		echo "Successfully mounted rootfs as $fs."

		# TODO: later on search other places if we want 100% backward compat.
		[ "$init" ] || init=/sbin/init
		if [ -f /rootfs/$init ]; then
			mount -t none /dev -o move /rootfs/dev
			mount -t none /proc -o move /rootfs/proc
			mount -t none /sys -o move /rootfs/sys

			if [ ! -c /rootfs/dev/console ]; then
				mknod /rootfs/dev/console c 5 1
			fi
			if [ ! -c /rootfs/dev/null ]; then
				mknod /rootfs/dev/null c 1 3
			fi
			if [ ! -c /rootfs/dev/zero ]; then
				mknod /rootfs/dev/zero c 1 5
			fi
			if [ ! -c /rootfs/dev/tty ]; then
				mknod /rootfs/dev/tty c 5 0
			fi

			# Before we switch to the real root
			# we have to kill udevd
			ls -1l /rootfs/proc/*/exe 2> /dev/null | \
				sed -n -e "s,.*/proc/\([0-9]*\)/.*udevd.*,kill \1,p" | \
				/bin/sh

			# enter the systems root
			exec switch_root /rootfs $init $*
		else
			echo "Specified init ($init) does not exist!"
		fi
	  fi
	done
  [ $(( i++ )) -eq 0 ] && echo "Waiting for root device to become ready ..."
  sleep 1
  done
fi

echo "Ouhm - some boot problem, but I do not scream. Debug shell:"
exec /bin/sh