#!/bin/bash

# omarchy:summary=Update system packages with pacman
# omarchy:requires-sudo=true

set -e

echo -e "\e[32m\nUpdate system packages\e[0m"

errors=$(mktemp)
trap 'rm -f "$errors"' EXIT

# /usr/share/omarchy is wholly Omarchy's; files can land there unowned and would
# otherwise abort the upgrade. Packaged content wins there.
#
# Progress bars stay on stdout. Errors are on stderr, kept for the conflict
# handler below; LC_ALL=C is what keeps them parseable in any locale.
if sudo env LC_ALL=C OMARCHY_UPDATE_PACMAN=1 pacman -Syu --noconfirm \
  --overwrite '/usr/share/omarchy/*' 2>"$errors"; then
  cat "$errors" >&2
  exit 0
fi
cat "$errors" >&2

# An upgrade blocked only by files pacman doesn't own yet is the one failure
# worth retrying: the handler clears them and runs this again. Anything else,
# including a second failure, is for a human.
[[ ${OMARCHY_UPDATE_RETRY:-} != 1 ]] || exit 1
# exec, so the EXIT trap above does not fire and the handler can still read the
# report. It takes over deleting it.
exec env OMARCHY_UPDATE_CONFLICT=1 omarchy-update-system-pkgs-when-conflicted "$errors"
