#!/usr/bin/env bash
# Installs the default KAMOS Flatpak apps on first boot (runs once).
set -u

LIST=/usr/share/kamos/flatpaks.list
STAMP=/var/lib/kamos-flatpaks-done

[ -f "$STAMP" ] && exit 0

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

ok=1
while IFS= read -r app; do
    case "$app" in ''|\#*) continue ;; esac
    app="${app%%#*}"; app="$(echo "$app" | xargs)"
    [ -z "$app" ] && continue
    flatpak install --system --noninteractive --or-update flathub "$app" || ok=0
done < "$LIST"

# Only mark done when everything succeeded, so it retries next boot otherwise
[ "$ok" = 1 ] && touch "$STAMP"
exit 0
