#!/usr/bin/env bash
# Launches the official, free Raspberry Pi Imager pointed at Mishigami
# GMRS's own custom OS list, so "Mishigami GMRS Node" shows up as a
# pickable image right inside that same trusted, actively-maintained tool
# — see node/image/README.md for why this project reuses Imager instead of
# shipping its own imaging software. Linux counterpart of
# node/image/windows/launch-gmrs-imager.bat — same idea, same --repo flag
# (rpi-imager is the same Qt application on every platform).
#
# This script does nothing privileged itself — it only locates an
# already-installed, already-trusted rpi-imager and launches it with one
# argument. No raw disk access, no elevation, nothing here needs signing.
set -euo pipefail

REPO_URL="https://node.mishigamigmrs.com/image/os-list.json"

if command -v rpi-imager >/dev/null 2>&1; then
    exec rpi-imager --repo "$REPO_URL"
fi

# Flatpak installs (flathub org.raspberrypi.rpi-imager) don't put a
# `rpi-imager` binary on PATH — they're launched through `flatpak run`.
if command -v flatpak >/dev/null 2>&1 \
        && flatpak list --app --columns=application 2>/dev/null | grep -qx org.raspberrypi.rpi-imager; then
    exec flatpak run org.raspberrypi.rpi-imager --repo "$REPO_URL"
fi

cat <<'EOF'
Couldn't find Raspberry Pi Imager on this system.

Install it with whichever of these works on your distro, then run this
script again:

  Debian / Ubuntu (and derivatives):
    sudo apt install rpi-imager

  Flatpak (any distro):
    flatpak install flathub org.raspberrypi.rpi-imager

  Or download it directly from the official site:
    https://www.raspberrypi.com/software/
EOF

xdg-open https://www.raspberrypi.com/software/ >/dev/null 2>&1 &
exit 1
