buildscript {
    ext.tornadofx_version = '1.7.10'
    ext.jna_version = '4.1.0'
    ext.purejavacomm_version = '0.0.18'
    ext.controlsfx_version = '8.40.12'

    ext.java_home = System.properties.'java.home'
    ext.pkg_source = "$buildDir/packagesrc"
    ext.pkg_outDir = "$buildDir/javapackage"
    ext.dist_source = "$pkg_source/demobench-$version"
    ext.pkg_version = version.indexOf('-') >= 0 ? version.substring(0, version.indexOf('-')) : version
    ext.pkg_macosxKeyUserName = 'R3CEV'

    repositories {
        mavenLocal()
        mavenCentral()
    }
}

apply plugin: 'java'
apply plugin: 'kotlin'
apply plugin: 'application'

evaluationDependsOn(':tools:explorer:capsule')

mainClassName = 'net.corda.demobench.DemoBench'
applicationDefaultJvmArgs = [
    '-Djava.util.logging.config.class=net.corda.demobench.config.LoggingConfig',
    '-Dorg.jboss.logging.provider=slf4j'
]

repositories {
    flatDir {
        dirs 'libs'
    }
    maven {
        jcenter()
        url 'http://www.sparetimelabs.com/maven2'
    }
}

dependencies {
    // TornadoFX: A lightweight Kotlin framework for working with JavaFX UI's.
    compile "no.tornado:tornadofx:$tornadofx_version"

    // Controls FX: more java FX components http://fxexperience.com/controlsfx/
    compile "org.controlsfx:controlsfx:$controlsfx_version"

    compile "net.corda.plugins:cordform-common:$gradle_plugins_version"

    compile project(':client:rpc')
    compile project(':finance')

    compile "com.h2database:h2:$h2_version"
    compile "net.java.dev.jna:jna-platform:$jna_version"
    compile "com.google.guava:guava:$guava_version"
    compile "com.sparetimelabs:purejavacomm:$purejavacomm_version"
    compile "org.slf4j:log4j-over-slf4j:$slf4j_version"
    compile "org.slf4j:jul-to-slf4j:$slf4j_version"
    compile "org.apache.logging.log4j:log4j-slf4j-impl:$log4j_version"
    compile "org.apache.logging.log4j:log4j-core:$log4j_version"
    compile "com.typesafe:config:$typesafe_config_version"

    // FontAwesomeFX: icons in the form of a font.
    compile "de.jensd:fontawesomefx-fontawesome:4.7.0-5"

    // JediTerm: the terminal emulator used in IntelliJ. We have forked it and tweaked it, see https://github.com/corda/jediterm
    compile ':terminal-331a005d6793e52cefc9e2cec6774e62d5a546b1'
    compile ':pty4j-0.7.2'

    testCompile project(':test-utils')
    testCompile project(':webserver')

    testCompile "junit:junit:$junit_version"
    testCompile "org.jetbrains.kotlin:kotlin-test:$kotlin_version"
    testCompile "org.assertj:assertj-core:${assertj_version}"
    testCompile "org.mockito:mockito-core:$mockito_version"
}

jar {
    manifest {
        attributes(
            'Main-Class': mainClassName,
            'Class-Path': configurations.runtime.collect { it.getName() }.join(' '),
            'Automatic-Module-Name': 'net.corda.tools.demobench'
        )
    }
}

test {
    systemProperty 'java.util.logging.config.class', 'net.corda.demobench.config.LoggingConfig'
    systemProperty 'org.jboss.logging.provider', 'slf4j'
}

distributions {
    main() {
        contents {
            into('lib/linux') {
                from 'libs/linux'
                fileMode = 0555
            }
            into('lib/macosx') {
                from 'libs/macosx'
                fileMode = 0555
            }
            into('lib/win') {
                from 'libs/win'
                fileMode = 0555
            }
            from(project(':tools:explorer:capsule').tasks.buildExplorerJAR) {
                rename 'node-explorer-(.*)', 'node-explorer.jar'
                into 'explorer'
                fileMode = 0444
            }
            from(project(':node:capsule').tasks.buildCordaJAR) {
                rename 'corda-(.*)', 'corda.jar'
                into 'corda'
                fileMode = 0444
            }
            from(project(':webserver:webcapsule').tasks.buildWebserverJar) {
                rename 'corda-webserver-(.*)', 'corda-webserver.jar'
                into 'corda'
                fileMode = 0444
            }
            from(project(':finance').tasks.jar) {
                rename 'corda-finance-(.*)', 'corda-finance.jar'
                into 'cordapps'
                fileMode = 0444
            }
            from(project(':samples:bank-of-corda-demo').jar) {
                rename 'bank-of-corda-demo-(.*)', 'bank-of-corda.jar'
                into 'cordapps'
                fileMode = 0444
            }
        }
    }
}

/*
 * Bundles the application using JavaPackager,
 * using the ZIP distribution as source.
 */
task javapackage(dependsOn: distZip) {

    doLast {
        delete([pkg_source, pkg_outDir])

        copy {
            from(zipTree(distZip.outputs.files.singleFile))
            into pkg_source
        }

        copy {
            /*
             * Copy non-text formats "as-is".
             */
            from("$projectDir/package") {
                exclude '**/*.spec'
                exclude '**/*.sh'
                exclude '**/*.wsf'
                exclude '**/*.manifest'
            }
            into "$pkg_source/package"
        }

        copy {
            /*
             * Expand tokens for text formats.
             */
            from("$projectDir/package") {
                include '**/*.spec'
                include '**/*.sh'
                include '**/*.wsf'
                include '**/*.manifest'
            }
            filter { line ->
                line.replaceAll('@pkg_version@', pkg_version)
                    .replaceAll('@signingKeyUserName@', pkg_macosxKeyUserName)
            }
            into "$pkg_source/package"
        }

        ant.taskdef(
            resource: 'com/sun/javafx/tools/ant/antlib.xml',
            classpath: "$pkg_source:$java_home/../lib/ant-javafx.jar"
        )

        ant.deploy(nativeBundles: packageType, outdir: pkg_outDir, outfile: 'DemoBench', verbose: 'true') {
            application(name: 'Corda DemoBench', version: pkg_version, mainClass: mainClassName)
            info(title: 'Corda DemoBench', vendor: 'R3', description: 'A sales and educational tool for demonstrating Corda.')
            //noinspection GroovyAssignabilityCheck
            resources {
                fileset(dir: "$dist_source/lib", type: 'jar') {
                    include(name: '*.jar')
                }

                fileset(dir: "$dist_source/lib", type: 'native') {
                    include(name: "macosx/**/*.dylib")
                    include(name: "win/**/*.dll")
                    include(name: "win/**/*.exe")
                    include(name: "linux/**/*.so")
                }

                fileset(dir: dist_source, type: 'data') {
                    include(name: 'corda/*.jar')
                    include(name: 'cordapps/*.jar')
                    include(name: 'explorer/*.jar')
                }

                fileset(dir: "$pkg_source/package", type: 'data') {
                    include(name: "bugfixes/**")
                }
            }

            // This is specific to MacOSX packager.
            bundleArgument(arg: 'mac.signing-key-user-name', value: pkg_macosxKeyUserName)

            platform {
                //noinspection GroovyAssignabilityCheck
                property(name: 'java.util.logging.config.class', value: 'net.corda.demobench.config.LoggingConfig')
                //noinspection GroovyAssignabilityCheck
                property(name: 'org.jboss.logging.provider', value: 'slf4j')
            }

            preferences(install: true)
        }
    }
}
