#!/bin/bash
# heartbeat-mirror.sh — 每 60 秒將本機各席位心跳 JSON mirror 去 server 8080 /heartbeat/
# （v13 新增；照主持機現行 heartbeat-mirror 做法入包。席位讀 env.sh 嘅 SEATS；
#   心跳檔喺 $ARENA_HOME/<席位>-heartbeat.json，由 watcher 見到 Stop 掣嗰刻寫入。）
set -uo pipefail
HERE="$(cd "$(dirname "$0")" && pwd)"
. "$HERE/env.sh"
while true; do
  for s in $SEATS; do
    f="$ARENA_HOME/$s-heartbeat.json"
    if [ -f "$f" ]; then
      "$SSH_WRAPPER" "cat > /root/public/heartbeat/$s-heartbeat.json.tmp && mv /root/public/heartbeat/$s-heartbeat.json.tmp /root/public/heartbeat/$s-heartbeat.json && chmod 644 /root/public/heartbeat/$s-heartbeat.json" < "$f" >/dev/null 2>&1 || true
    fi
  done
  sleep 60
done
