plugins {
    id 'java-library'
    alias catalog.plugins.kotlin.jvm
    alias catalog.plugins.envelope
    alias catalog.plugins.sambal
    alias catalog.plugins.graalvm.native.image
    alias catalog.plugins.graalvm.jlink
    alias catalog.plugins.jpms.check
    id 'maven-publish'
}


import net.woggioni.gradle.envelope.EnvelopeJarTask
import net.woggioni.gradle.envelope.EnvelopePlugin
import net.woggioni.gradle.graalvm.*

sourceSets {
    configureNativeImage {
        java {
        }
        kotlin {

        }
    }
}

configurations {

    release {
        transitive = false
        canBeConsumed = true
        canBeResolved = true
    }

    configureNativeImageImplementation {
        extendsFrom implementation
    }

    configureNativeImageRuntimeOnly {
        extendsFrom runtimeOnly
    }

    nativeImage {
        extendsFrom runtimeClasspath
    }

}

dependencies {
    configureNativeImageImplementation project
    configureNativeImageImplementation project(':rbcs-server-memcache')
    configureNativeImageImplementation project(':rbcs-server-redis')
//    configureNativeImageImplementation project(':rbcs-server-otel')

    implementation catalog.jwo
    implementation catalog.slf4j.api
    implementation catalog.picocli

    implementation project(':rbcs-client')
    implementation project(':rbcs-server')

    runtimeOnly catalog.logback.classic
    nativeImage project(':rbcs-server-memcache')
    nativeImage project(':rbcs-server-redis')

}


Property<String> mainModuleName = objects.property(String.class)
mainModuleName.set('net.woggioni.rbcs.cli')
Property<String> mainClassName = objects.property(String.class)
mainClassName.set('net.woggioni.rbcs.cli.RemoteBuildCacheServerCli')

tasks.named(JavaPlugin.COMPILE_JAVA_TASK_NAME, JavaCompile) {
    options.javaModuleMainClass = mainClassName
}

Provider<Jar> jarTaskProvider = tasks.named(JavaPlugin.JAR_TASK_NAME, Jar)

Provider<EnvelopeJarTask> envelopeJarTaskProvider = tasks.named(EnvelopePlugin.ENVELOPE_JAR_TASK_NAME, EnvelopeJarTask.class) {
    mainModule = mainModuleName
    mainClass = mainClassName

    extraClasspath = ["plugins"]

    systemProperties['logback.configurationFile'] = 'classpath:net/woggioni/rbcs/cli/logback.xml'
    systemProperties['io.netty.leakDetectionLevel'] = 'DISABLED'
}

tasks.named(NativeImagePlugin.CONFIGURE_NATIVE_IMAGE_TASK_NAME, NativeImageConfigurationTask) {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
        vendor = JvmVendorSpec.ORACLE
    }
    mainClass = "net.woggioni.rbcs.cli.graal.GraalNativeImageConfiguration"
    classpath = project.files(
        configurations.configureNativeImageRuntimeClasspath,
        sourceSets.configureNativeImage.output
    )
    mergeConfiguration = false
    systemProperty('logback.configurationFile', 'classpath:net/woggioni/rbcs/cli/logback.xml')
    systemProperty('io.netty.leakDetectionLevel', 'DISABLED')
    modularity.inferModulePath = false
    enabled = true
    systemProperty('gradle.tmp.dir', temporaryDir.toString())
}

nativeImage {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
        vendor = JvmVendorSpec.ORACLE
    }
    mainClass = mainClassName
    //mainModule = mainModuleName
    useMusl = true
    buildStaticImage = true
    linkAtBuildTime = false
    classpath = project.files(jarTaskProvider, configurations.nativeImage)
    compressExecutable = true
    compressionLevel = 6
    useLZMA = false
    //verbose = true
}

Provider<UpxTask> upxTaskProvider = tasks.named(NativeImagePlugin.UPX_TASK_NAME, UpxTask) {
}

Provider<JlinkTask> jlinkTaskProvider = tasks.named(JlinkPlugin.JLINK_TASK_NAME, JlinkTask) {
    toolchain {
        languageVersion = JavaLanguageVersion.of(25)
        vendor = JvmVendorSpec.ORACLE
    }

    mainClass = mainClassName
    mainModule = 'net.woggioni.rbcs.cli'
    classpath = project.files(
            configurations.configureNativeImageRuntimeClasspath,
            sourceSets.configureNativeImage.output
    )
    additionalModules = [
        'net.woggioni.rbcs.server.memcache',
        'net.woggioni.rbcs.server.redis',
        'ch.qos.logback.classic',
        'jdk.crypto.ec',
//        'io.opentelemetry.api',
//        'io.opentelemetry.instrumentation.netty_4_1',
//        'io.opentelemetry.sdk.autoconfigure',
//        'io.opentelemetry.instrumentation.logback_appender_1_0',
//        'io.opentelemetry.extension.trace.propagation'
    ]
    compressionLevel = 2
    stripDebug = false
}

Provider<Tar> jlinkDistTarTaskProvider = tasks.named(JlinkPlugin.JLINK_DIST_TAR_TASK_NAME, Tar) {
    exclude 'lib/libjvmcicompiler.so'
}

tasks.named(JavaPlugin.PROCESS_RESOURCES_TASK_NAME, ProcessResources) {
    from(rootProject.file('conf')) {
        into('net/woggioni/rbcs/cli')
        include 'logback.xml'
        include 'logging.properties'
    }
}

artifacts {
    release(envelopeJarTaskProvider)
    release(upxTaskProvider)
    release(jlinkDistTarTaskProvider)
}

publishing {
    publications {
        maven(MavenPublication) {
            artifact envelopeJar
            artifact(upxTaskProvider) {
                classifier = "linux-x86_64"
                extension = "exe"
            }
        }
    }
}



