load("@rules_erlang//:erlang_bytecode2.bzl", "erlang_bytecode", "erlc_opts")
load("@rules_erlang//:erlang_app.bzl", "erlang_app")

# gazelle:erlang_erlc_opt -DBUILD_WITHOUT_QUIC

erlc_opts(
    name = "erlc_opts",
    values = select({
        "@rules_erlang//:debug_build": [
            "+debug_info",
            "+warn_export_all",
            "+warn_obsolete_guard",
            "+warn_shadow_vars",
            "+warn_unused_import",
            "+warn_unused_vars",
            "-DBUILD_WITHOUT_QUIC",
        ],
        "//conditions:default": [
            "+debug_info",
            "+deterministic",
            "+warn_export_all",
            "+warn_obsolete_guard",
            "+warn_shadow_vars",
            "+warn_unused_import",
            "+warn_unused_vars",
            "-DBUILD_WITHOUT_QUIC",
        ],
    }),
    visibility = [":__subpackages__"],
)

erlang_bytecode(
    name = "other_beam",
    testonly = True,
    srcs = [
        "src/emqtt.erl",
        "src/emqtt_cli.erl",
        "src/emqtt_frame.erl",
        "src/emqtt_inflight.erl",
        "src/emqtt_props.erl",
        "src/emqtt_quic.erl",
        "src/emqtt_quic_connection.erl",
        "src/emqtt_quic_stream.erl",
        "src/emqtt_secret.erl",
        "src/emqtt_sock.erl",
        "src/emqtt_ws.erl",
    ],
    hdrs = [":public_and_private_hdrs"],
    app_name = "emqtt",
    dest = "ebin",
    erlc_opts = "//:erlc_opts",
)

filegroup(
    name = "beam_files",
    testonly = True,
    srcs = [":other_beam"],
)

filegroup(
    name = "srcs",
    testonly = True,
    srcs = [
        "src/emqtt.app.src",
        "src/emqtt.erl",
        "src/emqtt_cli.erl",
        "src/emqtt_frame.erl",
        "src/emqtt_inflight.erl",
        "src/emqtt_props.erl",
        "src/emqtt_quic.erl",
        "src/emqtt_quic_connection.erl",
        "src/emqtt_quic_stream.erl",
        "src/emqtt_secret.erl",
        "src/emqtt_sock.erl",
        "src/emqtt_ws.erl",
    ],
)

filegroup(
    name = "private_hdrs",
    testonly = True,
    srcs = glob(["src/**/*.hrl"]),
)

filegroup(
    name = "public_hdrs",
    testonly = True,
    srcs = [
        "include/emqtt.hrl",
        "include/logger.hrl",
    ],
)

filegroup(
    name = "priv",
    testonly = True,
    srcs = glob(["priv/**/*"]),
)

filegroup(
    name = "licenses",
    testonly = True,
    srcs = [
        "LICENSE",
    ],
)

filegroup(
    name = "public_and_private_hdrs",
    testonly = True,
    srcs = [
        ":private_hdrs",
        ":public_hdrs",
    ],
)

filegroup(
    name = "all_srcs",
    testonly = True,
    srcs = [
        ":public_and_private_hdrs",
        ":srcs",
    ],
)

erlang_app(
    name = "erlang_app",
    testonly = True,
    srcs = [":all_srcs"],
    hdrs = [":public_hdrs"],
    app_name = "emqtt",
    beam_files = [":beam_files"],
    license_files = [":license_files"],
    priv = [":priv"],
    deps = [
        "@cowlib//:erlang_app",
        "@getopt//:erlang_app",
        "@gun//:erlang_app",
    ],
)

alias(
    name = "emqtt",
    actual = ":erlang_app",
    visibility = ["//visibility:public"],
)

filegroup(
    name = "license_files",
    testonly = True,
    srcs = glob(["LICENSE*"]),
)
