#!/bin/bash
# Watch data.json files and auto-rebuild HTML embeds
# Optional fallback if file:// workflow is preferred
cd /home/isthekid/.openclaw/workspace

# Check if inotify-tools is installed
if ! command -v inotifywait &> /dev/null; then
  echo "❌ inotify-tools not installed"
  echo "Install with: sudo apt update && sudo apt install -y inotify-tools"
  exit 1
fi

echo "👀 Watching for changes to dashboard/data.json, kanban/kanban-state.json, and action/action-state.json"
echo "Press Ctrl+C to stop"

while true; do
  inotifywait -q -e modify,close_write dashboard/data.json kanban/kanban-state.json action/action-state.json 2>/dev/null
  echo "→ Detected change at $(date '+%H:%M:%S'), rebuilding..."
  
  python3 dashboard/build-dashboard-embed.py 2>&1 | sed 's/^/  [dashboard] /'
  python3 kanban/build-kanban-data.py 2>&1 | sed 's/^/  [kanban] /'
  python3 action/build-action-data.py 2>&1 | sed 's/^/  [action] /'
  
  echo "✓ Rebuilt at $(date '+%H:%M:%S')"
  echo "---"
done
