#!/bin/bash

set -u

mapfile -t pkgs < <(yay -Qua | awk '{print $1}')

if [ ${#pkgs[@]} -eq 0 ]; then
    echo "No AUR updates available."
    exit 0
fi

echo "Found ${#pkgs[@]} AUR package(s) with updates:"
printf '  %s\n' "${pkgs[@]}"
echo

failed=()
for pkg in "${pkgs[@]}"; do
    echo "==> Updating $pkg"
    if yay -S --noconfirm --answerdiff None --answerclean All --removemake "$pkg"; then
        echo "==> $pkg: OK"
    else
        echo "==> $pkg: FAILED"
        failed+=("$pkg")
    fi
    echo
done

if [ ${#failed[@]} -gt 0 ]; then
    echo "Failed packages:"
    printf '  %s\n' "${failed[@]}"
    exit 1
fi
