#!/usr/bin/env python3
"""Print the path of a phase's plan file, as `.atm-bd/phase-<p>.toml` names it."""
import argparse
import subprocess
import sys
from pathlib import Path
from sprint_index_common import load_phase_config, phase_of_root


def main():
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--root")
    parser.add_argument("--phase")
    args = parser.parse_args()
    if not args.root and not args.phase:
        parser.error("pass --root or --phase")
    repo = Path(subprocess.check_output(["git", "rev-parse", "--show-toplevel"], text=True).strip())
    phase = args.phase or phase_of_root(args.root)
    print(repo / load_phase_config(repo, phase)["plan"])


if __name__ == "__main__":
    try:
        main()
    except (OSError, RuntimeError, ValueError, TypeError, subprocess.CalledProcessError) as exc:
        print(f"phase-index-path: {exc}", file=sys.stderr)
        raise SystemExit(2)
