#!/usr/bin/env python3

"""
Runs Drake Visualizer with builtin scripts under an install tree.

Note that it is not entirely important that this script's Python interpreter
match that of the real `drake-visualizer` binary.
"""

from os.path import isdir, join, dirname, realpath
import sys

if sys.platform == "darwin":
    # Drake Visualizer is disabled for at least as long as it takes to
    # transition Drake and VTK 8.2 to Python 3.9 or some alternative solution
    # is found, see https://github.com/RobotLocomotion/drake/issues/14650 for
    # updates.
    sys.exit("ERROR: Drake Visualizer is disabled on macOS, see "
             "https://git.io/Jt1BG for updates")

# Ensure that we can import the necessary scripts, accommodating symlinks.
PREFIX_DIR = dirname(dirname(realpath(__file__)))
assert isdir(join(PREFIX_DIR, "bin")), f"Bad location: {PREFIX_DIR}"
# Add the necessary Python site-packages path that is congruent with this
# binary.
# N.B. We do not need to modify the PYTHONPATH environment variable, as the
# real drake-visualizer handles this on its own.
site_dir = join(PREFIX_DIR, "@PYTHON_SITE_PACKAGES_RELPATH@")
sys.path.insert(0, site_dir)

from _drake_visualizer_builtin_scripts import (  # noqa
    _exec_drake_visualizer_with_plugins,
)


def main():
    # Execute wrapper.
    _exec_drake_visualizer_with_plugins(
        drake_visualizer_real=join(PREFIX_DIR, "bin/.drake-visualizer-real"),
        # Ensure the wrapped binary shows 'drake-visualizer' in its usage.
        arg0=__file__,
    )


assert __name__ == "__main__"
main()
