#!/bin/sh

if ! command -v info >/dev/null; then
    info() {
        printf '%s\n' "$*"
    }
fi

if ! command -v die >/dev/null; then
    die() {
        printf '%s\n' "$*" >&2
        echo
        echo "Rebooting in 1 minute..."
        sleep 60
        echo b > /proc/sysrq-trigger || /sbin/reboot -f
    }
fi

info "warewulf: running pre-init wwinit process..."

info "warewulf: mounting kernel file systems..."
mountpoint -q /proc || (mkdir -p /proc && mount -t proc proc /proc && echo "Mounted /proc")
mountpoint -q /dev || (mkdir -p /dev && mount -t devtmpfs devtmpfs /dev && echo "Mounted /dev")
mountpoint -q /sys || (mkdir -p /sys && mount -t sysfs sysfs /sys && echo "Mounted /sys")
# https://systemd.io/INITRD_INTERFACE/
mountpoint -q /run || (mkdir -p /run && mount -t tmpfs -o mode=0755,nodev,nosuid,strictatime tmpfs /run && echo "Mounted /run")

info "warewulf: loading /warewulf/config..."
if [ -f "/warewulf/config" ]; then
    . /warewulf/config
else
    die "warewulf: ERROR: /warewulf/config not found"
fi

if [ -x /warewulf/run-wwinit.d ]; then
        /warewulf/run-wwinit.d
fi

NEXT_INIT=/warewulf/run-init
chmod +rx "${NEXT_INIT}"

info "warewulf: configuring root file system..."
WWROOT="${WWROOT:-initramfs}"
if [ "${WWROOT}" = "initramfs" ]; then
    info "warewulf: using initial rootfs and invoking ${NEXT_INIT}..."
    exec "${NEXT_INIT}"
else
    mkdir /newroot
    info "warewulf: mounting ${WWROOT} at /newroot"
    if [ "${WWROOT}" = "ramfs" -o "${WWROOT}" = "tmpfs" ]; then
        mount wwroot /newroot -t ${WWROOT} -o mpol=interleave # mpol ignored for ramfs
    else
        mount ${WWROOT} /newroot
    fi

    info "warewulf: copying image to /newroot..."
    tar -cf - --exclude ./proc --exclude ./sys --exclude ./dev --exclude --exclude ./newroot . | tar -xf - -C /newroot

    mkdir /newroot/proc /newroot/dev /newroot/sys 2>/dev/null

    info "warewulf: switching to /newroot and invoking ${NEXT_INIT}..."
    exec /sbin/switch_root /newroot "${NEXT_INIT}"
fi

die "warewulf: ERROR: wwinit encountered a problem."
