plugins {
    // Apply the java-library plugin to add support for Java Library
    id 'java-library'
}

repositories {
	mavenCentral()
    jcenter()
    
    maven { url 'http://windcorp.ru/./maven' }
}

dependencies {
    implementation 'org.apache.commons:commons-math3:3.6.1'
    implementation 'com.google.guava:guava:30.0-jre'
    implementation 'net.sf.trove4j:trove4j:3.0.3'
    
    implementation 'ru.windcorp.fork.io.github.java-graphics:glm:1.0.1'

	// log4j
	implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.13.3'
	implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.13.3'

	testImplementation 'junit:junit:4.12'
	
	// See also LWJGL dependencies below
}

/*
 *          LWJGL
 * (auto-generated script)
 *   ((here be dragons))
 */

import org.gradle.internal.os.OperatingSystem

project.ext.lwjglVersion = "3.2.3"

switch (OperatingSystem.current()) {
	case OperatingSystem.LINUX:
		def osArch = System.getProperty("os.arch")
		project.ext.lwjglNatives = osArch.startsWith("arm") || osArch.startsWith("aarch64")
			? "natives-linux-${osArch.contains("64") || osArch.startsWith("armv8") ? "arm64" : "arm32"}"
			: "natives-linux"
		break
	case OperatingSystem.MAC_OS:
		project.ext.lwjglNatives = "natives-macos"
		break
	case OperatingSystem.WINDOWS:
		project.ext.lwjglNatives = System.getProperty("os.arch").contains("64") ? "natives-windows" : "natives-windows-x86"
		break
}

dependencies {
	implementation platform("org.lwjgl:lwjgl-bom:$lwjglVersion")

	implementation "org.lwjgl:lwjgl"
	implementation "org.lwjgl:lwjgl-assimp"
	implementation "org.lwjgl:lwjgl-bgfx"
	implementation "org.lwjgl:lwjgl-glfw"
	implementation "org.lwjgl:lwjgl-nanovg"
	implementation "org.lwjgl:lwjgl-nuklear"
	implementation "org.lwjgl:lwjgl-openal"
	implementation "org.lwjgl:lwjgl-opengl"
	implementation "org.lwjgl:lwjgl-par"
	implementation "org.lwjgl:lwjgl-stb"
	implementation "org.lwjgl:lwjgl-vulkan"
	runtimeOnly "org.lwjgl:lwjgl::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-assimp::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-bgfx::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-glfw::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-nanovg::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-nuklear::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-openal::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-opengl::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-par::$lwjglNatives"
	runtimeOnly "org.lwjgl:lwjgl-stb::$lwjglNatives"
	if (lwjglNatives == "natives-macos") runtimeOnly "org.lwjgl:lwjgl-vulkan::$lwjglNatives"
}

// LWJGL END

jar {
    manifest {
        attributes(
        	"Main-Class": "ru.windcorp.progressia.client.ProgressiaClientMain",
        	"Class-Path": configurations.runtimeClasspath.collect { "lib/" + it.getName() }.join(' ')
        )
    }
}

/*
 * Copies runtime dependencies to a prespecified location so they can be packaged properly.
 */
task copyLibs(type: Copy) {
    into "${libsDir}/lib"
    from configurations.runtimeClasspath
}




build.dependsOn(copyLibs)



configurations {
    packageOnly 
    packageLibraries.extendsFrom runtimeClasspath
    packageLibraries.extendsFrom packageOnly
}

dependencies {
    def archs = ['natives-linux', 'natives-linux-arm64', 'natives-linux-arm32', 'natives-macos', 'natives-windows', 'natives-windows-x86']
    
    archs.each { arch ->
        packageOnly "org.lwjgl:lwjgl::$arch"
        packageOnly "org.lwjgl:lwjgl-assimp::$arch"
        packageOnly "org.lwjgl:lwjgl-bgfx::$arch"
        packageOnly "org.lwjgl:lwjgl-glfw::$arch"
        packageOnly "org.lwjgl:lwjgl-nanovg::$arch"
        packageOnly "org.lwjgl:lwjgl-nuklear::$arch"
        packageOnly "org.lwjgl:lwjgl-openal::$arch"
        packageOnly "org.lwjgl:lwjgl-opengl::$arch"
        packageOnly "org.lwjgl:lwjgl-par::$arch"
        packageOnly "org.lwjgl:lwjgl-stb::$arch"
    }
    
    packageOnly "org.lwjgl:lwjgl-vulkan::natives-macos"
}

task copyLibsForPackaging(type: Copy) {
    into "${libsDir}/lib"
    from configurations.packageLibraries
}

task createPackages(type: Exec) {
    commandLine './buildPackages.sh'
}

java {
    targetCompatibility = JavaVersion.VERSION_1_8
}

createPackages.dependsOn(copyLibsForPackaging)

