#!/usr/bin/env python3
# pylint: disable=broad-exception-caught,duplicate-code
"""
Claude Code Hook: Check.sh Validator
=====================================
This hook ensures ./check.sh is run without any additional arguments or pipes.
It runs as a PreToolUse hook for the Bash tool.

Configuration in settings.local.json:
{
  "hooks": {
    "PreToolUse": [
      {
        "matcher": "Bash",
        "hooks": [
          {
            "type": "command",
            "command": "python3 /Users/laurynas/dotfiles/.claude/hooks/validate-check-sh.py"
          }
        ]
      }
    ]
  }
}
"""

import json
import re
import sys


def main():
    """Entry point for the check.sh validation hook."""
    try:
        input_data = json.load(sys.stdin)
    except Exception as e:
        print(f"Error: Invalid JSON input: {e}", file=sys.stderr)
        # Exit code 1 shows stderr to the user but not to Claude
        sys.exit(1)

    tool_name = input_data.get("tool_name", "")
    if tool_name != "Bash":
        sys.exit(0)

    tool_input = input_data.get("tool_input", {})
    command = tool_input.get("command", "")

    if not command:
        sys.exit(0)

    # Match ./check.sh as a whole token (followed by EOL or a shell separator),
    # so unrelated commands like ./check.shutdown are not flagged.
    if (
        re.match(r"^\./check\.sh($|[\s|&;<>])", command)
        and command.strip() != "./check.sh"
    ):
        print(
            "./check.sh must be run as-is: "
            "no args, redirects, or pipes to grep, tail, etc.",
            file=sys.stderr,
        )
        # Exit code 2 blocks tool call and shows stderr to Claude
        sys.exit(2)

    sys.exit(0)


if __name__ == "__main__":
    main()
