#!/usr/bin/env bash
# ============================================================================
# dark-agents CLI launcher (macOS / Linux)
# ----------------------------------------------------------------------------
# Sets DARK_AGENTS_SERVER to the public API Gateway and forwards all args
# to the dark-agents binary. The binary itself ships with NO
# operator-specific infrastructure baked in.
#
# Usage:
#   ./dark-agents /help
#   ./dark-agents /connect DARK-XXXX-XXXX-XXXX-XXXX-XXXX-XXXX
#   ./dark-agents /whoami
#
# To override the server URL, export DARK_AGENTS_SERVER in your shell
# BEFORE calling this launcher (the env var in the calling shell wins).
# ============================================================================

set -e

# Resolve the directory this script lives in (works on macOS and Linux).
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"

# Pick the binary that matches the current platform.
case "$(uname -s)" in
  Darwin)
    case "$(uname -m)" in
      arm64|aarch64) BIN="$SCRIPT_DIR/dark-agents-darwin-arm64" ;;
      x86_64)        BIN="$SCRIPT_DIR/dark-agents-darwin-x64"   ;;
      *) echo "Unsupported macOS arch: $(uname -m)" >&2; exit 1 ;;
    esac
    ;;
  Linux)
    BIN="$SCRIPT_DIR/dark-agents-linux-x64"
    ;;
  *)
    echo "Unsupported OS: $(uname -s)" >&2
    exit 1
    ;;
esac

if [ ! -x "$BIN" ]; then
  echo "dark-agents binary not found or not executable: $BIN" >&2
  echo "Run: chmod +x $BIN" >&2
  exit 1
fi

# Set the server URL only if not already set by the caller.
export DARK_AGENTS_SERVER="${DARK_AGENTS_SERVER:-https://bxvjzjlz32.execute-api.us-east-1.amazonaws.com}"

exec "$BIN" "$@"
