/*
 * This file was generated by the Gradle 'init' task.
 *
 * This project uses @Incubating APIs which are subject to change.
 */

plugins {
    id 'org.jdrupes.vmoperator.java-application-conventions'
}

dependencies {
    implementation 'org.jgrapes:org.jgrapes.core:[1.22.1,2)'
    implementation 'org.jgrapes:org.jgrapes.util:[1.38.1,2)'
    implementation 'org.jgrapes:org.jgrapes.io:[2.12.1,3)'
    implementation 'org.jgrapes:org.jgrapes.http:[3.5.0,4)'
    implementation project(':org.jdrupes.vmoperator.common')

    implementation 'commons-cli:commons-cli:1.5.0'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:[2.16.1]'

    runtimeOnly 'org.slf4j:slf4j-jdk14:[2.0.7,3)'
}

application {
    applicationName = 'vm-runner.qemu'
    // Keep sync'd with deploy/vmop-deployment.yaml
    applicationDefaultJvmArgs = ['-Xmx32m', '-XX:+UseParallelGC',
        '-Djava.util.logging.manager=org.jdrupes.vmoperator.util.LongLoggingManager'
        ]
    // Define the main class for the application.
    mainClass = 'org.jdrupes.vmoperator.runner.qemu.Runner'
}

project.ext.gitBranch = grgit.branch.current.name.replace('/', '-')
def registry = "${project.rootProject.properties['docker.registry']}"
def rootVersion = rootProject.version

task buildImageArch(type: Exec) {
    dependsOn installDist
    inputs.files 'src/org/jdrupes/vmoperator/runner/qemu/Containerfile.arch'
    
    commandLine 'podman', 'build', '--pull',
        '-t', "${project.name}-arch:${project.gitBranch}",\
        '-f', 'src/org/jdrupes/vmoperator/runner/qemu/Containerfile.arch', '.'
}

task pushImageArch(type: Exec) {
    dependsOn buildImageArch

    commandLine 'podman', 'push', '--tls-verify=false', \
        "${project.name}-arch:${project.gitBranch}", \
        "${registry}/${project.name}-arch:${project.gitBranch}"
}

task tagWithVersionArch(type: Exec) {
    dependsOn pushImageArch

    enabled = !rootVersion.contains("SNAPSHOT")
    
    commandLine 'podman', 'push', \
        "${project.name}-arch:${project.gitBranch}",\
        "${registry}/${project.name}-arch:${project.version}"
}

task tagAsLatestArch(type: Exec) {
    dependsOn tagWithVersionArch

    enabled = !rootVersion.contains("SNAPSHOT")
        && !rootVersion.contains("alpha") \
        && !rootVersion.contains("beta") \
        || project.rootProject.properties['docker.testRegistry'] \
            && project.rootProject.properties['docker.registry'] \
            == project.rootProject.properties['docker.testRegistry']

    commandLine 'podman', 'push', \
        "${project.name}-arch:${project.gitBranch}",\
        "${registry}/${project.name}-arch:latest"
}

task buildImageAlpine(type: Exec) {
    dependsOn installDist
    inputs.files 'src/org/jdrupes/vmoperator/runner/qemu/Containerfile.alpine'
    
    commandLine 'podman', 'build', '--pull',
        '-t', "${project.name}-alpine:${project.gitBranch}",\
        '-f', 'src/org/jdrupes/vmoperator/runner/qemu/Containerfile.alpine', '.'
}

task pushImageAlpine(type: Exec) {
    dependsOn buildImageAlpine

    commandLine 'podman', 'push', '--tls-verify=false', \
        "localhost/${project.name}-alpine:${project.gitBranch}", \
        "${registry}/${project.name}-alpine:${project.gitBranch}"
}

task tagWithVersionAlpine(type: Exec) {
    dependsOn pushImageAlpine

    enabled = !rootVersion.contains("SNAPSHOT")
    
    commandLine 'podman', 'push', \
        "${project.name}-alpine:${project.gitBranch}",\
        "${registry}/${project.name}-alpine:${project.version}"
}

task tagAsLatestAlpine(type: Exec) {
    dependsOn tagWithVersionAlpine

    enabled = !rootVersion.contains("SNAPSHOT")
        && !rootVersion.contains("alpha") \
        && !rootVersion.contains("beta") \
        || project.rootProject.properties['docker.testRegistry'] \
            && project.rootProject.properties['docker.registry'] \
            == project.rootProject.properties['docker.testRegistry']

    commandLine 'podman', 'push', \
        "${project.name}-alpine:${project.gitBranch}",\
        "${registry}/${project.name}-alpine:latest"
}

task publishImage {
    dependsOn pushImageArch
    dependsOn tagWithVersionArch
    dependsOn tagAsLatestArch
    dependsOn pushImageAlpine
    dependsOn tagWithVersionAlpine
    dependsOn tagAsLatestAlpine
}
