"""CLI entry point for launching commands with the Anthropic API fetch interceptor.

Usage: ddapm-test-agent-run <command> [args...]

- For "claude": sets BUN_OPTIONS with --preload pointing to claude_intercept.mjs and
  exec's the claude binary from PATH (e.g. Homebrew Cask). Works with Bun-based Claude Code.
- For other commands: sets NODE_OPTIONS with --import pointing to claude_intercept.mjs
  and exec's the given command (for Node-based tools).
"""
import os
import shutil
import sys


def main() -> None:
    if len(sys.argv) < 2:
        print("Usage: ddapm-test-agent-run <command> [args...]", file=sys.stderr)
        sys.exit(1)

    mjs_path = os.path.join(os.path.dirname(os.path.abspath(__file__)), "claude_intercept.mjs")
    command = sys.argv[1]

    if command == "claude":
        claude_bin = shutil.which("claude")
        if not claude_bin:
            print("[ddapm] 'claude' not found in PATH", file=sys.stderr)
            sys.exit(1)
        existing = os.environ.get("BUN_OPTIONS", "")
        os.environ["BUN_OPTIONS"] = f"--preload {mjs_path} {existing}".strip()
        os.execv(claude_bin, [claude_bin] + sys.argv[2:])
    else:
        existing = os.environ.get("NODE_OPTIONS", "")
        os.environ["NODE_OPTIONS"] = f"--import {mjs_path} {existing}".strip()
        os.execvp(command, sys.argv[1:])
