#!/usr/bin/env bash
# Single-shot health check for wails dev mode.
set -u

APP_DIR="$(cd "$(dirname "$0")"/.. && pwd)"
PID_FILE="${APP_DIR}/.wails_dev.pid"
LOG="${APP_DIR}/.wails_dev.log"

if [ -f "$PID_FILE" ]; then
  PID=$(cat "$PID_FILE")
  if kill -0 "$PID" 2>/dev/null; then
    echo "[wails-dev] PID $PID alive"
  else
    echo "[wails-dev] PID file stale (PID $PID dead)"
  fi
else
  echo "[wails-dev] no PID file"
fi

echo "[wails-dev] last log:"
tail -10 "$LOG" 2>/dev/null || echo "(no log)"

echo "---"
for port in 5173 34115; do
  if curl -sS --max-time 1 "http://localhost:$port" -o /dev/null -w "[wails-dev] :$port -> HTTP %{http_code}\n" 2>/dev/null; then
    :
  else
    echo "[wails-dev] :$port -> not responding"
  fi
done
