#!/usr/bin/env bash
# Register local server with CNP cloud hub (set license_key in deploy/license-node.json first)
set -euo pipefail

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
ROOT="${INSTALL_ROOT:-$(cd "$SCRIPT_DIR/../.." && pwd)}"
CONFIG="$ROOT/deploy/license-node.json"
PYTHON="$ROOT/.venv/bin/python"

[[ -f "$CONFIG" ]] || { echo "Missing deploy/license-node.json — run install-cnperp-local.sh first."; exit 1; }
[[ -x "$PYTHON" ]] || PYTHON=python3

if ! grep -q '"license_key"[[:space:]]*:[[:space:]]*"[^"]\+' "$CONFIG"; then
  echo "Add your CNP license key to deploy/license-node.json, then run again."
  exit 1
fi

"$PYTHON" "$ROOT/scripts/heartbeat_license.py" --config "$CONFIG"
echo "Cloud link registered. LAN transactions sync to the online hub in near real time."
echo "Optional cron: $SCRIPT_DIR/setup_license_heartbeat_cron.sh"
