from jolt import attributes, BooleanParameter, Parameter
from jolt.pkgs import brotli, ssl, zlib, zstd
from jolt.plugins import cxxinfo, git, cmake
from jolt.tasks import TaskRegistry


@attributes.requires("requires_brotli")
@attributes.requires("requires_git")
@attributes.requires("requires_ssl")
@attributes.requires("requires_zlib")
@attributes.requires("requires_zstd")
@attributes.system
@cmake.requires()
@cmake.use_ninja()
@cxxinfo.publish()
class Curl(cmake.CMake):
    name = "curl"
    version = Parameter("8.17.0", help="Curl version.")
    pic = BooleanParameter(False, help="Build with position independent code.")
    shared = BooleanParameter(False, help="Build shared libraries.")
    requires_brotli = ["brotli:pic={pic},shared={shared}"]
    requires_git = ["git:url=https://github.com/curl/curl.git,rev=curl-{_version_tag},submodules=true"]
    requires_ssl = ["virtual/ssl:pic={pic},shared={shared}"]
    requires_zlib = ["virtual/zlib:pic={pic},shared={shared}"]
    requires_zstd = ["zstd:pic={pic},shared={shared}"]
    srcdir = "{git[curl]}"
    options = [
        "BUILD_SHARED_LIBS={shared[ON,OFF]}",
        "CURL_USE_LIBPSL=OFF",
        "USE_LIBIDN2=OFF",
        "OPENSSL_USE_STATIC_LIBS={shared[OFF,ON]}",
        "CMAKE_POSITION_INDEPENDENT_CODE={pic[ON,OFF]}",
    ]

    @property
    def _version_tag(self):
        return str(self.version).replace('.', '_')

    def publish(self, artifact, tools):
        super().publish(artifact, tools)
        if self.system == "windows":
            if self.shared:
                artifact.cxxinfo.libraries.append("libcurl_imp")
            else:
                artifact.cxxinfo.libraries.append("libcurl")
        else:
            artifact.cxxinfo.libraries.append("curl")


TaskRegistry.get().add_task_class(Curl)
