#!/bin/sh

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

NEWROOT=/newroot
ROOT_TIMEOUT=${FORGEOS_ROOT_TIMEOUT:-20}

root_fs_type() {
    awk '$2 == "/" { print $3; exit }' /proc/mounts 2>/dev/null
}

in_initramfs() {
    case "$(root_fs_type)" in
        rootfs|tmpfs)
            return 0
            ;;
        *)
            return 1
            ;;
    esac
}

console_note() {
    local tty

    for tty in /dev/tty0 /dev/tty1 /dev/console; do
        [ -w "$tty" ] || continue
        printf '%s\n' "$*" > "$tty" 2>/dev/null || true
    done
}

cmdline_value() {
    local key=$1
    local arg
    local value=

    for arg in $(cat /proc/cmdline 2>/dev/null); do
        case "$arg" in
            "$key="*)
                value=${arg#*=}
                ;;
        esac
    done

    [ -n "$value" ] || return 1
    printf '%s\n' "$value"
}

requested_root_spec() {
    local spec

    spec=$(cmdline_value forgeos.root 2>/dev/null || true)
    if [ -z "$spec" ]; then
        spec=$(cmdline_value root 2>/dev/null || true)
    fi

    case "$spec" in
        ''|rootfs|tmpfs)
            return 1
            ;;
    esac

    printf '%s\n' "$spec"
}

lookup_root_symlink() {
    local kind=$1
    local value=$2
    local path

    case "$kind" in
        PARTLABEL)
            path=/dev/disk/by-partlabel/$value
            ;;
        LABEL)
            path=/dev/disk/by-label/$value
            ;;
        UUID)
            path=/dev/disk/by-uuid/$value
            ;;
        PARTUUID)
            path=/dev/disk/by-partuuid/$value
            ;;
        *)
            return 1
            ;;
    esac

    [ -b "$path" ] || return 1
    printf '%s\n' "$path"
}

blkid_matches() {
    local info=$1
    local kind=$2
    local value=$3

    printf '%s\n' "$info" | grep -F " $kind=\"$value\"" >/dev/null 2>&1
}

scan_matching_root_device() {
    local kind=$1
    local value=$2
    local dev
    local info

    for dev in \
        /dev/nvme[0-9]n[0-9]p[0-9] \
        /dev/sd[a-z][0-9] \
        /dev/mmcblk[0-9]p[0-9] \
        /dev/vd[a-z][0-9]; do
        [ -b "$dev" ] || continue
        info=$(blkid "$dev" 2>/dev/null || true)
        blkid_matches "$info" "$kind" "$value" || continue
        printf '%s\n' "$dev"
        return 0
    done

    return 1
}

find_requested_root_device() {
    local spec=$1
    local kind
    local value

    case "$spec" in
        PARTLABEL=*)
            kind=PARTLABEL
            value=${spec#PARTLABEL=}
            ;;
        LABEL=*)
            kind=LABEL
            value=${spec#LABEL=}
            ;;
        UUID=*)
            kind=UUID
            value=${spec#UUID=}
            ;;
        PARTUUID=*)
            kind=PARTUUID
            value=${spec#PARTUUID=}
            ;;
        /dev/*)
            [ -b "$spec" ] || return 1
            printf '%s\n' "$spec"
            return 0
            ;;
        *)
            return 1
            ;;
    esac

    lookup_root_symlink "$kind" "$value" || scan_matching_root_device "$kind" "$value"
}

find_root_device() {
    local dev
    local spec

    spec=$(requested_root_spec 2>/dev/null || true)
    if [ -n "$spec" ]; then
        find_requested_root_device "$spec"
        return $?
    fi

    for dev in /dev/disk/by-partlabel/root /dev/disk/by-label/FORGE_ROOT; do
        [ -b "$dev" ] || continue
        printf '%s\n' "$dev"
        return 0
    done

    return 1
}

wait_for_root_device() {
    local attempt
    local rootdev

    for attempt in $(seq 1 "$ROOT_TIMEOUT"); do
        mdev -s 2>/dev/null || true
        rootdev=$(find_root_device) || true
        if [ -n "$rootdev" ]; then
            printf '%s\n' "$rootdev"
            return 0
        fi
        sleep 1
    done

    return 1
}

is_mounted_newroot() {
    grep -q " $NEWROOT " /proc/mounts 2>/dev/null
}

drop_to_rescue() {
    local reason=$1

    console_note "ForgeOS switch_root failed: $reason"
    /sbin/forgeos-bootdiag switch-root-failed || true
    if [ "$$" -eq 1 ]; then
        exec /sbin/init
    fi
    return 1
}

if ! grep -qw 'forgeos.switch_root=1' /proc/cmdline 2>/dev/null; then
    [ "$$" -eq 1 ] && exec /sbin/init
    exit 0
fi
if [ "$$" -ne 1 ]; then
    console_note "ForgeOS switch_root skipped: helper is not PID 1"
    exit 1
fi
in_initramfs || exec /sbin/init

/sbin/forgeos-bootdiag switch-root-start || true
console_note "ForgeOS initramfs: locating real root filesystem..."

ROOTDEV=$(wait_for_root_device) || drop_to_rescue "root partition not found" || exit 1

mkdir -p "$NEWROOT"
mount -t ext4 -o ro "$ROOTDEV" "$NEWROOT" 2>/dev/null || \
    drop_to_rescue "could not mount $ROOTDEV" || exit 1

if ! is_mounted_newroot; then
    drop_to_rescue "$NEWROOT was not mounted" || exit 1
fi

if [ ! -x "$NEWROOT/sbin/init" ]; then
    umount "$NEWROOT" 2>/dev/null || true
    drop_to_rescue "$ROOTDEV does not contain /sbin/init" || exit 1
fi

/sbin/forgeos-bootdiag switch-root-exec || true
console_note "ForgeOS initramfs: switching to $ROOTDEV"

for dir in dev proc sys run; do
    mkdir -p "$NEWROOT/$dir"
    mount --move "/$dir" "$NEWROOT/$dir" 2>/dev/null || true
done

exec switch_root -c /dev/console "$NEWROOT" /sbin/init

drop_to_rescue "switch_root exec returned"
exit 1
