#!/bin/bash
# Single-entry, systemd-compliant kernel-install hook for CachyOS

set -e

COMMAND="$1"          # add | remove
KERNEL_VERSION="$2"   # e.g. 6.18.4-1-cachyos

# Only act on CachyOS kernels
case "$KERNEL_VERSION" in
  *cachyos*) ;;
  *) exit 0 ;;
esac

BOOT_ROOT="${KERNEL_INSTALL_BOOT_ROOT:-/boot}"
ENTRY_DIR="$BOOT_ROOT/loader/entries"
ENTRY_FILE="$ENTRY_DIR/raveos.conf"

# --------------------------------------------------
# Fixed CachyOS kernel paths (non-versioned)
# --------------------------------------------------
LINUX_IMAGE="/vmlinuz-linux-cachyos"
INITRAMFS_IMAGE="/initramfs-linux-cachyos.img"

# --------------------------------------------------
# Microcode (must be first initrd)
# --------------------------------------------------
MICROCODE=""
[ -f "$BOOT_ROOT/intel-ucode.img" ] && MICROCODE="/intel-ucode.img"
[ -f "$BOOT_ROOT/amd-ucode.img" ] && MICROCODE="/amd-ucode.img"

# --------------------------------------------------
# Root device detection
# --------------------------------------------------
# findmnt can come back empty depending on the chroot/mount-namespace
# context this hook runs in (e.g. during pacstrap). Fall back to
# /proc/mounts, and finally /etc/fstab (once it exists), before giving up.
ROOT_SRC="$(findmnt -no SOURCE / || true)"
ROOT_FS="$(findmnt -no FSTYPE / || true)"
ROOT_OPTS="$(findmnt -no OPTIONS / || true)"

if [ -z "$ROOT_SRC" ]; then
  ROOT_SRC="$(awk '$2 == "/" {print $1; exit}' /proc/mounts)"
  ROOT_FS="$(awk '$2 == "/" {print $3; exit}' /proc/mounts)"
  ROOT_OPTS="$(awk '$2 == "/" {print $4; exit}' /proc/mounts)"
fi

if [ -z "$ROOT_SRC" ] && [ -f /etc/fstab ]; then
  ROOT_SRC="$(awk '$2 == "/" {print $1; exit}' /etc/fstab)"
  ROOT_FS="$(awk '$2 == "/" {print $3; exit}' /etc/fstab)"
  ROOT_OPTS="$(awk '$2 == "/" {print $4; exit}' /etc/fstab)"
fi

# /proc/mounts and /etc/fstab may give "UUID=xxxx"/"PARTUUID=xxxx" directly
# instead of a /dev/ path -- resolve to an actual device node so the blkid
# lookups below work either way.
case "$ROOT_SRC" in
  UUID=*|PARTUUID=*|LABEL=*)
    RESOLVED="$(blkid -t "$ROOT_SRC" -o device 2>/dev/null || true)"
    [ -n "$RESOLVED" ] && ROOT_SRC="$RESOLVED"
    ;;
esac

OPTIONS="rw quiet splash"

# LUKS
if [[ "$ROOT_SRC" == /dev/mapper/* ]]; then
  UUID="$(blkid -s UUID -o value "$ROOT_SRC" || true)"
  if [ -n "$UUID" ]; then
    OPTIONS="cryptdevice=UUID=$UUID:cryptroot root=/dev/mapper/cryptroot $OPTIONS"
  else
    OPTIONS="root=$ROOT_SRC $OPTIONS"
  fi
else
  PARTUUID="$(blkid -s PARTUUID -o value "$ROOT_SRC" || true)"
  [ -n "$PARTUUID" ] && OPTIONS="root=PARTUUID=$PARTUUID $OPTIONS"
fi

# --------------------------------------------------
# Btrfs subvolume support
# --------------------------------------------------
if [ "$ROOT_FS" = "btrfs" ]; then
  SUBVOL="$(echo "$ROOT_OPTS" | tr ',' '\n' | grep '^subvol=' || true)"
  [ -n "$SUBVOL" ] && OPTIONS="$OPTIONS rootflags=$SUBVOL"
fi

mkdir -p "$ENTRY_DIR"

case "$COMMAND" in
  add)
    {
      echo "title   RaveOS Linux (CachyOS Kernel)"
      echo "linux   $LINUX_IMAGE"
      [ -n "$MICROCODE" ] && echo "initrd  $MICROCODE"
      echo "initrd  $INITRAMFS_IMAGE"
      echo "options $OPTIONS"
    } > "$ENTRY_FILE"
    ;;

  remove)
    # Only remove entry if this was the last CachyOS kernel
    rm -f "$ENTRY_FILE"
    ;;

  *)
    exit 0
    ;;
esac

