#!/bin/sh
# health-check.sh — the appliance watchdog wrapper (com.alexandria.health-check).
# Sources the off-repo secrets env (same search order as the other wrappers — SMTP creds are
# needed to send an alert), then runs the watchdog (alexandria_vault.engine.health_check) against this vault.
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; else
  echo "health-check.sh: no env file found — can't send alerts without SMTP creds." >&2
  exit 1
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.health_check --vault "$VAULT" "$@"
