#!/usr/bin/env bash

# Remove the solopasha/hyprland COPR left over from an earlier setup.
#
# Panama installs Hyprland from lionheartp/Hyprland. A machine set up before
# that decision -- or half set up from sunhat-era notes -- can still carry
# solopasha's COPR repo file. It arrives disabled, so nothing is broken today,
# and that is exactly why it is worth removing now rather than later: the moment
# somebody enables it while debugging, two repositories provide hyprland,
# hyprland-uwsm and xdg-desktop-portal-hyprland, and dnf resolves the split by
# version rather than by intent. Half the desktop from one COPR and half from
# the other is a very bad afternoon.
#
# Only Panama's business if it is disabled. A machine that has deliberately
# enabled solopasha is a machine somebody made a choice about, and a migration
# is not the place to overrule it.

set -euo pipefail

repo_file="/etc/yum.repos.d/_copr:copr.fedorainfracloud.org:solopasha:hyprland.repo"

[[ -e "$repo_file" ]] || exit 0

# Enabled means somebody chose it. Say so and leave.
if grep -qE '^\s*enabled\s*=\s*1' "$repo_file"; then
    echo "Leaving solopasha/hyprland alone: it is enabled, so it is in use deliberately."
    echo "Panama installs Hyprland from lionheartp/Hyprland; having both enabled will"
    echo "eventually mix the two. Disable it with: sudo dnf copr disable solopasha/hyprland"
    exit 0
fi

PANAMA_PATH="${PANAMA_PATH:-$HOME/.local/share/Panama}"
# panama-sudo is pkexec, and pkexec with nobody to answer it does not fail --
# it waits forever. That hung an unattended ./install at this very migration.
# So the stated-reason prompt is reserved for a terminal someone is sitting at;
# a non-interactive run takes plain sudo, which is either already authorized
# (a warm timestamp, NOPASSWD) or fails fast enough to be retried at the next
# login, exactly as panama-migrate promises.
sudo_cmd=(sudo)
if [[ -t 0 && -x "$PANAMA_PATH/bin/panama-sudo" ]]; then
    sudo_cmd=(
        "$PANAMA_PATH/bin/panama-sudo" --reason
        "Removing the unused solopasha/hyprland repository, which conflicts with the one Panama installs from"
        --
    )
fi

"${sudo_cmd[@]}" rm -f "$repo_file"
echo "Removed the disabled solopasha/hyprland COPR; Hyprland comes from lionheartp/Hyprland."
