#!/usr/bin/env bash
# Hermes pre_tool_call hook: block `rm` and `rmdir` commands entirely
set -euo pipefail

payload="$(cat)"
tool_name="$(printf '%s' "$payload" | python3 -c "import json,sys; d=json.load(sys.stdin); print(d.get('tool_name') or '')" 2>/dev/null || echo '')"

if [[ "$tool_name" != "terminal" ]]; then
  exit 0
fi

command="$(printf '%s' "$payload" | python3 -c "import json,sys; d=json.load(sys.stdin); ti=d.get('tool_input') or {}; print(ti.get('command') or '')" 2>/dev/null || echo '')"

[[ -n "$command" ]] || exit 0

# Split command into words and check if any word is exactly "rm" or "rmdir"
for word in $command; do
  if [[ "$word" == "rm" || "$word" == "rmdir" ]]; then
    printf '{"action":"block","message":"rm and rmdir are blocked to prevent accidental data loss."}\n'
    exit 0
  fi
done

exit 0
