//
// Copyright (c) ZeroC, Inc. All rights reserved.
//

task copyTmpJars(type: Copy, dependsOn: jar) {
    from new File("${projectDir}/build/libs/${tmpJarName}")
    into "${libDir}"
    rename("${tmpJarName}", "${jarName}")

}

task updateManifest(dependsOn: copyTmpJars) {
    doLast {
        ant.jar(update: true, destfile: "${libDir}/${jarName}") {
            delegate.manifest {
                attribute(name: "Main-Class", value: "com.zeroc.IceGridGUI.Main")
                attribute(name: "Built-By", value: "ZeroC, Inc.")
                attribute(name: "Class-Path", value: configurations.runtimeClasspath.resolve().collect { "file://${it.toURI().getRawPath()}" }.join(' '))
            }
        }
    }
}
updateManifest.outputs.files("${libDir}/${jarName}")
assemble.dependsOn(updateManifest)

//
// Copy JARs to the install location
//
task copyJars(type: Copy, dependsOn: jar) {
    from new File("${projectDir}/build/libs/${tmpJarName}")
    into "${DESTDIR}${jarDir}"
    rename("${tmpJarName}", "${jarName}")
}

//
// We need to update the manifest of the installed IceGridGUI jar and fix the
// Class-Path to point to the installed JAR files.
//
task updateInstallManifest(dependsOn: copyJars) {
    doLast {
        ant.jar(update: true, destfile: "${DESTDIR}${jarDir}/${jarName}") {
            delegate.manifest {
                attribute(name: "Main-Class", value: "com.zeroc.IceGridGUI.Main")
                attribute(name: "Built-By", value: "ZeroC, Inc.")
                attribute(name: "Class-Path", value: configurations.runtimeClasspath.resolve().collect {
                    "file://${it.toURI().getRawPath()}" }.join(' ').replaceAll("${libDir.replaceAll('\\\\', '/')}", "${jarDir.replaceAll('\\\\', '/')}"))
            }
        }
    }
}
updateInstallManifest.outputs.files("${DESTDIR}${jarDir}/${jarName}")
task install(dependsOn: updateInstallManifest)
