buildscript {
	repositories {
		gradlePluginPortal()
	}
	dependencies {
// using jpackage only works if the JDK version is 14 or higher.
// your JAVA_HOME environment variable may also need to be a JDK with version 14 or higher.
		if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_14)) {
			classpath "org.beryx:badass-runtime-plugin:1.12.7"
		}
	}
}
if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_14)) {
	apply plugin: 'org.beryx.runtime'
}
else {
	apply plugin: 'application'
}

sourceSets.main.resources.srcDirs += [ rootProject.file('assets').path ]
mainClassName = 'com.last.commit.lwjgl3.Lwjgl3Launcher'
eclipse.project.name = appName + '-lwjgl3'
sourceCompatibility = 17

dependencies {
	implementation "com.badlogicgames.gdx:gdx-backend-lwjgl3:$gdxVersion"
	implementation "com.badlogicgames.gdx:gdx-freetype-platform:$gdxVersion:natives-desktop"
	implementation "com.badlogicgames.gdx:gdx-platform:$gdxVersion:natives-desktop"
	implementation project(':core')
}

def os = System.properties['os.name'].toLowerCase()

run {
	workingDir = rootProject.file('assets').path
	setIgnoreExitValue(true)

	if (os.contains('mac')) {
		// Required to run LWJGL3 Java apps on MacOS
		jvmArgs += "-XstartOnFirstThread"
	}
}

jar {
// sets the name of the .jar file this produces to the name of the game or app.
	archiveBaseName.set(appName)
// using 'lib' instead of the default 'libs' appears to be needed by jpackageimage.
	destinationDirectory = file("$project.buildDir/lib")
// the duplicatesStrategy matters starting in Gradle 7.0; this setting works.
	duplicatesStrategy(DuplicatesStrategy.EXCLUDE)
	dependsOn configurations.runtimeClasspath
	from { configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) } }
// these "exclude" lines remove some unnecessary duplicate files in the output JAR.
	exclude('META-INF/INDEX.LIST', 'META-INF/*.SF', 'META-INF/*.DSA', 'META-INF/*.RSA')
	dependencies {
		exclude('META-INF/INDEX.LIST', 'META-INF/maven/**')
	}
// setting the manifest makes the JAR runnable.
	manifest {
		attributes 'Main-Class': project.mainClassName
	}
// this last step may help on some OSes that need extra instruction to make runnable JARs.
	doLast {
		file(archiveFile).setExecutable(true, false)
	}
}

if (JavaVersion.current().isCompatibleWith(JavaVersion.VERSION_14)) {
    jpackageImage.dependsOn jar

    runtime {
        options.set(['--strip-debug',
                     '--compress', '2',
                     '--no-header-files',
                     '--no-man-pages',
                     '--strip-native-commands',
                     '--vm', 'server'])
// you could very easily need more modules than this one.
// use the lwjgl3:suggestModules task to see which modules may be needed.
        modules.set([
                'jdk.unsupported'
        ])
        distDir.set(file(buildDir))
        jpackage {
            imageName = appName
// you can set this to false if you want to build an installer, or keep it as true to build just an app.
            skipInstaller = true
// this may need to be set to a different path if your JAVA_HOME points to a low JDK version.
            jpackageHome = javaHome.getOrElse("")
            mainJar = jar.archiveFileName.get()
            if (os.contains('win')) {
                imageOptions = ["--icon", "icons/logo.ico"]
            } else if (os.contains('nix') || os.contains('nux') || os.contains('bsd')) {
                imageOptions = ["--icon", "icons/logo.png"]
            } else if (os.contains('mac')) {
                imageOptions = ["--icon", "icons/logo.icns", "--java-options", "\"-XstartOnFirstThread\""]
            }
        }
    }
}

// Equivalent to the jar task; here for compatibility with gdx-setup.
task dist(dependsOn: [jar]) {
}
