#!/bin/sh
# Run the installed poppy CLI for the Claude Code plugin.
#
# Claude Code starts hooks and MCP servers with its own PATH, which often lacks
# the directory pipx, uv or pip put poppy in. Try PATH first, then the places
# those installers use by default. Exit 127 with a fix when poppy is not
# installed.
#
# Exit 2 from a hook blocks the prompt or tool call that fired it, and Click
# also exits 2 on a usage error, such as an older poppy that lacks a hook this
# plugin calls. So a hook's exit 2 becomes 1, which Claude Code reports
# without blocking.
poppy_bin=""
if command -v poppy >/dev/null 2>&1; then
  poppy_bin=poppy
else
  for candidate in \
    "${PIPX_BIN_DIR:+$PIPX_BIN_DIR/poppy}" \
    "${UV_TOOL_BIN_DIR:+$UV_TOOL_BIN_DIR/poppy}" \
    "${XDG_BIN_HOME:+$XDG_BIN_HOME/poppy}" \
    "$HOME/.local/bin/poppy" \
    "${PIPX_HOME:-$HOME/.local/share/pipx}/venvs/poppy-memory/bin/poppy" \
    "$HOME/.local/pipx/venvs/poppy-memory/bin/poppy" \
    "${UV_TOOL_DIR:-${XDG_DATA_HOME:-$HOME/.local/share}/uv/tools}/poppy-memory/bin/poppy" \
    "$HOME"/Library/Python/*/bin/poppy \
    /opt/homebrew/bin/poppy \
    /usr/local/bin/poppy; do
    if [ -n "$candidate" ] && [ -x "$candidate" ]; then
      poppy_bin=$candidate
      break
    fi
  done
fi
if [ -z "$poppy_bin" ]; then
  echo "Poppy plugin: the poppy command was not found. Install it with \`pipx install poppy-memory\` or \`uv tool install poppy-memory\`, then restart Claude Code." >&2
  exit 127
fi
if [ "${1:-}" != hook ]; then
  exec "$poppy_bin" "$@"
fi
# Some shells replace stdin before applying a background command's redirections.
# Save hook input on a separate descriptor so the child still receives it.
# Probe on a different descriptor: some shells treat 0<&0 as a no-op even when closed.
if ( exec 3<&0 ) 2>/dev/null; then
  exec 3<&0
else
  exec 3</dev/null
fi
child_pid=""
forward_signal() {
  if [ -n "$child_pid" ]; then
    kill -s "$1" "$child_pid" 2>/dev/null || :
  fi
}
trap 'forward_signal TERM' TERM
# Background commands inherit SIGINT ignored, and Python keeps it ignored.
# Forward Ctrl-C as TERM so it still stops the hook.
trap 'forward_signal TERM' INT
trap 'forward_signal HUP' HUP
"$poppy_bin" "$@" <&3 3<&- &
child_pid=$!
exec 3<&-
# A trapped signal interrupts wait before the child has necessarily stopped.
while :; do
  wait "$child_pid"
  status=$?
  kill -0 "$child_pid" 2>/dev/null || break
done
if [ "$status" -eq 2 ]; then
  exit 1
fi
exit "$status"
