#!/usr/bin/env bash
# Schedule license heartbeat every 5 minutes (keeps cloud link + sync alive)
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
PYTHON="$ROOT/.venv/bin/python"
CRON_LINE="*/5 * * * * cd $ROOT && $PYTHON $ROOT/scripts/heartbeat_license.py --config $ROOT/deploy/license-node.json >> $ROOT/logs/heartbeat.log 2>&1"

if crontab -l 2>/dev/null | grep -F "heartbeat_license.py" >/dev/null; then
  echo "Heartbeat cron already installed."
  exit 0
fi

( crontab -l 2>/dev/null; echo "$CRON_LINE" ) | crontab -
echo "Installed cron heartbeat (every 5 min). Log: $ROOT/logs/heartbeat.log"
