#!/bin/bash
# Copy the live kernel into the freshly-unpacked target /boot.
#
# archiso keeps vmlinuz/initramfs in the ISO boot dir (arch/boot/x86_64/), NOT
# in the squashfs, so the rootfs that unpackfs lays down has an empty /boot.
# The kernel must be present before Calamares' `initcpio` module runs mkinitcpio
# (the stock linux.preset points ALL_kver at /boot/vmlinuz-linux) and before the
# `bootloader` module runs grub — otherwise the installed system is unbootable.
#
# Runs in the LIVE environment (Calamares shellprocess, dontChroot); the
# target root mount point is passed as $1.
#
# The kernel is read from /usr/lib/modules/$(uname -r)/vmlinuz — part of the
# live squashfs itself (the linux package always installs it there), not from
# the ISO's separate arch/boot/x86_64/ dir under /run/archiso/bootmnt. That
# mount gets torn down by archiso's own copytoram handling, which self-enables
# on most real hardware, so depending on it here would leave $ROOT/boot empty
# and the install unbootable on exactly the machines this is meant to run on.
set -uo pipefail

ROOT="${1:?target root required}"
KVER="$(uname -r)"
SRC_KERNEL="/usr/lib/modules/${KVER}/vmlinuz"
SRC_BOOT="/run/archiso/bootmnt/arch/boot/x86_64"

install -d -m 0755 "$ROOT/boot"

if [ -f "$SRC_KERNEL" ]; then
    cp -f "$SRC_KERNEL" "$ROOT/boot/vmlinuz-linux"
elif [ -f "$SRC_BOOT/vmlinuz-linux" ]; then
    echo "WARN: $SRC_KERNEL missing, falling back to $SRC_BOOT"
    cp -f "$SRC_BOOT/vmlinuz-linux" "$ROOT/boot/vmlinuz-linux"
else
    echo "ERROR: no kernel image found (checked $SRC_KERNEL and $SRC_BOOT/vmlinuz-linux) — install would be unbootable" >&2
    exit 1
fi

# Microcode, if the live medium carries it (grub-mkconfig picks it up). This
# is best-effort: post-install.sh's mkinitcpio `microcode` HOOKS entry embeds
# microcode from /usr/lib/firmware directly, so a missing standalone ucode.img
# here is not boot-critical, unlike the kernel image above.
for u in amd-ucode.img intel-ucode.img; do
    [ -f "$SRC_BOOT/$u" ] && cp -f "$SRC_BOOT/$u" "$ROOT/boot/$u"
done

# Replace the archiso initramfs setup that unpackfs copied from the live medium.
# On archiso the linux preset is PRESETS=('archiso') using archiso.conf (the live
# HOOKS). Calamares' `initcpio` runs `mkinitcpio -P`, which would build that
# archiso preset and either bake the live-boot hooks into the install or fail
# once archiso.conf is gone. Drop the drop-in and write a stock default+fallback
# preset so `initcpio` produces a normal, bootable initramfs from the config that
# the `initcpiocfg` module generates at /etc/mkinitcpio.conf.
rm -f "$ROOT/etc/mkinitcpio.conf.d/archiso.conf"
install -d -m 0755 "$ROOT/etc/mkinitcpio.d"
cat >"$ROOT/etc/mkinitcpio.d/linux.preset" <<'PRESET'
# mkinitcpio preset file for the 'linux' package
ALL_config="/etc/mkinitcpio.conf"
ALL_kver="/boot/vmlinuz-linux"

PRESETS=('default' 'fallback')

default_image="/boot/initramfs-linux.img"
fallback_image="/boot/initramfs-linux-fallback.img"
fallback_options="-S autodetect"
PRESET

echo "Copied live kernel into $ROOT/boot; reset mkinitcpio to a stock preset"
