evaluationDependsOn(":node:capsule")

import com.bmuschko.gradle.docker.tasks.image.DockerBuildImage
import com.bmuschko.gradle.docker.tasks.image.DockerPushImage

import java.time.LocalDateTime
import java.time.format.DateTimeFormatter

apply plugin: 'kotlin'
apply plugin: 'application'
apply plugin: 'com.bmuschko.docker-remote-api'

// We need to set mainClassName before applying the shadow plugin.
mainClassName = 'net.corda.core.ConfigExporterMain'
apply plugin: 'com.github.johnrengelman.shadow'

dependencies{
    compile project(':node')
}

shadowJar {
    baseName = 'config-exporter'
    classifier = null
    version = null
    zip64 true
    exclude '**/Log4j2Plugins.dat'
}

docker{
    registryCredentials  {
        url = System.env.DOCKER_URL ?: "hub.docker.com"
        username = System.env.DOCKER_USERNAME
        password = System.env.DOCKER_PASSWORD
    }
}

final dockerTempDir = file("$buildDir/docker-temp")

task buildDockerFolder(type: Copy) {
    into dockerTempDir
    from "src/bash/run-corda.sh"
    from(project(':node:capsule').tasks.buildCordaJAR) {
        rename 'corda-(.*)', 'corda.jar'
    }
    from(shadowJar) {
        rename 'config-exporter-(.*).jar', 'config-exporter.jar'
    }
    from "src/config/starting-node.conf"
    from "src/bash/generate-config.sh"
    from "src/docker/DockerfileAL"
    from "src/docker/Dockerfile"
    from "src/docker/Dockerfile11"
}

final String runTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"))
final String suffix = project.version.toString().toLowerCase().contains("snapshot") ?  runTime : "RELEASE"
final zuluBuildTags = ["corda/corda-zulu-java${JavaVersion.current()}-${project.version.toString().toLowerCase()}:${suffix}", "corda/corda-zulu-java${JavaVersion.current()}-${project.version.toString().toLowerCase()}:latest"]
final correttoBuildTags = ["corda/corda-corretto-java${JavaVersion.current()}-${project.version.toString().toLowerCase()}:${suffix}", "corda/corda-corretto-java${JavaVersion.current()}-${project.version.toString().toLowerCase()}:latest"]

task buildOfficialZuluDockerImage(type: DockerBuildImage, dependsOn: [buildDockerFolder]) {
    inputDir = dockerTempDir
    tags = zuluBuildTags
    dockerFile = new File(dockerTempDir, "Dockerfile")
}

task buildOfficialZuluJDK11DockerImage(type: DockerBuildImage, dependsOn: [buildDockerFolder]) {
    inputDir = dockerTempDir
    tags = zuluBuildTags
    dockerFile = new File(dockerTempDir, "Dockerfile11")
}

task buildOfficialCorrettoDockerImage(type: DockerBuildImage, dependsOn: [buildDockerFolder]) {
    inputDir = dockerTempDir
    tags = correttoBuildTags
    dockerFile = new File(dockerTempDir, "DockerfileAL")
}

task pushZuluTimeStampedTag('type': DockerPushImage, dependsOn: [buildOfficialZuluDockerImage]){
    imageName = zuluBuildTags[0]
}

task pushZuluLatestTag('type': DockerPushImage, dependsOn: [buildOfficialZuluDockerImage]){
    imageName = zuluBuildTags[1]
}

task pushZulu11TimeStampedTag('type': DockerPushImage, dependsOn: [buildOfficialZuluJDK11DockerImage]){
    imageName = zuluBuildTags[0]
}

task pushZulu11LatestTag('type': DockerPushImage, dependsOn: [buildOfficialZuluJDK11DockerImage]){
    imageName = zuluBuildTags[1]
}

task pushCorrettoTimeStampedTag('type': DockerPushImage, dependsOn: [buildOfficialCorrettoDockerImage]){
    imageName = correttoBuildTags[0]
}

task pushCorrettoLatestTag('type': DockerPushImage, dependsOn: [buildOfficialCorrettoDockerImage]){
    imageName = correttoBuildTags[1]
}

task pushOfficialImages(dependsOn: [pushZuluTimeStampedTag, pushZuluLatestTag, pushCorrettoTimeStampedTag, pushCorrettoLatestTag])
