#!/usr/bin/env bash

# omarchy:summary=Install a Flathub app and launch it once it finishes
# omarchy:args=<display-name> <app-id>
# omarchy:examples=omarchy install flatpak-and-launch Spotify com.spotify.Client

# Based: omarchy-install-and-launch for the apps Debian does not carry and
# Omarchy's Arch repositories did. They come from Flathub into the system
# installation, the way Install > Flatpak adds them.
name="${1-}"
app_id="${2-}"

if [[ -z $name || -z $app_id ]]; then
  echo "Usage: omarchy-install-flatpak-and-launch <display-name> <app-id>" >&2
  exit 1
fi

printf -v install_message '%q' "Installing ${name}..."
printf -v app_id_arg '%q' "$app_id"

# The subshell keeps & from backgrounding the installation too.
exec omarchy-launch-floating-terminal-with-presentation \
  "echo ${install_message}; omarchy-pkg-flatpak-add ${app_id_arg} && (setsid uwsm-app -- flatpak run ${app_id_arg} >/dev/null 2>&1 &)"
