#!/bin/sh
set -e

echo "[watcher] Starting inotify watch on /blog/src/ and /blog/astro.config.ts..."

DEBOUNCE_SECONDS=5

while true; do
  inotifywait -r -e modify,create,delete,move \
    /blog/src/ \
    /blog/astro.config.ts
  echo "[watcher] Change detected, debouncing for ${DEBOUNCE_SECONDS}s..."
  sleep "$DEBOUNCE_SECONDS"
  # Drain any remaining events that arrived during debounce
  while inotifywait -r -e modify,create,delete,move -t 0 \
    /blog/src/ \
    /blog/astro.config.ts 2>/dev/null; do
    echo "[watcher] Additional event during debounce, resetting..."
    sleep "$DEBOUNCE_SECONDS"
  done
  echo "[watcher] Building..."
  cd /blog && pnpm run build
  echo "[watcher] Build complete, watching for changes..."
done
