# -*- python -*-

load("//tools/skylark:drake_py.bzl", "drake_py_unittest")
load("//tools/skylark:py.bzl", "py_library")
load("//tools/lint:lint.bzl", "add_lint_tests")
load("@python//:version.bzl", "PYTHON_SITE_PACKAGES_RELPATH")
load("@drake//tools/install:install.bzl", "install", "install_files")
load(
    "@drake//tools/workspace:cmake_configure_file.bzl",
    "cmake_configure_file",
)
load("//tools/skylark:test_tags.bzl", "vtk_test_tags")

exports_files(
    # To be consumed by `//tools:drake_visualizer_py`.
    ["drake_visualizer_bazel.py"],
    visibility = ["//tools:__pkg__"],
)

# TODO(eric.cousineau): Merge this with `drake_visualizer_py` once it moves
# from `//tools` to here.
py_library(
    name = "builtin_scripts_py",
    visibility = ["//tools:__pkg__"],
    deps = [
        "//tools/workspace/drake_visualizer/_drake_visualizer_builtin_scripts",
    ],
)

# When developing within Drake, we don't want //tools:drake_visualizer to
# depend on the entire pydrake library, because that adds a dependency on a
# whole bunch of C++ code that is irrelevant for the visualizer.  So, here we
# provide a small stub version of pydrake with only getDrakePath implemented
# (the only piece drake_visualizer needs).  Note that the installed version of
# drake-visualizer *does* use the installed (full) version of pydrake -- this
# is development-sandbox-only stub.
py_library(
    name = "stub_pydrake",
    srcs = ["stub/pydrake/__init__.py"],
    visibility = ["//tools:__pkg__"],
)

# TODO(eric.cousineau): Use something simpler than cmake_configure_file to do
# basic subsitutions as a genrule.
cmake_configure_file(
    name = "config_wrapper_script",
    src = "drake_visualizer_installed.py.in",
    out = "drake_visualizer_installed.py",
    defines = [
        "PYTHON_SITE_PACKAGES_RELPATH=" + PYTHON_SITE_PACKAGES_RELPATH,
    ],
)

install_files(
    name = "install_wrapper_script",
    dest = "bin",
    files = ["drake_visualizer_installed.py"],
    rename = {
        "bin/drake_visualizer_installed.py": "drake-visualizer",
    },
)

install(
    name = "install",
    install_tests = [":test/drake_visualizer_installed_test.py"],
    visibility = ["//tools/workspace:__pkg__"],
    deps = [
        ":install_wrapper_script",
        "//tools/workspace/drake_visualizer/_drake_visualizer_builtin_scripts:install",  # noqa
        "@drake_visualizer//:install",
    ],
)

drake_py_unittest(
    name = "drake_visualizer_bazel_test",
    data = [
        "//tools:drake_visualizer",
    ],
    tags = vtk_test_tags(),
)

add_lint_tests(
    python_lint_extra_srcs = [
        "drake_visualizer_installed.py",
    ],
)
