#!/bin/sh

. /init_functions

msg ":: Loading Initramfs"

/bin/mount -t sysfs none /sys
/bin/mount -t proc  none /proc

read CMDLINE </proc/cmdline
export CMDLINE

# Used so hooks can override params to kinit
export kinit_params=""
export root=""
echo "/sbin/modprobe" > /proc/sys/kernel/modprobe

msg ":: Loading udev..."
echo > /proc/sys/kernel/hotplug
/sbin/udevd --daemon

for cmd in ${CMDLINE}; do
    case "${cmd}" in
        \#*) break ;; # ignore everything after a # in the commandline
        # The kernel passes those to the kernel on its own
        [0123456Ss]) ;;
        single) ;;
        #Allow "init=X" to pass-through
        init=*) kinit_params="${kinit_params} ${cmd}" ;;
        # only export stuff that does work with dash :)
        *=*) cmd="$(replace -s= "${cmd}" '.' '_')"
             cmd="$(replace -s= "${cmd}" '-' '_')"
             export "${cmd}"
             ;;
        *)   cmd="$(replace "${cmd}" '.' '_')"
             cmd="$(replace "${cmd}" '-' '_')"
             export "${cmd}=y" 
           ;;
    esac
done

if [ -n "${disablehooks}" ]; then
    for d in $(replace "${disablehooks}" ','); do
        export "hook_${d}=disabled"
    done
fi

if [ -n "${disablemodules}" ]; then
    for d in $(replace "${disablemodules}" ','); do
        export "mod_${d}=disabled"
    done
fi

if [ -n "${earlymodules}" ]; then
    for m in $(replace "${earlymodules}" ','); do
        /sbin/modprobe -q ${m} > /dev/null 2>&1
    done
fi

. /config

for m in ${MODULES}; do
    TST=""
    eval "TST=\$mod_${m}"
    if [ "${TST}" != "disabled" ]; then
        /sbin/modprobe -q ${m} > /dev/null 2>&1
    fi
done

# If rootdelay is empty or not a non-negative integer, set it to 10
if [ -z "${rootdelay}" -o ! "${rootdelay}" -ge 0 ]; then
    export rootdelay=10
fi
# We'll wait for the root device, so make sure klibc doesn't
export kinit_params="${kinit_params} rootdelay=0"

if [ -e "/hooks" ]; then
    for h in ${HOOKS}; do
        TST=""
        eval "TST=\$hook_${h}"
        if [ "${TST}" != "disabled" ]; then
            run_hook () { msg "${h}: no run function defined"; }
            if [ -e "/hooks/${h}" ]; then
               . /hooks/${h}
               msg ":: Running Hook [${h}]"
               run_hook
            fi
        fi
    done
fi

if [ "${break}" = "y" ]; then
    echo ":: Break requested, type 'exit' to resume operation"
    echo "   NOTE: klibc contains no 'ls' binary, use 'echo *' instead"
    PS1="ramfs$ " /bin/sh -i
fi

# If we boot from NFS, don't check for a block device in /dev
# Let kinit do it all
if [ -z "${nfsroot}" -a "${root}" != "/dev/nfs" ]; then
    if ! poll_device "${root}" ${rootdelay}; then
        msg "\nRoot device '${root}' doesn't exist, attempting to create it"

        eval $(/bin/parseblock "${root}")
        if [ -z "${BLOCKDEVICE}" ]; then
            echo "ERROR: Failed to parse block device ids for '${root}'"
        else
            echo "/bin/mknod /dev/root b ${BLOCKDEVICE}"
            /bin/mknod /dev/root b ${BLOCKDEVICE} >/dev/null
            export root="/dev/root"
        fi
        if [ ! -b "${root}" -a ! -L "${root}" ]; then
            err "Unable to detect or create root device '${root}'"
            echo "You are being dropped to a recovery shell"
            echo "    Type 'reboot' to reboot"
            echo "    Type 'exit' to try and continue booting"
            echo "NOTE: klibc contains no 'ls' binary, use 'echo *' instead"
            echo ""
            echo "If the device '${root}' gets created while you are here,"
            echo "try adding 'rootdelay=10' or higher to the kernel command-line"
            PS1="ramfs$ " /bin/sh -i
            msg "Trying to continue (this will most likely fail)..."
        fi
    fi
fi

if [ -f "/message" ]; then
    msg "$(cat /message)"
fi
msg ":: Initramfs Completed - control passing to kinit"

#Special handling if udev is running
udevpid=$(/bin/minips -C udevd -o pid=)
if [ -n "${udevpid}" ]; then
    /bin/kill -9 ${udevpid} > /dev/null 2>&1
    /bin/sleep 0.01
fi

exec /bin/kinit "$@" -- "root=${root}" ${kinit_params} > /dev/null 2>&1