#!/bin/sh
# Copyright 2026 ANNE Media - https://anne.media
# All rights reserved.
#
# Redistribution and use of this script, with or without modification, is
# permitted provided that the following conditions are met:
#
# 1. Redistributions of this script must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
#  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
#  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO
#  EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
#  PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
#  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
#  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
#  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
#  ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
#repo/install/initrd-src/files/init
export PATH=/bin:/sbin:/usr/bin:/usr/sbin


mount -t proc     proc     /proc
mount -t sysfs    sysfs    /sys
mount -t devtmpfs devtmpfs /dev
mkdir -p /dev/pts
mount -t devpts devpts /dev/pts 2>/dev/null || true
[ -e /dev/ptmx ] || ln -sf /dev/pts/ptmx /dev/ptmx
mount -t securityfs securityfs /sys/kernel/security 2>/dev/null || true
mount -t efivarfs efivarfs /sys/firmware/efi/efivars 2>/dev/null || true

# /dev/fd and std* symlinks. bash's process substitution uses /dev/fd/N.
ln -sf /proc/self/fd   /dev/fd
ln -sf /proc/self/fd/0 /dev/stdin
ln -sf /proc/self/fd/1 /dev/stdout
ln -sf /proc/self/fd/2 /dev/stderr

# /tmp as tmpfs.
mount -t tmpfs -o mode=1777 tmpfs /tmp

echo /usr/sbin/modprobe > /proc/sys/kernel/modprobe 2>/dev/null || true

UDEVD=$(command -v udevd 2>/dev/null \
    || ls /usr/lib/udev/udevd 2>/dev/null \
    || ls /sbin/udevd 2>/dev/null \
    || true)
if [ -n "$UDEVD" ]; then
    "$UDEVD" --daemon 2>/dev/null || true
    udevadm control --reload-rules 2>/dev/null || true
    udevadm trigger --action=add 2>/dev/null || true
    udevadm settle --timeout=15 2>/dev/null || true
else
    echo /bin/mdev > /proc/sys/kernel/hotplug 2>/dev/null || true
    mdev -s 2>/dev/null || true
fi

modprobe loop 2>/dev/null || true

modprobe drm            2>/dev/null || true
modprobe drm_kms_helper 2>/dev/null || true
modprobe fbcon          2>/dev/null || true
modprobe bochs          2>/dev/null || true
modprobe cirrus         2>/dev/null || true
modprobe mgag200        2>/dev/null || true
modprobe virtio-gpu     2>/dev/null || true
modprobe vmwgfx         2>/dev/null || true
modprobe qxl            2>/dev/null || true
# Preload the AEAD composition dm-integrity uses, so its kernel
# self-test runs here at boot rather than during the crypto format
# dialog later. The self-test prints "alg: No test for ..." lines
# which would otherwise leak onto the console while dialog is up.
modprobe authenc  2>/dev/null || true
modprobe hmac     2>/dev/null || true


for m in sr_mod cdrom sd_mod ata_piix ata_generic ahci \
         virtio_pci virtio_blk virtio_scsi virtio_net \
         e100 e1000 e1000e r8169 tg3 bnx2 bnx2x \
         ext4 isofs vfat xfs btrfs; do
    modprobe "$m" 2>/dev/null || true
done

# AM-OS Boot Logo ---
if [ -c /dev/fb0 ] && [ -x /usr/bin/fbv ]; then
    LOGO_FILE="/etc/amos-logo-top.png"  # Default fallback (1920x)
    
    # Read the framebuffer width from sysfs
    if [ -r /sys/class/graphics/fb0/virtual_size ]; then
        FB_WIDTH=$(cut -d, -f1 /sys/class/graphics/fb0/virtual_size)
        
        # If the width is 1280 or smaller, use the 720p optimized logo
        if [ -n "$FB_WIDTH" ] && [ "$FB_WIDTH" -le 1280 ]; then
            if [ -f /etc/amos-logo-720.png ]; then
                LOGO_FILE="/etc/amos-logo-top-720.png"
            fi
        fi
    fi

    # Launch fbv with the selected file
    fbv -y -c -i -f "$LOGO_FILE" >/dev/null 2>&1 &
    echo $! > /run/amos-logo.pid
   # chvt 1
fi

udevadm settle --timeout=15 2>/dev/null || true

export LANG=C.utf8
export LC_ALL=C.utf8
export NCURSES_NO_UTF8_ACS=1
# Switch the virtual console to UTF-8 mode
if command -v kbd_mode >/dev/null 2>&1; then
    kbd_mode -u 2>/dev/null || true
fi

# Load a font with the interpunct glyph
if command -v setfont >/dev/null 2>&1; then
    setfont /usr/share/kbd/consolefonts/Lat2-Terminus16.psfu.gz 2>/dev/null || \
    setfont /usr/share/kbd/consolefonts/eurlatgr.psfu.gz 2>/dev/null || true
fi
# --------------------------------------

hostname amos 2>/dev/null || true
[ -f /etc/issue ] && cat /etc/issue

# --- network and dropbear SSH for debugging ---

ip link set lo up

mkdir -p /etc/dropbear /root/.ssh
chmod 700 /root/.ssh

if [ -f /etc/dropbear/authorized_keys ]; then
    cp /etc/dropbear/authorized_keys /root/.ssh/authorized_keys
    chmod 600 /root/.ssh/authorized_keys
else
    echo "SSH: no authorized_keys baked into initrd; dropbear will reject all logins."
fi

# Host key, generated once per boot (initrd /etc is not persistent).
if [ ! -f /etc/dropbear/dropbear_ed25519_host_key ]; then
    dropbearkey -t ed25519 -f /etc/dropbear/dropbear_ed25519_host_key >/dev/null
fi

# Bring up whatever NIC QEMU/VBox handed us. The -s argument points
# udhcpc at a lease script that actually applies the address; without
# it busybox udhcpc obtains the lease and does nothing with it.
for iface in $(ls /sys/class/net | grep -v '^lo$'); do
    ip link set "$iface" up 2>/dev/null || true
    udhcpc -i "$iface" -s /etc/udhcpc.script -q -n -t 10 2>/dev/null || true
    ip -4 addr show "$iface" | grep -q 'inet ' && break
done

# dropbear 2020.81 daemonises by itself. There is no -E flag on this
# version (that arrived in 2022.82). stderr is left connected so a
# failed start shows on the boot console instead of vanishing.
dropbear -s -p 62222 -r /etc/dropbear/dropbear_ed25519_host_key

if pgrep -x dropbear >/dev/null 2>&1; then
    echo "SSH: dropbear listening on :62222"
else
    echo "SSH: dropbear failed to start"
fi

# if [ -n "${FBV_PID:-}" ]; then
#     sleep 3
#     kill "$FBV_PID" 2>/dev/null || true
# fi

if [ -f /run/amos-logo.pid ]; then
    sleep 3
    kill "$(cat /run/amos-logo.pid)" 2>/dev/null || true
    pkill -x fbv 2>/dev/null || true
    rm -f /run/amos-logo.pid
fi

while :; do

    setsid cttyhack /etc/amos-prompt.sh
    rc=$?
    echo
    echo "Installer exited with status $rc."
    echo "Dropping to a shell. Type 'exit' to return to the prompt."
    echo
    setsid cttyhack /bin/bash -l || true
    echo
    echo "Shell exited. Restarting the boot prompt in 5 s..."
    sleep 5
done
