plugins {
    id 'java'
    id 'com.github.johnrengelman.shadow' version '8.1.1'
}

// Get the library path from a Gradle property or default to "libs"
def libPath = project.hasProperty('libPath') ? project.getProperty('libPath') : '/user/local/share/Just-Forge-2D/libs'

def engineJar = ""

def osName = System.getProperty("os.name").toLowerCase()
def osArch = System.getProperty("os.arch").toLowerCase()

if (osName.contains("linux")) {
    engineJar = 'Just-Forge-2D-release-1.0-linux'
} else if (osName.contains("mac")) {
    engineJar = 'Just-Forge-2D-SNAPSHOT-1.2-macos'
} else if (osName.contains("win")) {
    if (osArch.contains("amd64")) {
        engineJar = 'Just-Forge-2D-release-1.0-win64'
    } else if (osArch.contains("x86")) {
        engineJar = 'Just-Forge-2D-SNAPSHOT-1.2-win32'
    } else {
        throw new GradleException("Unsupported Windows architecture: " + osArch)
    }
} else {
    throw new GradleException("Unsupported operating system: " + osName)
}

repositories {
    flatDir {
        dirs libPath // Use the configurable library path
    }
    mavenCentral()
}

shadowJar {
    archiveClassifier.set('all')
    mergeServiceFiles()
    manifest {
        attributes 'Main-Class': 'Just_Forge_2D.Main'
    }
}

dependencies {
    if (engineJar != null) {
        implementation name: engineJar
    } else {
        throw new GradleException("Engine JAR could not be resolved")
    }
}