plugins {
    id 'java-library'
}

allprojects {
    repositories {

        // SpongePowered Maven repository for Mixins
        maven {
            url "https://repo.spongepowered.org/maven/"
        }
        // FabricMC Maven repository for Fabric Loader
        maven {
            url "https://maven.fabricmc.net/"
        }

        // Maven Central for core dependencies
        mavenCentral() {
            content {
                // Force ASM and ME to come from the fabric maven.
                // This ensures that the version has been mirrored for use by the launcher/installer.
                excludeGroupByRegex "org.ow2.asm"
                excludeGroupByRegex "io.github.llamalad7"
            }
        }
    }
}

version = "$provider_version"
group = "ru.nern"
archivesBaseName = "cosmicreach-fabric-modloader"

dependencies {
    api 'com.google.guava:guava:33.0.0-jre'
    api 'com.google.code.gson:gson:2.9.1'

    // Fabric dependencies
    api "net.fabricmc:fabric-loader:$loader_version"
    api "net.fabricmc:tiny-mappings-parser:0.2.2.14"
    api "net.fabricmc:access-widener:2.1.0"

    // Mixin dependencies
    api "org.ow2.asm:asm:$asm_version"
    api "org.ow2.asm:asm-analysis:$asm_version"
    api "org.ow2.asm:asm-commons:$asm_version"
    api "org.ow2.asm:asm-tree:$asm_version"
    api "org.ow2.asm:asm-util:$asm_version"
    api("net.fabricmc:sponge-mixin:$mixin_version") {
        exclude module: 'launchwrapper'
        exclude group: 'com.google.guava'
        exclude group: 'com.google.code.gson'
    }
    api annotationProcessor("io.github.llamalad7:mixinextras-fabric:$mixin_extras_version")
}

sourceSets {
    main {
        java {
            srcDir 'src'
        }
    }
}

jar {
    manifest {
        attributes(
                'Class-Path': configurations.runtimeClasspath.collect { it.getName() }.join(' '),
                'Specification-Version': 8.0,
                'Multi-Release': 'true'
        )
    }
}

tasks.register('copyDependencies', Copy) {
    group 'build'
    from configurations.runtimeClasspath
    into "release/deps/"
}

assemble {
    group 'build'
    dependsOn 'jar'
    dependsOn 'copyDependencies'
}

tasks.register('generateLaunchScripts') {
    doLast {
        new File("release/", "launch.sh").text =
                "java -Dfabric.skipMcProvider=true -classpath \"/cosmic-reach.jar:cosmicreach-fabric-modloader-${provider_version}.jar:deps/*\" net.fabricmc.loader.launch.knot.KnotClient"
        new File("release/", "launch.bat").text =
                "java -Dfabric.skipMcProvider=true -classpath \"/cosmic-reach.jar;cosmicreach-fabric-modloader-${provider_version}.jar;deps/*\" net.fabricmc.loader.launch.knot.KnotClient"
    }
}

tasks.register('buildAndCopy', Copy) {
    dependsOn generateLaunchScripts
    group 'build'
    dependsOn 'assemble'
    from "build/libs/"
    into "release/"
}