load("@rules_pkg//:pkg.bzl", "pkg_tar")
load("//common/private:passwd.bzl", "passwd_entry", "passwd_file")
load(":docker.bzl", "docker_image")

REGISTRY = "localhost:5000"

# Create a passwd file with a root and selenium user and uid.
passwd_entry(
    name = "root_user",
    gid = 0,
    uid = 0,
    username = "root",
)

passwd_entry(
    name = "seluser",
    home = "/home/seluser",
    info = "Selenium User",
    uid = 1002,
    username = "seluser",
)

passwd_file(
    name = "passwd",
    entries = [
        ":root_user",
        ":seluser",
    ],
)

pkg_tar(
    name = "passwd-tar",
    srcs = [":passwd"],
    mode = "0644",
    package_dir = "etc",
)

ENTRY_POINT = [
    "java",
    "-jar",
    "/opt/selenium/bin/selenium",
]

docker_image(
    name = "grid-base",
    base = "@java_image_base",
    entrypoint = ENTRY_POINT,
    tars = [
        ":grid-binary",
        ":passwd-tar",
    ],
    user = "seluser",
    workdir = "/app",
)

docker_image(
    name = "distributor-image",
    base = ":grid-base",
    entrypoint = ENTRY_POINT + ["distributor"],
    ports = [
        "5553/tcp",  # Web
    ],
)

docker_image(
    name = "event-bus-image",
    base = ":grid-base",
    entrypoint = ENTRY_POINT + ["event-bus"],
    ports = [
        "5557/tcp",  # Web
        "4442/tcp",  # ZeroMQ
        "4443/tcp",
    ],
)

docker_image(
    name = "router-image",
    base = ":grid-base",
    entrypoint = ENTRY_POINT + ["router"],
    ports = [
        "4444/tcp",
    ],
)

docker_image(
    name = "session-map-image",
    base = ":grid-base",
    entrypoint = ENTRY_POINT + ["sessions"],
    ports = [
        "5556/tcp",  # Web
        "4442/tcp",  # ZeroMQ
        "4443/tcp",
    ],
)

docker_image(
    name = "session-queue-image",
    base = ":grid-base",
    entrypoint = ENTRY_POINT + ["sessionqueue"],
    ports = [
        "5559/tcp",  # Web
        "4442/tcp",  # ZeroMQ
        "4443/tcp",
    ],
)

pkg_tar(
    name = "grid-binary",
    srcs = [
        "//java/src/org/openqa/selenium/grid:executable-grid",
    ],
    mode = "0755",
    package_dir = "/opt/selenium/bin",
)

pkg_tar(
    name = "one-shot",
    srcs = [
        "//java/src/org/openqa/selenium/grid/node/k8s",
    ],
    package_dir = "/opt/selenium",
)

pkg_tar(
    name = "start-scripts",
    srcs = [
        ":selenium.sh",
    ],
    package_dir = "/opt/selenium/bin",
)

docker_image(
    name = "chrome-node",
    base = "@chrome_standalone",
    entrypoint = [
        "/opt/selenium/bin/selenium.sh",
        "node",
    ],
    ports = [
        "5555",  # Selenium
        "5900",  # vnc,
    ],
    tars = [
        ":grid-binary",
        ":one-shot",
        ":start-scripts",
    ],
    user = "seluser",
)

docker_image(
    name = "firefox-node",
    base = "@firefox_standalone",
    entrypoint = [
        "/opt/selenium/bin/selenium.sh",
        "node",
    ],
    ports = [
        "5555",  # Selenium
        "5900",  # vnc,
    ],
    tars = [
        ":grid-binary",
        ":one-shot",
        ":start-scripts",
    ],
    user = "seluser",
)
