#!/bin/sh

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

INFORM_PATH=/mnt/inform
HAL_PATH=/mnt/hal

SYNC="/bin/sync"
MKDIR="/bin/mkdir"
MOUNT="/bin/mount"
UMOUNT="/bin/umount"
DIRNAME="/bin/dirname"
REBOOT="/sbin/reboot"
BLKID="/usr/sbin/blkid"
CP="/bin/cp"

#------------------------------------------------
#       mount_partitions
#------------------------------------------------
mount_partitions() {
    "$MOUNT" -t proc none /proc
    "$MOUNT" -t sysfs none /sys
    "$MOUNT" -t smackfs smackfs /smack
    "$MOUNT" -t tmpfs tmpfs /run -o rw,nosuid,nodev,mode=755
    "$MOUNT" -t tmpfs tmpfs /tmp -o mode=1777,smackfsroot=*

    "$MKDIR" /dev/pts
    "$MOUNT" -t devpts devpts /dev/pts
}

#------------------------------------------------
#       mount_inform
#------------------------------------------------
mount_inform() {
    PART_INFORM=$("$BLKID" -L "inform" -o device)
    if [ "z$PART_INFORM" != "z" ]; then
        "$MKDIR" -p ${INFORM_PATH}
        "$MOUNT" -t ext4 ${PART_INFORM} ${INFORM_PATH}
    fi
}

#------------------------------------------------
#       mkdir_p_parent
#------------------------------------------------
mkdir_p_parent() {
    dst_dir=`"$DIRNAME" "$1"`
    if [ ! -d "$dst_dir" -a ! -L "$dst_dir" ]; then
        "$MKDIR" -p "$dst_dir"
    fi
}

#------------------------------------------------
#       copy_hal_data
#------------------------------------------------
copy_hal_data() {
    if [ ! -e "/hal/.hal_list" ]; then
        return
    fi

    need_copy=0
    while read file
    do
        dst="/hal/${file}"

        if [ ! -e $dst ]; then
            need_copy=1
            break
        fi
    done < /hal/.hal_list

    PART_HAL=$("$BLKID" -L "hal" -o device)
    if [ "$need_copy" = "1" -a "z$PART_HAL" != "z" ]; then
        #mount hal partition
        "$MKDIR" -p ${HAL_PATH}
        "$MOUNT" ${PART_HAL} ${HAL_PATH} -o ro

        #Load list and copy
        while read file
        do
            src="${HAL_PATH}/$file"
            dst="/hal/${file}"

            mkdir_p_parent $dst

            "$CP" -f "$src" "$dst"

        done < /hal/.hal_list

        #Done
        "$UMOUNT" ${HAL_PATH}
    fi
}

#------------------------------------------------
#       do_reboot
#------------------------------------------------
do_reboot() {
    echo "Reboot"
    "$SYNC"
    "$REBOOT"
    while [ 1 ]
    do
        sleep 1
        echo "."
    done
}

#------------------------------------------------
#       Main Routine Start
#------------------------------------------------
echo "You entered into /sbin/init on initrd"

mount_partitions
mount_inform
copy_hal_data

cd /

# Manually parse /proc/cmdline to avoid additional tools on image
read cmdline </proc/cmdline
echo "Kernel command line: $cmdline"
set -- $cmdline
while [ $# -gt 0 ]; do
    key="${1%%=*}"
    if [ "$key" = "bootmode" ]; then
        BOOT_MODE="${1#*=}"
        break;
    fi
    shift
done

if [ "z$BOOT_MODE" = "z" ]; then
    echo "BOOT_MODE was NOT defined!!"
    echo "Do reboot!!"
    do_reboot
fi
echo "BOOTMODE is ${BOOT_MODE}"

if [ -f /sbin/${BOOT_MODE}-init ]; then
    exec "/sbin/${BOOT_MODE}-init"
else
    echo "no ${BOOT_MODE}-init!!"
    echo "Do reboot!!"
    do_reboot
fi
