#!/usr/bin/env python3
"""PreToolUse hook: 阻止读取 _archived/ 目录下的文件"""
import json, re, sys

try:
    data = json.load(sys.stdin)
except Exception:
    sys.exit(0)

tool = data.get("tool_name", "")
tool_input = data.get("tool_input") or {}

# 从不同工具中提取路径
path = ""
if tool in ("Read", "Edit", "Write"):
    path = tool_input.get("file_path", "")
elif tool == "Glob":
    path = tool_input.get("path", "")

path = path.replace("\\", "/")

if re.search(r"/_archived/|/_archived$|^_archived/", path) or re.search(
    r"[/\\]_archived[/\\]", tool_input.get("file_path", "")
):
    print(json.dumps({
        "decision": "block",
        "reason": "[archived-block] _archived/ 目录内容已废弃，禁止读取或参考。请使用当前版本的文件。"
    }))
