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

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
REPO_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
CLIENT_DIR="$REPO_ROOT/apps/client"

WEB_HOST="${IOP_WEB_HOST:-0.0.0.0}"
WEB_PORT="${IOP_WEB_PORT:-13000}"

IOP_CONTROL_PLANE_HTTP_URL="${IOP_CONTROL_PLANE_HTTP_URL:-http://localhost:18000}"
IOP_CONTROL_PLANE_WIRE_URL="${IOP_CONTROL_PLANE_WIRE_URL:-ws://localhost:19080/client}"

cd "$CLIENT_DIR"
echo "[web] dir=$CLIENT_DIR"
echo "[web] listen=$WEB_HOST:$WEB_PORT"
echo "[web] control_plane_http=$IOP_CONTROL_PLANE_HTTP_URL"
echo "[web] control_plane_wire=$IOP_CONTROL_PLANE_WIRE_URL"

exec flutter run -d web-server \
  --web-hostname "$WEB_HOST" \
  --web-port "$WEB_PORT" \
  --dart-define=IOP_CONTROL_PLANE_HTTP_URL="$IOP_CONTROL_PLANE_HTTP_URL" \
  --dart-define=IOP_CONTROL_PLANE_WIRE_URL="$IOP_CONTROL_PLANE_WIRE_URL"
