#!/bin/sh
set -eu

. "$(git rev-parse --show-toplevel)/packages/tools/src/sh/env.sh"

NAME=$(jq -r '.name' package.json)

# Tuning knobs come from the same gitignored root .env a deploy reads (RECFLARE_<VAR>, see
# .env.example), so a knob is configured in exactly one place whether you're running locally
# or shipping. Unset knobs fall back to the worker's own default constants, same as a deploy.
#
# Passed as `--var` rather than left to wrangler's own .env loading: wrangler only reads a
# .env sitting next to the worker's wrangler.jsonc, which would mean a second file per app.
recflare_load_env
EXTRA_VARS=$(recflare_vars)

# The base domain isn't a tuning knob (recflare_vars skips the deploy inputs), so pass it
# the same way a deploy does. It matters locally for the workers that hand out addresses —
# `ns`, and the combined `mono` worker, whose discovery document IS the thing you point a
# client at. Unset, the worker keeps the placeholder default in its wrangler.jsonc; running
# mono behind a tunnel, set RECFLARE_DOMAIN to the hostname that reaches it and the document
# it serves names that host instead of a domain nothing here answers on.
DOMAIN_VAR=""
[ -z "${RECFLARE_DOMAIN:-}" ] || DOMAIN_VAR="--var DOMAIN:$RECFLARE_DOMAIN"

# Give each worker a stable, unique dev port so `turbo dev` can run them all in
# parallel without colliding on wrangler's default 8787 (and its 9229 inspector
# port). The offset is the worker's alphabetical position among its siblings, so
# it stays deterministic and needs no per-worker configuration.
OFFSET=$(
	for dir in ../*/; do
		[ -f "${dir}wrangler.jsonc" ] || continue
		jq -r '.name' "${dir}package.json"
	done | sort | grep -Fnx "$NAME" | cut -d: -f1
)
PORT=$((8787 + OFFSET - 1))
INSPECTOR_PORT=$((9229 + OFFSET - 1))

# $EXTRA_VARS and $DOMAIN_VAR are intentionally unquoted — they're flag lists to word-split
# on, and neither a knob value nor a domain contains whitespace.
exec wrangler dev \
	--var NAME:"$NAME" \
	$DOMAIN_VAR \
	$EXTRA_VARS \
	--port "$PORT" \
	--inspector-port "$INSPECTOR_PORT" \
	"$@"
