#!/bin/sh
# Otty — Claude Code hook.
#
# Reports the agent's state to the Otty app over IPC so a terminal pane can
# show the processing / idle / awaiting-input badge. Otty registers this script
# in ~/.claude/settings.json (Settings → Agents → Install Hooks). It is shipped
# as a readable, code-signed file inside Otty.app so you can audit exactly what
# runs on every Claude hook event — Otty injects only the per-install paths.
#
# Args:
#   $1  state     — processing | idle | awaiting
#   $2  claude_pid — the Claude process's pid (its $PPID at hook time), used to
#                    detect --dangerously-skip-permissions ("bypass") mode
#   $3  "ctx"     — present only for PermissionRequest: forward the full hook
#                   stdin (base64) so Otty can offer auto-approve context
# Env:
#   OTTY_CLI — absolute path to the bundled otty-cli (injected by Otty; falls
#              back to the copy next to this script's bundle)
state="$1"
claude_pid="$2"
want_ctx="$3"

# Claude passes the hook payload as JSON on stdin.
input=$(cat)

# Prefer the env the Claude CLI exports; fall back to the stdin JSON.
sid="${CLAUDE_SESSION_ID:-$(printf '%s' "$input" | sed -n 's/.*"session_id"[[:space:]]*:[[:space:]]*"\([^"]*\)".*/\1/p' | head -n 1)}"

# Bypass-permissions mode is read from the Claude process's own argv.
bypass=0
ps -o args= -p "$claude_pid" 2>/dev/null | grep -qF -- '--dangerously-skip-permissions' && bypass=1

# Otty injects OTTY_CLI; the fallback locates otty-cli relative to this script
# inside the bundle (…/Contents/Resources/agent-integration/claude → …/MacOS).
cli="${OTTY_CLI:-$(CDPATH= cd -- "$(dirname -- "$0")/../../../MacOS" 2>/dev/null && pwd)/otty-cli}"

# Backgrounded so the hook never blocks Claude. Errors are intentionally
# swallowed: a missing Otty app must not break the user's Claude session.
if [ "$want_ctx" = "ctx" ]; then
    ctx=$(printf '%s' "$input" | base64)
    "$cli" state:claude session-id="$sid" state="$state" bypass="$bypass" context-b64="$ctx" 2>/dev/null &
else
    "$cli" state:claude session-id="$sid" state="$state" bypass="$bypass" 2>/dev/null &
fi
