#!/usr/bin/env python3
"""Shared entrypoint: dispatch a tool against a given deck without cd-ing into it.

    tools/mtg decks/reaper-king-no-more bracket_check
    tools/mtg decks/reaper-king-no-more combos --almost 30
    tools/mtg decks/reaper-king-no-more variant no-mikaeus --sim 24
    tools/mtg decks/reaper-king-no-more roles decks/no-mikaeus/decklist.txt

Usage: tools/mtg <deck_path> <tool_name> [tool args...]

<deck_path> is any deck directory, e.g. decks/reaper-king-no-more.
<tool_name> is a tools/*.py module name (with or without .py), or run_forge
for the shell script. Everything after it is passed through unchanged, with
--deck <deck_path> prepended so the underlying tool resolves the same deck.
"""
import os
import subprocess
import sys

TOOLS = os.path.dirname(os.path.abspath(__file__))


def main():
    args = sys.argv[1:]
    if len(args) < 2:
        sys.exit(__doc__)
    deck_path, tool = args[0], args[1]
    rest = args[2:]

    if tool in ("run_forge", "run_forge.sh"):
        cmd = ["bash", os.path.join(TOOLS, "run_forge.sh"), deck_path] + rest
    else:
        name = tool if tool.endswith(".py") else tool + ".py"
        script = os.path.join(TOOLS, name)
        if not os.path.exists(script):
            sys.exit(f"no such tool: {name} (looked in {TOOLS})")
        cmd = [sys.executable, script, "--deck", deck_path] + rest

    return subprocess.call(cmd)


if __name__ == "__main__":
    sys.exit(main())
