#!/bin/bash

# omarchy:summary=Restore the preinstalled Omarchy applications (web apps, TUIs, and selected packages).
# omarchy:requires-sudo=true

if gum confirm --affirmative "$(omarchy-i18n common.yes)" --negative "$(omarchy-i18n common.no)" "$(omarchy-i18n preinstalls.confirmRestore)"; then
  echo -e "Restoring preinstalled Omarchy applications...\n"

  # Recreates the shipped .desktop launchers (web apps and TUIs).
  omarchy-refresh-applications || exit 1

  # Re-enable Omarchy's default mise tools for this user and rebuild their shims.
  if ! omarchy-mise-default-tools enable; then
    echo -e "\nCould not restore mise tools. Preinstalls remain marked as removed."
    exit 1
  fi

  # Mirrors the list in omarchy-remove-preinstalls; both track omarchy-base.packages.
  # Only packages with Fedora RPMs are listed; the Flatpak-only apps
  # (obsidian and moonlight-qt) are restored
  # through their own channels (see FEDORA.md).
  if ! omarchy-pkg-add \
    libreoffice \
    pinta \
    hype \
    monologue \
    omacalc \
    omacut \
    omawrite \
    xournalpp; then
    echo -e "\nPreinstalls are still marked as removed. Fix the errors above and try again."
    exit 1
  fi

  # Last, so a failure above leaves the opt-out intact rather than restoring
  # keybindings for apps that never came back
  rm -f ~/.local/state/omarchy/preinstalls-removed
  hyprctl reload
fi
