#!/usr/bin/env bash
set -euo pipefail

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
PHP_BIN="${PHP_BIN:-php}"

EMAIL_TO="${CRON_HEALTH_EMAIL_TO:-}"
MAX_MINUTES="${CRON_HEALTH_MAX_MINUTES:-}"
FROM_ADDR="${CRON_HEALTH_FROM:-}"

ARGS=()
if [[ -n "${EMAIL_TO}" ]]; then
  ARGS+=("--email=${EMAIL_TO}")
fi
if [[ -n "${MAX_MINUTES}" ]]; then
  ARGS+=("--max-minutes=${MAX_MINUTES}")
fi
if [[ -n "${FROM_ADDR}" ]]; then
  ARGS+=("--from=${FROM_ADDR}")
fi

"${PHP_BIN}" "${ROOT_DIR}/scripts/cron-health-check.php" "${ARGS[@]}"
