#!/bin/sh

PATH=/sbin:/bin:/usr/sbin:/usr/bin

ROOT_MOUNT="/mnt"

# Copied from initramfs-framework. The core of this script probably should be
# turned into initramfs-framework modules to reduce duplication.
udev_daemon() {
	OPTIONS="/sbin/udev/udevd /sbin/udevd /lib/udev/udevd /lib/systemd/systemd-udevd"

	for o in $OPTIONS; do
		if [ -x "$o" ]; then
			echo $o
			return 0
		fi
	done

	return 1
}

_UDEV_DAEMON=`udev_daemon`

boot_live_root() {
    if [ -d /sys/class/leds/act ]; then
        echo "eth0" > /sys/class/leds/act/device_name
	echo 1 > /sys/class/leds/act/tx
	echo 1 > /sys/class/leds/act/rx
    fi
    if [ -d /sys/class/leds/link ]; then
        echo "eth0" > /sys/class/leds/link/device_name
	echo 1 > /sys/class/leds/link/link
    fi
    mkdir -p ${ROOT_MOUNT}

    if [ -z ${LABEL_DISK} ]; then
	if [ -b "${ROOTFS_PART}" ]; then
	    fsck -y ${ROOTFS_PART}	
	fi
    else
	BLK=`blkid -L ${LABEL_DISK}`
        if [ -b "${BLK}" ]; then
	    fsck -y ${BLK}
        fi
    fi
    if ! mount ${ROOTFS_PART} ${ROOT_MOUNT} ; then
        fatal "Could not mount rootfs partition"
    fi

    mount -n --move /run ${ROOT_MOUNT}/run
    # Move the mount points of some filesystems over to
    # the corresponding directories under the real root filesystem.
    #for dir in `awk '/\/dev.* \/run\/media/{print $2}' /proc/mounts`; do
    #    mkdir -p  ${ROOT_MOUNT}/run/media/${dir##*/}
    #    mount -n --move $dir ${ROOT_MOUNT}/run/media/${dir##*/}
    #done

    # Move the mount points of some filesystems over to
    # the corresponding directories under the real root filesystem.
    mount -n --move /proc ${ROOT_MOUNT}/proc
    mount -n --move /sys ${ROOT_MOUNT}/sys
    mount -n --move /dev ${ROOT_MOUNT}/dev

    cd $ROOT_MOUNT

    # busybox switch_root supports -c option
    exec switch_root -c /dev/console $ROOT_MOUNT /sbin/init $CMDLINE ||
        fatal "Couldn't switch_root, dropping to shell"
}

fatal() {
    echo $1 >$CONSOLE
    echo >$CONSOLE
    exec sh
}

early_setup() {
    mkdir -p /proc
    mkdir -p /sys
    mount -t proc proc /proc
    mount -t sysfs sysfs /sys
    # use /dev with devtmpfs
    if grep -q devtmpfs /proc/filesystems; then
        mkdir -p /dev
        mount -t devtmpfs devtmpfs /dev
    else
        if [ ! -d /dev ]; then
                fatal "ERROR: /dev doesn't exist and kernel doesn't has devtmpfs enabled."
        fi
    fi

    mkdir -p /run
    mkdir -p /var/run
    mount -t tmpfs tmpfs /run

    #$_UDEV_DAEMON --daemon
    #udevadm trigger --action=add
}

read_args() {
    [ -z "$CMDLINE" ] && CMDLINE=`cat /proc/cmdline`
    for arg in $CMDLINE; do
        optarg=`expr "x$arg" : 'x[^=]*=\(.*\)'`
        case $arg in
	    root=*)
		    ROOTFS_PART=$optarg
		    for arg1 in $ROOTFS_PART; do
			optarg1=`expr "x$arg1" : 'x[^=]*=\(.*\)'`
			case $arg1 in
			    LABEL=*)
				LABEL_DISK=$optarg1 ;;
			esac
		    done
		    ;;
            label=*)
                label=$optarg ;;
            console=*)
                if [ -z "${console_params}" ]; then
                    console_params=$arg
                else
                    console_params="$console_params $arg"
                fi ;;
        esac
    done
}

early_setup

[ -z "$CONSOLE" ] && CONSOLE="/dev/console"

read_args
[ -z "$label" ] && label="boot"

case $label in
    boot)
	boot_live_root
        ;;
    debug)
	fatal "Enter debug shell..."
	;;
    *)
        # Not sure what boot label is provided.  Try to boot to avoid locking up.
	fatal "not valid lable.Enter shell..."
        ;;
esac

# If we're getting here, we failed...
fatal "boot image failed"
