load("@bazel_skylib//rules:write_file.bzl", "write_file")
load("@container_structure_test//:defs.bzl", "container_structure_test")
load(
    "@rules_oci//oci:defs.bzl",
    "oci_image",
    "oci_image_index",
    "oci_push",
    "oci_tarball",
)
load("//:rabbitmq.bzl", "APP_VERSION")

filegroup(
    name = "context-files",
    srcs = [
        "10-defaults.conf",
        "20-management_agent.disable_metrics_collector.conf",
        "Dockerfile",
        "docker-entrypoint.sh",
        "//:package-generic-unix",
    ],
)

_ARCHS = [
    "amd64",
    "arm64",
]

_TAGS = [
    "docker",
    "manual",
    "no-sandbox",
    "no-remote-exec",  # buildbuddy runners do not have the emulator available
]

[
    genrule(
        name = "docker-build-%s" % arch,
        srcs = [
            ":context-files",
        ],
        outs = [
            "image-%s.tar" % arch,
        ],
        cmd = """set -euo pipefail

CONTEXT="$$(mktemp -d)"

cp $(locations :context-files) "$$CONTEXT"

docker buildx \\
    build \\
    "$$CONTEXT" \\
    --platform linux/{arch} \\
    --build-arg RABBITMQ_VERSION="{rmq_version}" \\
    --output type=tar,dest=$(location image-{arch}.tar) $${{EXTRA_BUILDX_OPTS:-}}
""".format(
            arch = arch,
            rmq_version = APP_VERSION,
        ),
        tags = _TAGS,
    )
    for arch in _ARCHS
]

write_file(
    name = "cmd",
    out = "cmd.txt",
    # must match Dockerfile
    content = ["rabbitmq-server"],
)

write_file(
    name = "entrypoint",
    out = "entrypoint.txt",
    # must match Dockerfile
    content = ["docker-entrypoint.sh"],
)

[
    oci_image(
        name = "image-%s" % arch,
        architecture = arch,
        cmd = ":cmd",
        entrypoint = ":entrypoint",
        # must match Dockerfile
        # docker inspect bazel/packaging/docker-image:rabbitmq-amd64
        # after
        # bazel run //packaging/docker-image:rabbitmq-amd64
        # to check values
        env = {
            "PATH": "/opt/rabbitmq/sbin:/opt/erlang/bin:/opt/openssl/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin",
            "ERLANG_INSTALL_PATH_PREFIX": "/opt/erlang",
            "OPENSSL_INSTALL_PATH_PREFIX": "/opt/openssl",
            "RABBITMQ_DATA_DIR": "/var/lib/rabbitmq",
            "RABBITMQ_VERSION": APP_VERSION,
            "RABBITMQ_HOME": "/opt/rabbitmq",
            "HOME": "/var/lib/rabbitmq",
            "LANG": "C.UTF-8",
            "LANGUAGE": "C.UTF-8",
            "LC_ALL": "C.UTF-8",
        },
        os = "linux",
        tags = _TAGS,
        tars = [":image-%s.tar" % arch],
    )
    for arch in _ARCHS
]

[
    oci_tarball(
        name = "rabbitmq-%s" % arch,
        image = ":image-%s" % arch,
        repo_tags = ["bazel/%s:rabbitmq-%s" % (package_name(), arch)],
        tags = _TAGS,
    )
    for arch in _ARCHS
]

oci_image_index(
    name = "image",
    images = [
        ":image-%s" % arch
        for arch in _ARCHS
    ],
    tags = _TAGS,
)

oci_tarball(
    name = "rabbitmq",
    format = "oci",
    image = ":image",
    repo_tags = ["bazel/%s:rabbitmq" % package_name()],
    tags = _TAGS,
)

[
    container_structure_test(
        name = "rabbitmq_test_%s" % arch,
        configs = ["//packaging/docker-image/test_configs:rabbitmq_ubuntu.yaml"],
        image = ":image-%s" % arch,
        tags = _TAGS,
    )
    for arch in _ARCHS
]

oci_push(
    name = "push",
    image = ":image",
    repository = "index.docker.io/pivotalrabbitmq/rabbitmq",
    tags = _TAGS,
)
