#!/usr/bin/env python3
"""Doxygen filter for plain-text resource files.

This filter renders non-code resources as file-level documentation with a
verbatim content block to keep parsing stable and avoid malformed symbols.
"""

from __future__ import annotations

import hashlib
import sys
from pathlib import Path


def escape_comment_end(text: str) -> str:
    return text.replace("*/", "* /")


def main() -> int:
    if len(sys.argv) != 2:
        return 1

    path = Path(sys.argv[1])
    try:
        lines = path.read_text(encoding="utf-8", errors="replace").splitlines()
    except OSError:
        return 1

    brief = f"Plain-text PEM resource file: {path.name}."

    digest = hashlib.md5(str(path).encode("utf-8")).hexdigest()[:10]

    print("/**")
    print(f" * @file {path.name}")
    print(f" * @brief {escape_comment_end(brief)}")
    print(" * @details")
    print(" * \\verbatim")
    for line in lines:
        print(f" * {escape_comment_end(line)}")
    print(" * \\endverbatim")
    print(" */")
    print(f"typedef int doxygen_text_file_{digest};")

    return 0


if __name__ == "__main__":
    raise SystemExit(main())
