#!/usr/bin/env bash
# Enable user-managed AUR support by building the yay helper from its official
# AUR source. Run this after installing OnixOS, as a normal sudo-capable user.
set -euo pipefail

if (( EUID == 0 )); then
    echo "Run onixos-enable-aur as a normal user, not root." >&2
    exit 1
fi

if command -v yay >/dev/null 2>&1; then
    echo "AUR support is already enabled: $(command -v yay)"
    exit 0
fi

command -v sudo >/dev/null || { echo "sudo is required." >&2; exit 1; }

echo "[AUR] Installing build prerequisites"
sudo pacman -S --noconfirm --needed base-devel git

build_dir="$(mktemp -d -t onixos-aur.XXXXXX)"
trap 'rm -rf -- "$build_dir"' EXIT

echo "[AUR] Building yay from aur.archlinux.org"
git clone --depth=1 https://aur.archlinux.org/yay.git "$build_dir/yay"
cd "$build_dir/yay"
makepkg -si --noconfirm --needed

echo "[AUR] Enabled. Use yay to install AUR packages."
