#!/usr/bin/env bash
# Runs INSIDE arch-chroot. systemd-boot + mkinitcpio with STATIC loader entries.
# mkinitcpio already built /boot/initramfs-linux*.img during pacstrap, and its
# pacman hooks rebuild them on every kernel update — nothing to regenerate here
# or later. The kernel cmdline lives in the loader entry's `options` line.
set -euo pipefail

bootctl install                                  # ESP is mounted at /boot

ROOT_UUID="$(findmnt -no UUID /)"

# microcode image matching this CPU (both packages are installed)
case "$(grep -m1 -oE 'Intel|AMD' /proc/cpuinfo)" in
  Intel) UCODE="initrd  /intel-ucode.img" ;;
  AMD)   UCODE="initrd  /amd-ucode.img" ;;
  *)     UCODE="" ;;
esac

mkdir -p /boot/loader/entries

cat > /boot/loader/loader.conf <<EOF
default arch.conf
timeout 3
console-mode keep
EOF

cat > /boot/loader/entries/arch.conf <<EOF
title   Arch Linux
linux   /vmlinuz-linux
$UCODE
initrd  /initramfs-linux.img
options root=UUID=$ROOT_UUID rw
EOF

cat > /boot/loader/entries/arch-fallback.conf <<EOF
title   Arch Linux (fallback initramfs)
linux   /vmlinuz-linux
$UCODE
initrd  /initramfs-linux-fallback.img
options root=UUID=$ROOT_UUID rw
EOF
