#!/bin/sh
# Discord from Flathub, for panels that ask for it (kiosks[].enable_discord).
#
# Gated rather than unconditional: a kitchen panel does not want a chat client, and
# the Loggia one is the whole reason this exists (see configs/discord/discord-launch).
# ENABLE_DISCORD comes from /etc/touchpanel-agent/config.env, which live-build has
# already copied in before hooks run — the same mechanism 0300-flatpak-spotify uses
# for nothing at all today, and the reason that file is written first.
set -eu

ENABLE_DISCORD=false
if [ -r /etc/touchpanel-agent/config.env ]; then
  # shellcheck disable=SC1091
  . /etc/touchpanel-agent/config.env
fi

if [ "${ENABLE_DISCORD:-false}" != "true" ]; then
  echo "0350-flatpak-discord: ENABLE_DISCORD is not true — skipping."
  exit 0
fi

flatpak remote-add --if-not-exists flathub https://dl.flathub.org/repo/flathub.flatpakrepo

# VERIFY BEFORE THE FIRST REAL BUILD: confirm this application ID against the live
# Flathub listing (`flatpak search Discord`). Believed correct (Discord's own
# published app) but not checked from this environment — same honesty as the Spotify
# hook next door.
DISCORD_APP_ID="com.discordapp.Discord"

if flatpak install -y --noninteractive flathub "$DISCORD_APP_ID"; then
  echo "0350-flatpak-discord: installed ${DISCORD_APP_ID}."
else
  echo "0350-flatpak-discord: WARNING — could not install ${DISCORD_APP_ID} during the"
  echo "  build (no network in the chroot, or the app ID is wrong). Run this on the"
  echo "  booted image instead:  flatpak install -y flathub ${DISCORD_APP_ID}"
  echo "  The Discord workspace will be empty until this is done."
fi
