#!/usr/bin/env python3
"""Start the real `viame`, with libpython findable.

Generated by cmake/wheel/build_wheel.py. See the note beside LAUNCHER there
for why this is not simply the binary.
"""
import os
import sys
import sysconfig


def main():
    real = os.path.join(sys.prefix, "libexec", "viame")

    if not os.path.exists(real):
        sys.exit("viame: {{}} is missing; the install is incomplete".format(real))

    # Where this interpreter keeps libpython. A standalone build keeps it to
    # itself, and the loader will not find it without being told.
    paths = []
    for var in ("LIBDIR", "LIBPL"):
        value = sysconfig.get_config_var(var)
        if value and os.path.isdir(value):
            paths.append(value)

    if paths:
        existing = os.environ.get("LD_LIBRARY_PATH", "")
        merged = [p for p in paths if p not in existing.split(os.pathsep)]
        if merged:
            os.environ["LD_LIBRARY_PATH"] = os.pathsep.join(
                merged + ([existing] if existing else []))

    try:
        os.execv(real, [real] + sys.argv[1:])
    except OSError as e:
        sys.exit("viame: cannot run {{}}: {{}}".format(real, e))


if __name__ == "__main__":
    main()
