#!/usr/bin/env bash
# perplexity-monitor-ingest-dir — watch a directory for new Perplexity
# markdown reports and import them into the precis-mcp store as $0
# cache entries (kind=research by default).
#
# Wrapper that runs under the precis-mcp package's local venv so the
# `precis` package is importable. Mirrors paper-monitor-ingest-dir.

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/_perplexity_monitor_ingest_dir.py" "$@"
fi

# Fallback: no local venv. Use `uv run` and let it sync deps on demand.
exec uv run --project "$PKG_DIR" python \
    "$SCRIPT_DIR/_perplexity_monitor_ingest_dir.py" "$@"
