#!/bin/bash

# omarchy:summary=Swap stock Mesa and vulkan-asahi to mesa-aurora and vulkan-aurora when linux-aurora 7.3+ is installed
# omarchy:hidden=true
# omarchy:requires-sudo=true

# linux-aurora 7.3+ names its DRM GPU driver "aurora", which stock mesa and
# vulkan-asahi cannot open (omacom/omarchy-mac-pkgs#234). [omarchy] is shared
# across all aarch64 platforms, so mesa-aurora cannot carry replaces=('mesa'),
# and a bare conflict aborts pacman -Syu --noconfirm. Run a targeted swap on
# Apple Silicon Macs that still carry stock Mesa or vulkan-asahi alongside
# linux-aurora 7.3+.

set -euo pipefail

omarchy-hw-aarch64-apple || exit 0

ver_line=$(pacman -Q linux-aurora 2>/dev/null) || exit 0
pkgver=${ver_line#* }
pkgver_base=${pkgver%%[-.]aurora*}
(( $(vercmp "$pkgver_base" 7.3) >= 0 )) || exit 0

installed=$(pacman -Qq 2>/dev/null) || exit 0
is_installed() {
  grep -Fxq -- "$1" <<<"$installed"
}

stock_gpu=()
targets=()
if is_installed mesa && ! is_installed mesa-aurora; then
  stock_gpu+=(mesa)
  targets+=(omarchy/mesa-aurora)
fi
vulkan_needed=0
if is_installed vulkan-asahi && ! is_installed vulkan-aurora; then
  stock_gpu+=(vulkan-asahi)
  vulkan_needed=1
fi
if is_installed vulkan-asahi-aurora && ! is_installed vulkan-aurora; then
  stock_gpu+=(vulkan-asahi-aurora)
  vulkan_needed=1
fi
if (( vulkan_needed )); then
  targets+=(omarchy/vulkan-aurora)
fi

(( ${#stock_gpu[@]} > 0 )) || exit 0

fail_swap() {
  printf '\033[31m\n%s\033[0m\n' "$1" >&2
  printf '%s\n' "${@:2}" "$(omarchy-i18n update.appleGraphicsGuidance 'omarchy update')" >&2
  exit 1
}

# --ask=4 answers Yes to every package conflict, including linux-aurora's own
# conflicts=("mesa-aurora<MIN" "vulkan-aurora<MIN"). Verify every target exists
# in [omarchy] and satisfies linux-aurora's conflict floor before invoking
# pacman so a lagging mirror can never uninstall the kernel.
kernel_conflicts=$(LC_ALL=C pacman -Qi linux-aurora 2>/dev/null | awk '
  /^Conflicts With[[:space:]]*:/ {
    sub(/^[^:]*:[[:space:]]*/, "")
    in_conflicts = 1
    print
    next
  }
  in_conflicts && /^[[:space:]]+/ {
    print
    next
  }
  in_conflicts { exit }
')

target_conflicts_kernel() {
  local pkg=$1 cand_ver=$2 token minver
  for token in $kernel_conflicts; do
    case "$token" in
      "$pkg")
        return 0
        ;;
      "$pkg<="*)
        minver=${token#"$pkg<="}
        (( $(vercmp "$cand_ver" "$minver") <= 0 )) && return 0
        ;;
      "$pkg<"*)
        minver=${token#"$pkg<"}
        (( $(vercmp "$cand_ver" "$minver") < 0 )) && return 0
        ;;
      "$pkg="*)
        minver=${token#"$pkg="}
        (( $(vercmp "$cand_ver" "$minver") == 0 )) && return 0
        ;;
    esac
  done
  return 1
}

for target in "${targets[@]}"; do
  pkg=${target#omarchy/}
  cand_ver=$(LC_ALL=C pacman -Si "$target" 2>/dev/null | awk '/^Version[[:space:]]*:/ { print $NF; exit }') || cand_ver=""
  if [[ -z $cand_ver ]] || target_conflicts_kernel "$pkg" "$cand_ver"; then
    fail_swap "$(omarchy-i18n update.appleGraphicsUnavailable "$pkgver" "${targets[*]}" "${stock_gpu[*]}")"
  fi
done

echo -e "\e[32m\n$(omarchy-i18n update.appleGraphics "${stock_gpu[*]}" "${targets[*]}")\e[0m"
# Under --noconfirm, --ask=4 flips ALPM_QUESTION_CONFLICT_PKG from No to Yes for
# this targeted swap so mesa-aurora can replace stock mesa without affecting
# other aarch64 machines.
if ! omarchy-update-pacman -S --needed --noconfirm --ask=4 "${targets[@]}"; then
  fail_swap "$(omarchy-i18n update.appleGraphicsFailed "${stock_gpu[*]}" "${targets[*]}" "$pkgver")" \
    "$(omarchy-i18n update.appleGraphicsRetry "sudo pacman -S --needed ${targets[*]}")"
fi
