#!/bin/bash
set -euo pipefail

PROJECT_ROOT="/root/sourceserver/weather"
PYTHON_BIN="$PROJECT_ROOT/.venv/bin/python"
LOCK_FILE="/tmp/weather-display-refresh.lock"
LOG_FILE="/var/log/weather-display-refresh.log"

mkdir -p "$(dirname "$LOG_FILE")"

{
  echo "==== $(date '+%Y-%m-%d %H:%M:%S') display refresh start ===="
  flock -n 9 || {
    echo "refresh skipped: previous run still active"
    exit 0
  }
  cd "$PROJECT_ROOT"
  "$PYTHON_BIN" -m src.display.scheduled_refresh
  echo "==== $(date '+%Y-%m-%d %H:%M:%S') display refresh done ===="
} 9>"$LOCK_FILE" >>"$LOG_FILE" 2>&1
