apply plugin: 'izpack'
if (macosx) apply plugin: 'com.github.cr0.macappbundle'

dependencies {
    izpack 'org.codehaus.izpack:izpack-standalone-compiler:4.3.5'
}

task prepareIzpack(type: Copy, dependsOn: installDist) {
    destinationDir = file("$buildDir/install/izpack")
    from('src/main/izpack/resources') {
        into 'resources'
    }
    into('binary') {
        from installDist.destinationDir
    }
}

izPackCreateInstaller.dependsOn prepareIzpack
izPackCreateInstaller.doFirst {
    ant.chmod(dir: "$buildDir/install/izpack/binary/bin", excludes: '*.bat', perm: 'ugo+x')
}

izpack {
    baseDir = file("$buildDir/install/izpack")
    installFile = file('src/main/izpack/install.xml')
    outputFile = file("$buildDir/distributions/${project.name}-${version}-installer.jar")
    compression = 'deflate'
    compressionLevel = 9
    appProperties = [
        'app.group'       : 'Applications',
        'app.name'        : project.name,
        'app.title'       : project.name,
        'app.version'     : project.version,
        'app.subpath'     : "${project.name}-${project.version}",
        'app.binary'      : project.name,
        'app.java.version': targetCompatibility
    ]
}

if (macosx) {
    File javaHome = new File(System.properties['java.home'])
    javaHome = javaHome.name == 'jre' ? javaHome.parentFile : javaHome

    macAppBundle {
        jreHome = javaHome.absolutePath
        mainClassName = project.mainClassName
        icon = 'src/media/griffon.icns'
        javaProperties.put('apple.laf.useScreenMenuBar', 'true')
    }
}

if (!project.hasProperty('keyAlias')) ext.keyAlias = ''
if (!project.hasProperty('keystorePwd')) ext.keystorePwd = ''

if (keyAlias || keystorePwd) {
    apply plugin: 'de.gliderpilot.jnlp'
    jnlp {
        useVersions = true
        withXml {
            information {
                title project.name
                vendor project.group ?: project.name
            }
            security {
                'all-permissions'()
            }
        }
        signJarParams = [alias: keyAlias, storepass: keystorePwd]
    }
}
