#!/bin/bash
# Elemental Arena — one-line puck installer (macOS / Linux).
#
# Point it at the arena you are pairing to, either as an argument or via the
# ARENA_BASE_URL environment variable:
#
#   curl -fsSL "$ARENA_HOST/static/flash/install.sh" | bash -s -- "$ARENA_HOST"
#   ARENA_BASE_URL="$ARENA_HOST" ./install.sh
#
# With neither, it assumes a local arena on http://localhost:8004.
#
# Plug your puck in over USB first (hold the BOOT button while connecting).
set -e

BASE_URL="${1:-${ARENA_BASE_URL:-http://localhost:8004}}"
case "$BASE_URL" in
  http://*|https://*) shift 2>/dev/null || true ;;
  *) BASE_URL="${ARENA_BASE_URL:-http://localhost:8004}" ;;
esac

echo "• Arena: $BASE_URL"
DIR="$(mktemp -d)"
echo "• Fetching the Elemental Arena flasher…"
curl -fsSL "$BASE_URL/static/flash/flash.py" -o "$DIR/flash.py"
echo "• Launching…"
ARENA_BASE_URL="$BASE_URL" python3 "$DIR/flash.py" "$@"
