#!/bin/bash

# omarchy:summary=Warn on Apple Silicon Macs still configured with legacy omarchy-mac repositories
# omarchy:hidden=true

# Legacy omarchy-mac installs can still carry [omarchy-aarch64], ALARM's [aur],
# or restricted Usage on [omarchy]. Detect these read-only via pacman-conf and
# warn clearly without modifying /etc/pacman.conf or blocking the update.

set -uo pipefail

omarchy-hw-aarch64-apple || exit 0

pacman_conf=${OMARCHY_PACMAN_CONF:-/etc/pacman.conf}
repos=$(pacman-conf --config "$pacman_conf" --repo-list 2>/dev/null) || exit 0

warnings=()
if grep -Fxq 'omarchy-aarch64' <<<"$repos"; then
  warnings+=("$(omarchy-i18n update.legacyMacRepo "$pacman_conf")")
fi
if grep -Fxq 'aur' <<<"$repos"; then
  # Only flag Arch Linux ARM's retired [aur] mirror, never user-hosted repos.
  aur_servers=$(pacman-conf --config "$pacman_conf" --repo aur Server 2>/dev/null || true)
  if grep -Eiq 'archlinuxarm\.org' <<<"$aur_servers"; then
    warnings+=("$(omarchy-i18n update.legacyMacAurRepo "$pacman_conf")")
  fi
fi
if grep -Fxq 'omarchy' <<<"$repos"; then
  usage=$(pacman-conf --config "$pacman_conf" --repo omarchy Usage 2>/dev/null || true)
  if [[ -n $usage ]] && ! grep -Fxq 'All' <<<"$usage" &&
    { ! grep -Fxq 'Sync' <<<"$usage" || ! grep -Fxq 'Install' <<<"$usage" || ! grep -Fxq 'Upgrade' <<<"$usage"; }; then
    warnings+=("$(omarchy-i18n update.legacyMacUsage "$(tr '\n' ' ' <<<"$usage" | sed 's/ $//')")")
  fi
fi

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

echo -e "\e[33m\n$(omarchy-i18n update.legacyMacHeading)\e[0m" >&2
for warning in "${warnings[@]}"; do
  echo "  - $warning" >&2
done
printf '%s\n' "$(omarchy-i18n update.legacyMacGuide)" >&2
echo "https://omarchy.org/manual/mac-support/#coming-from-omarchy-mac" >&2
echo >&2
exit 0
