#!/bin/sh
# Vault hourly sweep wrapper. Sources the off-repo secrets env (so the email/Claude tasks
# have credentials), then runs the sweep engine
# (alexandria_vault.engine.sweep, from the installed package) against this vault. Missing env is non-fatal
# (location / --plan still work; only the email tasks need credentials).
#   Override the env file with ALEXANDRIA_ENV=/path/to/alexandria.env
#   Search order (most findable first):
#     1. $ALEXANDRIA_ENV
#     2. ~/Library/Application Support/Alexandria/alexandria.env  <- default (via `alexandria secrets --set`)
#     3. ~/Alexandria-secrets/alexandria.env   <- legacy
#     4. ~/.config/alexandria/alexandria.env   <- legacy
ENVFILE=""
for f in "$ALEXANDRIA_ENV" "$HOME/Library/Application Support/Alexandria/alexandria.env" "$HOME/Alexandria-secrets/alexandria.env" "$HOME/.config/alexandria/alexandria.env"; do
  if [ -n "$f" ] && [ -f "$f" ]; then ENVFILE="$f"; break; fi
done
if [ -n "$ENVFILE" ]; then set -a; . "$ENVFILE"; set +a; fi
DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
# The engine lives in the installed alexandria-vault package (docs/DEVAULT.md step 3),
# not in the vault: run it from $ALEXANDRIA_PYTHON if set (e.g. in alexandria.env), else
# the app venv the installer maintains, else whatever python3 has the package.
VAULT=$(CDPATH= cd -- "$DIR/../../.." && pwd)
APP_PY="$HOME/Library/Application Support/Alexandria/app/.venv/bin/python3"
if [ -n "$ALEXANDRIA_PYTHON" ]; then PY="$ALEXANDRIA_PYTHON"
elif [ -x "$APP_PY" ]; then PY="$APP_PY"
else PY="python3"; fi
exec "$PY" -m alexandria_vault.engine.sweep --vault "$VAULT" "$@"
