plugins {
    id 'org.openjfx.javafxplugin' version '0.1.0'
}

repositories {
    mavenCentral()
    maven {
        url 'https://nexus.gluonhq.com/nexus/content/repositories/releases/'
    }
}

def attachVersion = "4.0.13"
def target = System.getProperty("substrate.target")
def classifier = "ios" == target ? target : "desktop"

configurations {
    substrate
}

dependencies {
    substrate "com.gluonhq:substrate:$substrateVersion"

    implementation "com.gluonhq:charm-glisten:6.0.6"
    implementation ("com.gluonhq.attach:display:$attachVersion:$classifier") {
        exclude group:"org.openjfx"
    }
    implementation ("com.gluonhq.attach:lifecycle:$attachVersion:$classifier") {
        exclude group:"org.openjfx"
    }
    if ("ios" == target) {
        implementation ("com.gluonhq.attach:statusbar:$attachVersion:$classifier") {
            exclude group:"org.openjfx"
        }
    } else {
        implementation ("com.gluonhq.attach:statusbar:$attachVersion") {
            exclude group:"org.openjfx"
        }
    }
    implementation ("com.gluonhq.attach:storage:$attachVersion:$classifier") {
        exclude group:"org.openjfx"
    }
    implementation ("com.gluonhq.attach:util:$attachVersion") {
        exclude group:"org.openjfx"
    }
}

javafx {
    version = "17.0.10"
    modules = [ "javafx.controls" ]
}

mainClassName = "com.gluonhq.substrate.test.Main"

task runScript(type: Exec) {
    dependsOn 'build'

    def javaHome = validateJavaHome()
    def graalvmHome = validateGraalvmHome()
    def javafxStaticSdk = validateJavafxStaticSdk()

    def expected = System.getProperty("expected")
    def skipsigning = System.getProperty("skipsigning")
    def consoleProcessLog = System.getProperty("consoleProcessLog")

    def imageClasspath = ""
    if (javafxStaticSdk != null) {
        imageClasspath = "${javafxStaticSdk}/lib/javafx.base.jar${File.pathSeparator}" +
                "${javafxStaticSdk}/lib/javafx.graphics.jar${File.pathSeparator}" +
                "${javafxStaticSdk}/lib/javafx.controls.jar${File.pathSeparator}" +
                project.sourceSets.main.runtimeClasspath.filter{!it.name.contains('javafx')}.asPath
    } else {
        imageClasspath = project.sourceSets.main.runtimeClasspath.asPath
    }

    def argsList = ["${javaHome}/bin/java", '-cp', project.configurations.substrate.asPath,
                    "-Dimagecp=${imageClasspath}",
                    "-Dgraalvm=${graalvmHome}"]
    if (javafxStaticSdk != null) {
        argsList += "-Djavafxsdk=${javafxStaticSdk}"
    }
    if (target != null) {
        argsList += ["-DtargetProfile=$target"]
    } else {
        argsList += ["-Dprism.sw=true"]
    }
    if (expected != null) {
        argsList += ["-Dexpected=$expected"]
    }
    if (skipsigning != null) {
        argsList += ["-Dskipsigning=$skipsigning"]
    }
    if (consoleProcessLog != null) {
        argsList += ["-DconsoleProcessLog=$consoleProcessLog", "-Dverbose=true"]
    }
    argsList += ["-Dmainclass=$mainClassName", 'com.gluonhq.substrate.SubstrateDispatcher']
    commandLine argsList
}
