#!/usr/bin/env bash
# First-boot installer for the GeForce NOW native Linux app (beta).
# GFN ships from NVIDIA's own flatpak repo, not Flathub, so the
# default-flatpaks module can't handle it. Retries while the network
# settles; stamps /var/lib/bc250/.gfn-installed on success so the
# service never runs again.

STAMP_DIR="/var/lib/bc250"
STAMP="$STAMP_DIR/.gfn-installed"
REPO_URL="https://international.download.nvidia.com/GFNLinux/flatpak/geforcenow.flatpakrepo"

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

for attempt in $(seq 1 30); do
    if flatpak remote-add --if-not-exists --system GeForceNOW "$REPO_URL" \
       && flatpak install --system --noninteractive -y GeForceNOW com.nvidia.geforcenow; then
        mkdir -p "$STAMP_DIR"
        touch "$STAMP"
        echo "GeForce NOW installed."
        exit 0
    fi
    echo "GeForce NOW install attempt $attempt failed; retrying in 60s..."
    sleep 60
done

echo "GeForce NOW install failed after 30 attempts; will retry next boot." >&2
exit 1
