#!/usr/bin/env bash
# Install the per-user Flatpaks listed in /usr/share/flatpak/flatpaks.list on
# the first login of each user. Run as a systemd --user oneshot unit.
set -euo pipefail

LIST="/usr/share/flatpak/flatpaks.list"
MARKER="${HOME}/.config/flatpak-user-firstboot.done"

[[ -f "${LIST}" ]] || exit 0

# Make Flathub available for the current user.
flatpak remote-add --user --if-not-exists flathub \
  https://flathub.org/repo/flathub.flatpakrepo

mapfile -t apps < <(
  sed -e 's/#.*//' -e 's/[[:space:]]//g' "${LIST}" | grep -v '^$' || true
)

if [[ ${#apps[@]} -gt 0 ]]; then
  flatpak install --user --noninteractive --assumeyes "${apps[@]}"
fi

install -dm0755 "$(dirname "${MARKER}")"
touch "${MARKER}"
