load("@io_bazel_rules_go//go:def.bzl", "go_binary", "go_cross_binary", "go_library")
load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("@rules_pkg//pkg:mappings.bzl", "pkg_files")

go_library(
    name = "debugd_lib",
    srcs = ["debugd.go"],
    importpath = "github.com/edgelesssys/constellation/v2/debugd/cmd/debugd",
    visibility = ["//visibility:private"],
    deps = [
        "//debugd/internal/debugd/deploy",
        "//debugd/internal/debugd/info",
        "//debugd/internal/debugd/logcollector",
        "//debugd/internal/debugd/metadata",
        "//debugd/internal/debugd/metadata/cloudprovider",
        "//debugd/internal/debugd/metadata/fallback",
        "//debugd/internal/debugd/server",
        "//debugd/internal/filetransfer",
        "//debugd/internal/filetransfer/streamer",
        "//internal/cloud/aws",
        "//internal/cloud/azure",
        "//internal/cloud/cloudprovider",
        "//internal/cloud/gcp",
        "//internal/cloud/openstack",
        "//internal/cloud/qemu",
        "//internal/logger",
        "@com_github_spf13_afero//:afero",
        "@org_uber_go_zap//:zap",
    ],
)

go_binary(
    name = "debugd",
    embed = [":debugd_lib"],
    # keep
    pure = "on",
    visibility = ["//visibility:public"],
)

go_cross_binary(
    name = "debugd_linux_amd64",
    platform = "//bazel/platforms:go-pure_x86_64-linux",
    target = ":debugd",
    visibility = ["//visibility:public"],
)

pkg_files(
    name = "debugd_unit",
    srcs = glob(["service/**"]),
    renames = {
        "service/system-preset/20-debugd.preset": "usr/lib/systemd/system-preset/20-debugd.preset",
        "service/system/debugd.service": "usr/lib/systemd/system/debugd.service",
    },
)

pkg_tar(
    name = "debugd-package",
    srcs = [
        ":debugd_linux_amd64",
        ":debugd_unit",
    ],
    mode = "0755",
    remap_paths = {"/debugd_linux_amd64": "/usr/bin/debugd"},
    visibility = ["//visibility:public"],
)
