description = 'Conscrypt: OpenJdk UberJAR'

Directory buildTop = layout.buildDirectory.get()
ext {
    buildUberJar = Boolean.parseBoolean(System.getProperty('org.conscrypt.openjdk.buildUberJar', 'false'))
    uberJarClassifiers = (System.getProperty('org.conscrypt.openjdk.uberJarClassifiers',
            'osx-x86_64,osx-aarch_64,linux-x86_64,windows-x86_64')).split(',')
    classesDir = buildTop.dir('classes')
    resourcesDir = buildTop.dir('resources')
    sourcesDir = buildTop.dir('sources')
    javadocDir = buildTop.dir('docs/javadoc')
}

if (buildUberJar) {
    apply plugin: libs.plugins.bnd.get().pluginId

    configurations {
        uberJar
    }

    // Point the jar task to the copied classes and resources directories.
    jar {
        from classesDir
        from resourcesDir
        from javadocDir
    }

    sourcesJar {
        from sourcesDir
    }

    // Add the dependencies for the uber jar.
    uberJarClassifiers.each { uberJarClassifier ->
        dependencies.uberJar "${group}:conscrypt-openjdk:${version}:${uberJarClassifier}"
    }

    /**
     * Copy the native libraries to the resources directory.
     */
    def copySharedLibs = tasks.register("copySharedLibs",  Copy) {
        dependsOn configurations.uberJar
        from {
            configurations.uberJar.collect {
                zipTree(it)
            }
        }
        include '/META-INF/native/**'
        into file(resourcesDir)
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
    tasks.named("jar").configure {
        dependsOn copySharedLibs
    }

    /**
     * Copy the object files to the classes directory.
     */
    def copyClasses = tasks.register("copyClasses", Copy) {
        dependsOn configurations.uberJar
        from {
            configurations.uberJar.collect {
                zipTree(it)
            }
        }
        exclude '/META-INF/**'
        into file(classesDir)
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
    tasks.named("jar").configure {
        dependsOn copyClasses
    }

    def copySources = tasks.register("copySources", Copy) {
        dependsOn copyJavadocs
        dependsOn ":conscrypt-constants:runGen"
        from project(":conscrypt-openjdk").sourceSets.main.java
        into file(sourcesDir)
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
    tasks.named("sourcesJar").configure {
        dependsOn copySources
    }

    def copyJavadocs = tasks.register("copyJavadocs", Copy) {
        dependsOn ':conscrypt-openjdk:javadoc'
        from project(':conscrypt-openjdk').layout.buildDirectory
        include('docs/**/*')
        into layout.buildDirectory
        duplicatesStrategy = DuplicatesStrategy.EXCLUDE
    }
    tasks.named('javadocJar').configure {
        dependsOn copyJavadocs
    }
    tasks.named('jar').configure {
        dependsOn copyJavadocs
    }

    // Note that this assumes that the version of BoringSSL for each
    // artifact exactly matches the one on the current system.
    jar.manifest {
        attributes ('BoringSSL-Version': boringSslVersion,
                    'Automatic-Module-Name': 'org.conscrypt',
                    'Bundle-SymbolicName': 'org.conscrypt',
                    '-exportcontents': 'org.conscrypt.*')
    }

    apply from: "$rootDir/gradle/publishing.gradle"
    publishing.publications.maven {
        artifact sourcesJar
        artifact javadocJar
        artifact jar
    }
} else {
    // Not building an uber jar - disable all tasks.
    tasks.configureEach {
        it.enabled = false
    }
}
