#!/usr/bin/env bash
# run a single command once per save in a subfolder

WATCH="/home/zaine/master-folder/org_files/org_web/lima"   # subfolder to watch
RUN_IN="/home/zaine/master-folder/org_files/org_web"       # run command from here

# pass the command to run as args:
#   ./watch.sh emacs -Q --script build-site.el
[[ $# -gt 0 ]] || { echo "Usage: $0 <command to run>"; exit 1; }
CMD=("$@")

# ignore common temp/swap files
EXCLUDE='(^|/)(#.*#|\.#.*|.*~|.*\.tmp|.*\.sw[pox])($|/)'

inotifywait -m -r -q \
  -e close_write,moved_to \
  --exclude "$EXCLUDE" \
  --format 'Triggered by: %w%f' \
  "$WATCH" |
while read -r path; do
  echo "$path"
  ( cd "$RUN_IN" && "${CMD[@]}" )
done
