#!/usr/bin/env python3
"""CMake -> MSVC6 LINK.EXE shim.

Runs LINK.EXE through wibo. Compilation also uses wibo (see the `cl` wrapper).
"""
import os
import pathlib
import subprocess
import sys

ROOT = pathlib.Path(__file__).resolve().parents[2]
TC = ROOT / "toolchain"
WIBO = os.environ.get("WIBO") or "wibo"
LINK = str(TC / "msvc6" / "Bin" / "LINK.EXE")
LIBS = ROOT / "libs"

# LINK flags whose value is a path glued after a colon, e.g. /OUT:Z:\a\b.exe
COLON_FLAGS = ("/OUT:", "/PDB:", "/IMPLIB:", "/LIBPATH:", "/MAP:", "/DEF:")


def to_win(p: str) -> str:
    return "Z:" + p.replace("/", "\\")


def is_abs_path(tok: str) -> bool:
    if not tok.startswith("/"):
        return False
    first = tok[1:].split("/", 1)[0]
    return first != "" and os.path.isdir("/" + first)


def conv(tok: str) -> str:
    for f in COLON_FLAGS:
        if tok.upper().startswith(f):
            rest = tok[len(f) :]
            return tok[: len(f)] + (to_win(rest) if is_abs_path(rest) else rest)
    if is_abs_path(tok):
        return to_win(tok)
    return tok


def main() -> int:
    args = [conv(a) for a in sys.argv[1:]]
    import_libs = sorted(p.name for p in LIBS.glob("*.lib"))
    env = dict(os.environ)
    env["LIB"] = to_win(str(LIBS))
    return subprocess.call([WIBO, LINK, *args, *import_libs], env=env)


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