#!/bin/bash

# omarchy:summary=Rebuild the boot images after an initramfs, kernel or command line change
# omarchy:args=[--initramfs PKGBASE=FILE]... [PKGBASE...] | --prebuilt | --pacman-install | --pacman-remove
# omarchy:hidden=true
# omarchy:requires-sudo=true

# Limine: limine-mkinitcpio, which reports a kernel it could not rebuild but
# still exits 0, so its output is checked. systemd-boot: mkinitcpio builds a
# plain initramfs per kernel, omarchy-boot-sync stores it as a boot image with
# the command line in effect and publishes the images. Either way a failure
# exits non-zero.
#
# --pacman-install / --pacman-remove are the pacman hooks' entry points on
# systemd-boot machines; --pacman-install reads the hook targets on stdin.
#
# --initramfs PKGBASE=FILE stores FILE as that kernel's initramfs on
# systemd-boot instead of building one: the installer passes the generic one
# its root image carries, which saves the install its mkinitcpio build. Every
# other kernel builds as usual, and so does this one when FILE is missing or
# the kernel isn't installed. An import touches $STORE/prebuilt-initramfs so
# omarchy-boot-confirm schedules a background rebuild with autodetect and early
# KMS after the first boot.

set -euo pipefail

MODULES=${OMARCHY_BOOT_MODULES:-/usr/lib/modules}
MKINITCPIO=${OMARCHY_BOOT_MKINITCPIO:-/usr/bin/mkinitcpio}
STORE=${OMARCHY_BOOT_STORE:-/var/lib/omarchy/boot}
PREBUILT_MARKER=$STORE/prebuilt-initramfs
REBUILD_LOCK=${OMARCHY_BOOT_REBUILD_LOCK:-/run/omarchy-boot-rebuild.lock}
PACMAN_LOCK=${OMARCHY_PACMAN_LOCK:-/var/lib/pacman/db.lck}
PACMAN_LOCAL_DB=${OMARCHY_PACMAN_LOCAL_DB:-/var/lib/pacman/local}
DEFER_ON_PACMAN=0
declare -A PREBUILT=()

with_rebuild_lock() {
  if ! mkdir -p "$(dirname "$REBUILD_LOCK")" || ! exec 8>"$REBUILD_LOCK"; then
    echo "cannot open boot rebuild lock $REBUILD_LOCK" >&2
    return 1
  fi
  flock -w "${OMARCHY_BOOT_REBUILD_LOCK_WAIT:-300}" 8 || {
    echo "another boot image rebuild is still running" >&2
    return 1
  }
}

# Catches a fast pacman transaction that starts and finishes while mkinitcpio runs.
pacman_db_stamp() {
  [[ -d $PACMAN_LOCAL_DB ]] && stat -c '%Y:%Z' "$PACMAN_LOCAL_DB" 2>/dev/null || true
}

# Only the kernel package owns modules.builtin (pkgbase may be the aarch64 shim's, the directory
# also the headers'); the copy kernel-modules-hook restores has no owner. Nothing owned: keep all.
installed_kernels() {
  local pkgbase dir all=() owned=()
  for pkgbase in "$MODULES"/*/pkgbase; do
    [[ -f $pkgbase ]] || continue
    dir=${pkgbase%/pkgbase}
    all+=("$(<"$pkgbase") ${dir##*/}")
    if pacman -Qo -- "$dir/modules.builtin" &>/dev/null; then owned+=("${all[-1]}"); fi
  done
  ((${#owned[@]})) || owned=("${all[@]}")
  ((${#owned[@]} == 0)) || printf '%s\n' "${owned[@]}"
}

rebuild_limine() {
  local log status=0
  log=$(mktemp)
  limine-mkinitcpio "$@" 2>&1 | tee "$log" || status=$?
  if grep -q 'mkinitcpio failed for kernel' "$log"; then
    status=1
  fi
  rm -f "$log"
  return "$status"
}

# The boot hooks Limine's entry tool runs before it builds an image, kept as
# the contract: Snapdragon's device tree list refreshes there
# (install/hardware/qualcomm/dtb-uki.sh). Exit codes 100 and up stop the build.
run_pre_hooks() {
  local hook status
  for hook in "${OMARCHY_BOOT_HOOKS_DIR:-/etc/boot/hooks/pre.d}"/*; do
    [[ -x $hook && $hook != *.disabled ]] || continue
    # Limine's own hooks (config enrollment) are Limine's business.
    [[ $(readlink -f "$hook") == */limine-* ]] && continue
    status=0
    HOOK_CALLER=omarchy-boot-rebuild "$hook" || status=$?
    if ((status >= 100)); then
      echo "Boot hook $hook failed ($status); not building boot images" >&2
      return 1
    fi
  done
}

rebuild_systemd_boot() {
  local -a only=("$@")
  local pkgbase kver initramfs db_before failed=0 built=0 used_prebuilt=0 deferred=0
  with_rebuild_lock || return 1
  if ((DEFER_ON_PACMAN)); then
    [[ -e $PREBUILT_MARKER && ! -e $PACMAN_LOCK ]] || return 0
    db_before=$(pacman_db_stamp)
  fi
  run_pre_hooks || return 1
  while read -r pkgbase kver; do
    if ((${#only[@]})) && [[ " ${only[*]} " != *" $pkgbase "* ]]; then
      continue
    fi
    initramfs=${PREBUILT[$pkgbase]:-}
    unset "PREBUILT[$pkgbase]"
    if [[ -n $initramfs && ! -s $initramfs ]]; then
      echo "The pre-built initramfs $initramfs for $pkgbase is missing; building one" >&2
      initramfs=""
    fi
    if [[ -n $initramfs ]]; then
      echo "Using the pre-built initramfs $initramfs for kernel $pkgbase ($kver)"
      if omarchy-boot-sync import "$pkgbase" "$kver" "$MODULES/$kver/vmlinuz" "$initramfs"; then
        used_prebuilt=1
      else
        failed=1
      fi
      built=1
    else
      initramfs=$(mktemp --suffix=.img)
      if "$MKINITCPIO" -k "$kver" -g "$initramfs"; then
        if ((DEFER_ON_PACMAN)) && [[ -e $PACMAN_LOCK || $(pacman_db_stamp) != "$db_before" ]]; then
          deferred=1
          rm -f "$initramfs"
          break
        fi
        omarchy-boot-sync import "$pkgbase" "$kver" "$MODULES/$kver/vmlinuz" "$initramfs" || failed=1
        built=1
      else
        echo "mkinitcpio failed for kernel $pkgbase ($kver)" >&2
        failed=1
      fi
      rm -f "$initramfs"
    fi
  done < <(installed_kernels)
  for pkgbase in "${!PREBUILT[@]}"; do
    echo "Kernel $pkgbase isn't installed or isn't being rebuilt; ignoring its pre-built initramfs" >&2
  done
  if ((built)); then
    omarchy-boot-sync sync || failed=1
  fi
  if ((failed == 0 && built && !deferred)); then
    if ((used_prebuilt)); then
      touch "$PREBUILT_MARKER" 2>/dev/null || true
    elif ((${#only[@]} == 0)); then
      rm -f "$PREBUILT_MARKER" 2>/dev/null || true
    fi
  fi
  return "$failed"
}

# Kernels whose vmlinuz changed; nothing (meaning all kernels) when anything
# else the initramfs is built from changed.
pacman_kernels() {
  local -a kernels=()
  local target all=0 kver
  while read -r target; do
    if [[ $target == usr/lib/modules/*/vmlinuz ]]; then
      kver=${target#usr/lib/modules/}
      kver=${kver%/vmlinuz}
      [[ -f $MODULES/$kver/pkgbase ]] && kernels+=("$(<"$MODULES/$kver/pkgbase")")
    else
      all=1
    fi
  done
  if ((!all)); then
    printf '%s\n' "${kernels[@]}"
  fi
}

record_initcpio_state() {
  if command -v omarchy-boot-sync-initcpio >/dev/null 2>&1; then
    omarchy-boot-sync-initcpio --record || true
  fi
}

case ${1:-} in
--prebuilt)
  DEFER_ON_PACMAN=1
  rebuild_systemd_boot
  ;;
--pacman-install)
  mapfile -t kernels < <(pacman_kernels)
  rebuild_systemd_boot "${kernels[@]}"
  if ((${#kernels[@]} == 0)); then
    record_initcpio_state
  fi
  ;;
--pacman-remove)
  # After the transaction: the sync drops the live entries of a kernel that is
  # no longer installed, and an upgraded kernel keeps its published image until
  # the install hook replaces it. A snapshot pinned to an image keeps it.
  omarchy-boot-sync sync
  ;;
*)
  kernels=()
  while (($#)); do
    case $1 in
    --initramfs)
      [[ ${2:-} == ?*=?* ]] || { echo "--initramfs needs PKGBASE=FILE" >&2; exit 2; }
      PREBUILT[${2%%=*}]=${2#*=}
      shift
      ;;
    *) kernels+=("$1") ;;
    esac
    shift
  done
  status=0
  for backend in $(omarchy-boot-backend --managed); do
    case $backend in
    limine) rebuild_limine "${kernels[@]}" || status=1 ;;
    systemd-boot) rebuild_systemd_boot "${kernels[@]}" || status=1 ;;
    esac
  done
  if ((status == 0 && ${#kernels[@]} == 0)); then
    record_initcpio_state
  fi
  exit "$status"
  ;;
esac
