load("//java:defs.bzl", "artifact", "java_dist_zip", "java_export", "java_import", "javadoc")
load("//java:version.bzl", "SE_VERSION")
load("//java/src/org/openqa/selenium/devtools:versions.bzl", "CDP_DEPS")

filegroup(
    name = "template-pom",
    srcs = ["pom.xml"],
    visibility = ["//visibility:public"],
)

java_export(
    name = "core",
    srcs = glob([
        "*.java",
        "federatedcredentialmanagement/*.java",
        "html5/*.java",
        "internal/*.java",
        "interactions/**/*.java",
        "logging/**/*.java",
        "mobile/*.java",
        "net/*.java",
        "virtualauthenticator/*.java",
        "print/*.java",
    ]),
    hides = [
        "org.openqa.selenium.interactions.internal",
    ],
    maven_coordinates = "org.seleniumhq.selenium:selenium-api:%s" % SE_VERSION,
    opens_to = [
        "org.seleniumhq.selenium.json",
    ],
    pom_template = ":template-pom",
    tags = [
        "release-artifact",
    ],
    visibility = ["//visibility:public"],
    deps = [
        ":manifest",
        artifact("org.jspecify:jspecify"),
    ],
)

java_export(
    name = "client-combined",
    maven_coordinates = "org.seleniumhq.selenium:selenium-java:" + SE_VERSION,
    pom_template = ":template-pom",
    tags = [
        "release-artifact",
    ],
    visibility = [
        "//visibility:public",
    ],
    exports = [
        ":core",
        "//java/src/org/openqa/selenium/chrome",
        "//java/src/org/openqa/selenium/edge",
        "//java/src/org/openqa/selenium/firefox",
        "//java/src/org/openqa/selenium/ie",
        "//java/src/org/openqa/selenium/remote",
        "//java/src/org/openqa/selenium/safari",
        "//java/src/org/openqa/selenium/support",
    ] + CDP_DEPS,
)

java_dist_zip(
    name = "client-zip",
    files = [
        "//:license",
        "//java:CHANGELOG",
    ],
    third_party_prefixes = [
        "@maven//",
        "//third_party",
    ],
    deps = [
        ":client-combined",
    ],
)

javadoc(
    name = "client-javadoc",
    third_party_prefixes = [
        "@",
        "//third_party",
    ] + CDP_DEPS,
    transitive = True,
    deps = [
        ":client-combined",
    ],
)

java_import(
    name = "manifest",
    jars = [
        ":manifest-entries",
        ":manifest-jar",
    ],
    visibility = [
        "//java/src/org/openqa/selenium/grid:__pkg__",
    ],
)

genrule(
    name = "manifest-jar",
    outs = [
        "version.jar",
    ],
    cmd = select({
        "//common:stamp": """
        mkdir -p META-INF &&
        echo "Selenium-Version = %s" >META-INF/selenium-build.properties &&
        echo "Build-Revision = " $$(grep GIT_REVISION bazel-out/stable-status.txt | cut -d' ' -f 2) >>META-INF/selenium-build.properties &&
        $(location @bazel_tools//tools/zip:zipper) c $@ META-INF/selenium-build.properties
    """ % SE_VERSION,
        "//conditions:default": """
        mkdir -p META-INF &&
        echo "Selenium-Version = %s" >META-INF/selenium-build.properties &&
        echo "Build-Revision = Unknown"  >>META-INF/selenium-build.properties &&
        $(location @bazel_tools//tools/zip:zipper) c $@ META-INF/selenium-build.properties
    """ % SE_VERSION,
    }),
    stamp = select({
        "//common:stamp": True,
        "//conditions:default": False,
    }),
    tools = [
        "@bazel_tools//tools/zip:zipper",
    ],
)

genrule(
    name = "manifest-entries",
    outs = [
        "manifest-entries.jar",
    ],
    cmd = select({
        "//common:stamp": """
      mkdir -p META-INF &&
      echo Manifest-Version: 1.0 >META-INF/MANIFEST.MF &&
      echo "" >>META-INF/MANIFEST.MF &&
      echo "" >>META-INF/MANIFEST.MF &&
      echo Name: Build-Info >>META-INF/MANIFEST.MF &&
      echo Selenium-Version: %s >>META-INF/MANIFEST.MF &&
      echo Build-Revision: $$(grep GIT_REVISION bazel-out/stable-status.txt | cut -d' ' -f 2) >>META-INF/MANIFEST.MF &&
      echo Build-Time: $$(date) >>META-INF/MANIFEST.MF &&
      $(location @bazel_tools//tools/zip:zipper) c $@ META-INF/MANIFEST.MF
      """ % SE_VERSION,
        "//conditions:default": """
      mkdir -p META-INF &&
      echo Manifest-Version: 1.0 >META-INF/MANIFEST.MF &&
      echo "" >>META-INF/MANIFEST.MF &&
      echo "" >>META-INF/MANIFEST.MF &&
      echo Name: Build-Info >>META-INF/MANIFEST.MF &&
      echo Selenium-Version: %s >>META-INF/MANIFEST.MF &&
      echo Build-Revision: Unknown >>META-INF/MANIFEST.MF &&
      echo Build-Time: Wed Jan 3 21:49:10 2007 +0000 >>META-INF/MANIFEST.MF &&
      $(location @bazel_tools//tools/zip:zipper) c $@ META-INF/MANIFEST.MF
      """ % SE_VERSION,
    }),
    stamp = select({
        "//common:stamp": True,
        "//conditions:default": False,
    }),
    tools = [
        "@bazel_tools//tools/zip:zipper",
    ],
)
