#!/bin/bash

# omarchy:summary=Reinstall all default Omarchy packages from the stable channel, or on Apple Silicon the edge channel
# omarchy:requires-sudo=true

set -e

# Reinstall all default Omarchy packages from the platform's default channel
# (stable, or edge on Apple Silicon) and downgrade any packages that are too new.

# Set the package repository to the platform's default channel
source "$OMARCHY_PATH/install/helpers/pacman.sh"
platform=$(omarchy-hw-platform)
channel=$(omarchy_pacman_default_channel "$platform")
omarchy-refresh-pacman "$channel"

# Downgrade any packages to that channel's setup
omarchy-update-pacman -Suu --noconfirm

# Ensure this platform's default packages are installed
default_packages=$(omarchy-pkg-defaults)
# pipewire-jack conflicts with jack and jack2, and --ask 4 below would remove
# them for it, so a machine that kept either keeps it.
if omarchy-pkg-present jack2 || omarchy-pkg-present jack; then
  default_packages=$(grep -vx pipewire-jack <<<"$default_packages")
fi
mapfile -t packages <<<"$default_packages"
# --ask 4 accepts removing a stock package a default replaces, such as
# power-profiles-daemon for omarchy-power-profiles-daemon.
omarchy-update-pacman -Syu --noconfirm --ask 4 --needed "${packages[@]}"
