#!/bin/bash

# omarchy:summary=Print the boot loader whose boot images Omarchy manages: systemd-boot, limine or none
# omarchy:args=[--managed]
# omarchy:hidden=true

# systemd-boot once omarchy-boot-switch made it the loader the firmware starts,
# or on a machine installed with it; limine on a machine still booting Limine. --managed prints every loader whose
# boot images have to be kept current: both while a switch is on trial, when
# the firmware still starts Limine but systemd-boot is staged next to it.

state=${OMARCHY_BOOT_STORE:-/var/lib/omarchy/boot}/state
phase=$(sed -n 's/^phase=//p' "$state" 2>/dev/null | tail -1)
limine_gate=${OMARCHY_LIMINE_GATE:-/var/lib/omarchy/limine.enabled}

# An Apple Silicon Mac carries Limine's tools from its boot package but boots
# Limine only once it opted in with the gate marker; before that its own boot
# package builds its boot images, and the gate refuses a Limine build (status
# 100). So the marker, as the gate reads it, decides there.
has_limine() {
  omarchy-cmd-present limine-entry-tool || return 1
  if omarchy-hw-aarch64-apple; then
    [[ -e $limine_gate || -L $limine_gate ]]
  else
    return 0
  fi
}

case $phase in
active)
  echo systemd-boot
  ;;
promoted)
  # systemd-boot starts the machine, and Limine stays its fallback until the
  # switch finishes, so both are kept current.
  [[ ${1:-} == "--managed" ]] && has_limine && echo limine
  echo systemd-boot
  ;;
staged | trial | trial-failed)
  if [[ ${1:-} == "--managed" ]]; then
    has_limine && echo limine
    echo systemd-boot
  elif has_limine; then
    echo limine
  else
    echo none
  fi
  ;;
*)
  # A machine installed with systemd-boot never switched: omarchy-boot-systemd,
  # whose snapper plugin is there, says so even in a root whose boot store
  # isn't mounted. (Its pacman hook can't: the installer masks it for a while.)
  if [[ -f ${OMARCHY_BOOT_SYSTEMD_MARKER:-/usr/lib/snapper/plugins/20-omarchy-boot} ]]; then
    echo systemd-boot
  elif has_limine; then
    echo limine
  else
    echo none
  fi
  ;;
esac
