#!/usr/bin/env bash
# Run scheduled batch profiling from the repo root so ``python-dotenv`` finds ``.env``.
#
# Cron examples (use your real paths; avoid relying on cron's minimal PATH):
#
#   0 2 * * * /path/to/Eatance-User-Profiling/scripts/run_batch_scheduled_profiling.sh >> /var/log/eatance-batch.log 2>&1
#
# Skip if a previous run is still holding the lock (requires ``flock`` from util-linux):
#
#   0 2 * * * flock -n /tmp/eatance-batch-profiling.lock /path/to/.../run_batch_scheduled_profiling.sh >> /var/log/eatance-batch.log 2>&1
#
set -euo pipefail

REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
cd "$REPO_ROOT"
export PYTHONUNBUFFERED=1

if command -v uv >/dev/null 2>&1; then
  exec uv run python batch_scheduled_profiling.py "$@"
elif [[ -x "${REPO_ROOT}/.venv/bin/python" ]]; then
  exec "${REPO_ROOT}/.venv/bin/python" batch_scheduled_profiling.py "$@"
else
  echo "run_batch_scheduled_profiling.sh: install uv or create .venv (see README)." >&2
  exit 127
fi
