#!/usr/bin/env bash
# paper-count — count papers in the precis-mcp store.
#
# Wrapper that runs under the precis-mcp package's local venv so the
# `[paper]` extras (acatome-extract + sentence-transformers) are
# available. Note: `uv run --project=<pkg>` would resolve to the
# workspace root's `.venv` instead, which does NOT carry the optional
# deps — so we invoke the package venv's interpreter directly.

set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
PKG_DIR="$(cd "$SCRIPT_DIR/.." && pwd)"
VENV_PY="$PKG_DIR/.venv/bin/python"

: "${PRECIS_DATABASE_URL:=postgresql://acatome:acatome@127.0.0.1:5432/precis}"
: "${PRECIS_EMBEDDER:=bge-m3}"
export PRECIS_DATABASE_URL PRECIS_EMBEDDER

if [[ -x "$VENV_PY" ]]; then
    exec "$VENV_PY" "$SCRIPT_DIR/_paper_count.py" "$@"
fi

# Fallback: no local venv. Use `uv run` and let it sync extras
# on demand. This is slower but gets new clones working.
exec uv run --project "$PKG_DIR" --extra paper python "$SCRIPT_DIR/_paper_count.py" "$@"
