#!/usr/bin/env bash
# Shiloh broadcaster — one-shot installer for the relay client.
#
# Usage:
#   curl -fsSL http://broadcaster.shilohbv.com/install.sh | bash
#
# Installs `shiloh-relay` to ~/.local/bin/ on Linux x86_64.
# Leaves systemd wiring to the operator (see pi-hp/README.md for the
# Pi/unit pattern).

set -euo pipefail

BASE_URL="${BASE_URL:-http://broadcaster.shilohbv.com}"
INSTALL_DIR="${INSTALL_DIR:-$HOME/.local/bin}"
BIN_NAME="shiloh-relay"

arch="$(uname -m)"
os="$(uname -s)"

if [[ "$os" != "Linux" ]]; then
  echo "install.sh: only Linux is supported here (got $os)"
  echo "macOS: build from source with 'cargo build --release -p shiloh-relay'"
  exit 1
fi

case "$arch" in
  x86_64|amd64)
    asset="shiloh-relay-linux-x86_64"
    ;;
  aarch64|arm64)
    asset="shiloh-relay-linux-aarch64"
    ;;
  *)
    echo "install.sh: unsupported arch '$arch'"
    exit 1
    ;;
esac

mkdir -p "$INSTALL_DIR"
dest="$INSTALL_DIR/$BIN_NAME"

echo "→ downloading $BASE_URL/dist/$asset"
curl -fsSL "$BASE_URL/dist/$asset" -o "$dest.tmp"
chmod +x "$dest.tmp"
mv -f "$dest.tmp" "$dest"

echo "→ installed: $dest"
"$dest" --version 2>/dev/null || "$dest" --help 2>/dev/null | head -1 || true

echo
echo "Next steps:"
echo "  1. Make sure $INSTALL_DIR is on your PATH."
echo "  2. Run: $BIN_NAME connect --server stg-srv001.bq.shilohbv.com:5005 --name \$(hostname)"
echo "  3. For a systemd user unit, see pi-hp/shiloh-relay.service as a template."
