/*
 * 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.19.0,2)'
    implementation 'org.jgrapes:org.jgrapes.io:[2.7.0,3)'
    implementation 'org.jgrapes:org.jgrapes.http:[3.1.0,4)'
    implementation 'org.jgrapes:org.jgrapes.util:[1.29.0,2)'
    implementation project(':org.jdrupes.vmoperator.util')

    implementation 'commons-cli:commons-cli:1.5.0'
    implementation 'org.freemarker:freemarker:[2.3.32,2.4)'
    implementation 'com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:[2.15.1,3]'
    
    runtimeOnly 'com.electronwill.night-config:yaml:3.6.6'    
}

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

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

task tagLatestArchImage(type: Exec) {
    dependsOn buildArchImage
    
    commandLine 'podman', 'tag', "${project.name}-arch:${project.version}",\
        "${project.name}:latest"
}

task buildLatestArchImage {
    dependsOn buildArchImage
    dependsOn tagLatestArchImage
}

task pushArchImage(type: Exec) {
    dependsOn buildArchImage

    commandLine 'podman', 'push', '--tls-verify=false', \
        "localhost/${project.name}-arch:${project.version}", \
        "${project.rootProject.properties['docker.registry']}" \
            + "/${project.name}-arch:${project.version}"
}

task pushArchLatestImage(type: Exec) {
    dependsOn buildLatestArchImage

    commandLine 'podman', 'push', '--tls-verify=false', \
        "localhost/${project.name}-arch:${project.version}", \
        "${project.rootProject.properties['docker.registry']}" \
            + "/${project.name}-arch:latest"
}

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

task tagLatestAlpineImage(type: Exec) {
    dependsOn buildAlpineImage
    
    commandLine 'podman', 'tag', "${project.name}-alpine:${project.version}",\
        "${project.name}-alpine:latest"
}

task buildLatestAlpineImage {
    dependsOn buildAlpineImage
    dependsOn tagLatestAlpineImage
}

task pushAlpineImage(type: Exec) {
    dependsOn buildAlpineImage

    commandLine 'podman', 'push', '--tls-verify=false', \
        "localhost/${project.name}-alpine:${project.version}", \
        "${project.rootProject.properties['docker.registry']}" \
            + "/${project.name}-alpine:${project.version}"
}

task pushAlpineLatestImage(type: Exec) {
    dependsOn buildLatestAlpineImage

    commandLine 'podman', 'push', '--tls-verify=false', \
        "localhost/${project.name}-alpine:${project.version}", \
        "${project.rootProject.properties['docker.registry']}" \
            + "/${project.name}-alpine:latest"
}

task pushImages {
    dependsOn pushArchImage
    dependsOn pushArchLatestImage
    dependsOn pushAlpineImage
    dependsOn pushAlpineLatestImage
}

