#!/bin/bash # auto-rebuild-watcher.sh — inotifywait daemon for garden content/ changes # # Runs as a systemd service (garden-content-watcher.service). # Watches ~/projects/garden/content/ recursively and triggers garden-build.sh # on file create/modify/delete/move events. # # Debounce: waits 3 seconds after the last event before triggering rebuild, # so rapid batch edits (e.g. `git pull`, bulk copy) cause one rebuild, not N. set -euo pipefail CONTENT_DIR="$HOME/projects/garden/content" BUILD_SCRIPT="$HOME/pulse/garden-build.sh" if [ ! -d "$CONTENT_DIR" ]; then echo "✗ Content dir not found: $CONTENT_DIR" exit 1 fi if [ ! -x "$BUILD_SCRIPT" ]; then echo "✗ Build script not found/executable: $BUILD_SCRIPT" exit 1 fi echo "=== Garden content watcher starting at $(date -u) ===" echo " Watching: $CONTENT_DIR" echo " Rebuild: $BUILD_SCRIPT" # Initial rebuild on startup (content may have changed while watcher was down) bash "$BUILD_SCRIPT" # Monitor with inotifywait — recursive, all file events in content/ inotifywait -m -r -e modify -e create -e delete -e move \ --format '%e %w%f' \ "$CONTENT_DIR" 2>/dev/null | while IFS= read -r line; do EVENT_TYPE="${line%% *}" FILE_PATH="${line#* }" # Debounce flag: touch a temp flag, sleep 3 seconds, then rebuild if # the flag is still there (no newer event removed it) FLAG="${HOME}/projects/garden/.rebuild-pending" touch "$FLAG" sleep 3 if [ -f "$FLAG" ]; then rm -f "$FLAG" echo "→ Content change detected: $EVENT_TYPE ${FILE_PATH##$HOME/projects/garden/content/}" bash "$BUILD_SCRIPT" fi done