/*
 * 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 project(':org.jdrupes.vmoperator.manager.events')
    
    implementation 'commons-cli:commons-cli:1.5.0'
    
    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.34.0,2)'
    
    implementation 'org.jgrapes:org.jgrapes.webconsole.base:[1.5.0,2)'
    implementation 'org.jgrapes:org.jgrapes.webconsole.vuejs:[1.5.0,2)'
    implementation 'org.jgrapes:org.jgrapes.webconsole.rbac:[1.3.0,2)'
    implementation 'org.jgrapes:org.jgrapes.webconlet.oidclogin:[1.3.0,2)'
    
    runtimeOnly 'org.jgrapes:org.jgrapes.webconlet.sysinfo:[1.4.0,2)'
    runtimeOnly 'org.jgrapes:org.jgrapes.webconlet.logviewer:[0.2.0,2)'
    
    runtimeOnly 'com.electronwill.night-config:yaml:[3.6.7,3.7)'    
    runtimeOnly 'org.eclipse.angus:angus-activation:[1.0.0,2.0.0)'
    runtimeOnly 'org.slf4j:slf4j-jdk14:[2.0.7,3)'
    runtimeOnly 'org.apache.logging.log4j:log4j-to-jul:2.20.0'

    runtimeOnly project(':org.jdrupes.vmoperator.vmconlet')
    runtimeOnly project(':org.jdrupes.vmoperator.vmviewer')
}

application {
    applicationName = 'vm-manager'
    applicationDefaultJvmArgs = ['-Xmx128m', '-XX:+UseParallelGC',
        '-Djava.util.logging.manager=org.jdrupes.vmoperator.util.LongLoggingManager'
        ]
    // Define the main class for the application.
    mainClass = 'org.jdrupes.vmoperator.manager.Manager'
}

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

task tagLatestImage(type: Exec) {
    dependsOn buildImage
    
    enabled = !project.version.contains("SNAPSHOT")
        && !project.version.contains("alpha") \
        && !project.version.contains("beta") \
        || project.rootProject.properties['docker.testRegistry'] \
            && project.rootProject.properties['docker.registry'] \
            == project.rootProject.properties['docker.testRegistry']

    commandLine 'podman', 'tag', "${project.name}:${project.version}",\
        "${project.name}:latest"
}

task buildLatestImage {
    dependsOn buildImage
    dependsOn tagLatestImage
}

task pushImage(type: Exec) {
    dependsOn buildImage

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

task pushLatestImage(type: Exec) {
    dependsOn buildLatestImage

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

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

task pushForTest(type: Exec) {
    dependsOn buildImage

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

task pushImages {
	// Don't push without testing first
    dependsOn test
    dependsOn pushImage
    dependsOn pushLatestImage
}

test {
    enabled = project.hasProperty("k8s.testCluster")

    if (enabled) {
        dependsOn project.tasks["pushForTest"]
    }

    useJUnitPlatform()

    testLogging {
        showStandardStreams = true
    }
    
    systemProperty "k8s.testCluster", project.hasProperty("k8s.testCluster")
        ? project.getProperty("k8s.testCluster") : null
}

// Update favicon:
// # Convert with inkscape to png because convert cannot handle svg 
// # background transparency, then
// convert VM-Operator.png -background transparent \
//     -define icon:auto-resize=256,64,48,32,16 favicon.ico
