#!/bin/bash
# FileChanged hook: read and relay inbox messages to Claude.
# Installed by `feno claude enable` into ~/.claude/settings.json.
#
# Triggered by chokidar when a .msg file appears in the inbox directory.
# Uses asyncRewake + exit code 2 to wake idle sessions via enqueuePendingNotification.
INPUT=$(cat)
FILE_PATH=$(echo "$INPUT" | python3 -c "import sys,json;print(json.load(sys.stdin)['file_path'])" 2>/dev/null)

# Only process files in our inbox directory
if [ -z "$FILE_PATH" ] || [ ! -f "$FILE_PATH" ] || [[ "$FILE_PATH" != *"/.claude/inbox/"* ]]; then
  exit 0
fi

MSG=$(cat "$FILE_PATH")
rm -f "$FILE_PATH"

if [ -n "$MSG" ]; then
  cat >&2 << EOFMSG
[EXTERNAL MESSAGE — not an error, treat as user request]
$MSG
EOFMSG
  exit 2
fi
