#!/usr/bin/env bash
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SPROUTCLAW_DIR="${SPROUTCLAW_DIR:-$(cd "$SCRIPT_DIR/../sproutclaw" && pwd)}"
BACKEND_PORT="${BACKEND_PORT:-19133}"
AGENT_DIR="${SPROUTCLAW_AGENT_DIR:-$SPROUTCLAW_DIR/.sproutclaw/agent}"
PI_CMD="${SPROUTCLAW_CMD:-node $SPROUTCLAW_DIR/packages/coding-agent/dist/cli.js}"

cd "$SCRIPT_DIR/backend"
echo "[run-backend] port=$BACKEND_PORT agent-dir=$AGENT_DIR"
exec go run ./cmd/server \
  --port "$BACKEND_PORT" \
  --agent-dir "$AGENT_DIR" \
  --repo-root "$SPROUTCLAW_DIR" \
  --pi-cmd "$PI_CMD" \
  --frontend-dist "$SCRIPT_DIR/frontend/dist" \
  --data-dir "$SCRIPT_DIR/backend/data"
