#!/usr/bin/env bash
# scripts/dev — run a command inside the precis-dev container.
#
# Host has Git + Docker + the IDE. Every other dev tool (pytest, ruff,
# mypy, uv, plantuml, psql, …) lives inside the precis-dev container.
# This wrapper is the standard way to invoke them.
#
# Usage:
#   scripts/dev                         # interactive bash shell
#   scripts/dev pytest tests/           # run a single tool
#   scripts/dev uv run ruff check .     # uv-driven invocation
#   scripts/dev bash -lc 'ruff && mypy && pytest'   # compound check
#
# The container bind-mounts this repo at /app, so file edits on the host
# are visible inside the container immediately (no rebuild needed).
#
# See: docs/decisions/0009-dockerfile-relocation-container-first.md
set -euo pipefail

cd "$(dirname "$0")/.."
WORKTREE="$PWD"

INFRA_COMPOSE="${PRECIS_COMPOSE:-${PWD}/docker/dev/compose.yaml}"
if [[ ! -f "${INFRA_COMPOSE}" ]]; then
    echo "ERR: compose file not found at ${INFRA_COMPOSE}" >&2
    echo "     set PRECIS_COMPOSE=/path/to/compose.yaml to override" >&2
    exit 1
fi

# Per-worktree compose project so this worktree's one-off run doesn't collide
# with every sibling's on the default project name `dev` (gr176375's fix for
# scripts/test/scripts/ship, extended here — gr311857). See
# scripts/lib/compose-project.sh.
source "${WORKTREE}/scripts/lib/compose-project.sh"
COMPOSE_PROJECT="$(compose_project_for "$WORKTREE")"

# `--no-deps` skips bringing up any depends_on services. precis-dev has
# none right now, but make it explicit so we never accidentally spin up
# heavier siblings (acatome-watch, corpus-server).
exec docker compose -f "${INFRA_COMPOSE}" -p "${COMPOSE_PROJECT}" --profile dev run --rm --no-deps -v "${WORKTREE}":/app precis-dev "$@"
