load("@fbcode//registry:defs.bzl", "rpm")
load("@fbcode_macros//build_defs:export_files.bzl", "export_file")
load("@fbcode_macros//build_defs:python_binary.bzl", "python_binary")
load("@fbcode_macros//build_defs:python_library.bzl", "python_library")
load("@fbcode_macros//build_defs:python_unittest.bzl", "python_unittest")
load("@fbsource//tools/build_defs/testinfra:network_access_utils.bzl", "network_access_utils")

oncall("scm_client_infra")

# Just copy the pre-configured attributes so that we have
# know precisely what the characteristics will be for this build.
export_file(
    name = "config.h",
    src = "buck_config.h",
    out = "config.h",
    mode = "copy",
)

python_library(
    name = "watchman_version",
    srcs = ["watchman_version.py"],
    base_module = "",
    typing = True,
)

python_unittest(
    name = "test_watchman_version",
    srcs = ["test/test_watchman_version.py"],
    network_access = network_access_utils.none(),
    typing = True,
    deps = [
        ":watchman_version",
        "//libfb/py:testutil",
    ],
)

python_binary(
    name = "watchman-perf",
    srcs = {"watchman-perf.py": "watchman_perf.py"},
    base_module = "",
    labels = ["noautodeps"],
    main_function = "watchman_perf.main",
    typing = True,
)

python_unittest(
    name = "test_watchman_perf",
    srcs = [
        "test/test_watchman_perf.py",
    ],
    compatible_with = [
        "ovr_config//os:linux",
        "ovr_config//os:macos",
    ],
    labels = ["noautodeps"],
    network_access = network_access_utils.none(),
    typing = True,
    deps = [
        ":watchman-perf-library",
    ],
)

export_file(
    name = "watchman.json.default",
)

export_file(
    name = "watchman-perf.py",
)

rpm.builder(
    name = "fb-watchman",
    autoclean_keep_versions = 200,
    configurations = [
        rpm.configuration(
            arch = "x86_64",
        ),
        rpm.configuration(
            arch = "aarch64",
        ),
    ],
    features = [
        rpm.install(
            src = "fbcode//watchman:watchman",
            dst = "/usr/local/bin/watchman",
        ),
        rpm.install(
            src = "fbcode//watchman/cli:cli",
            dst = "/usr/local/bin/watchmanctl",
        ),
        rpm.install(
            src = "fbcode//watchman/python/bin:watchman-diag",
            dst = "/usr/local/bin/watchman-diag",
        ),
        rpm.install(
            src = "fbcode//watchman/python/bin:watchman-wait",
            dst = "/usr/local/bin/watchman-wait",
        ),
        rpm.install(
            src = "fbcode//watchman/python/bin:watchman-make",
            dst = "/usr/local/bin/watchman-make",
        ),
        rpm.install(
            src = "fbcode//watchman/python/bin:watchman-replicate-subscription",
            dst = "/usr/local/bin/watchman-replicate-subscription",
        ),
        rpm.install(
            src = ":watchman.json.default",
            dst = "/etc/watchman.json.default",
            mode = 0o0644,
        ),
        rpm.install(
            src = ":watchman-perf.py",
            dst = "/usr/local/sbin/watchman-perf.py",
            mode = 0o0755,
        ),
        rpm.ensure_dirs_exist(
            dirs = "/var/facebook/watchman",
            mode = 0o02777,
        ),
    ],
    override_log_paths = ["watchman"],
    summary = "Watch files, trigger stuff",
)
