#!/bin/sh

HEARTBEAT_FILE="/opt/homelab/state/stability-agent.heartbeat"
MAX_AGE_SECONDS=300 # 5 minutes

if [ ! -f "$HEARTBEAT_FILE" ]; then
    echo "Heartbeat file missing"
    exit 1
fi

# Get current time in seconds
NOW=$(date +%s)

# Get file modification time in seconds
# Busybox stat (standard in alpine/slim) uses -c %Y
FILE_TIME=$(stat -c %Y "$HEARTBEAT_FILE")

AGE=$((NOW - FILE_TIME))

if [ "$AGE" -gt "$MAX_AGE_SECONDS" ]; then
    echo "Heartbeat is too old: ${AGE}s"
    exit 1
fi

exit 0
