plugins {
    id 'com.matthewprenger.cursegradle'
    id 'com.modrinth.minotaur'
}

loom {
    mods {
        main { // to match the default mod generated for Forge
            sourceSet project.sourceSets.main
            sourceSet project(':common').sourceSets.main
        }
    }
    runs {
        client {
            vmArgs "-Xmx1G"
            vmArgs "-Xms1G"
            property("mixin.debug.export", "true")
        }
    }
}

def copyJarNameConsistent = tasks.register('copyJarNameConsistent', Copy) {
    from remapJar // shortcut for createJar.outputs.files
    into project.file("build/libs")
    rename { name -> "modernfix-" + project.name + "-latest.jar" }
}

def copyJarToBin = tasks.register('copyJarToBin', Copy) {
    from remapJar // shortcut for createJar.outputs.files
    into rootProject.file("bin")
    mustRunAfter "copyJarNameConsistent"
}

tasks.build.dependsOn(copyJarToBin, copyJarNameConsistent)

def isBeta = project.version.toString().contains("beta")

curseforge {
    if (System.getenv("CURSEFORGE_TOKEN") != null) {
        apiKey = System.getenv("CURSEFORGE_TOKEN")
        project {
            id = "790626"
            changelog = file("${rootDir}/CHANGELOG.md")
            changelogType = "markdown"
            releaseType = isBeta ? "beta" : "release"
            addGameVersion project.name.capitalize()
            gameVersionStrings.addAll(supported_minecraft_versions.tokenize(","))
            mainArtifact remapJar
        }
    }
}

modrinth {
    token = System.getenv("MODRINTH_TOKEN")
    projectId = "modernfix" // This can be the project ID or the slug. Either will work!
    versionType = isBeta ? "beta" : "release" // This is the default -- can also be `beta` or `alpha`
    uploadFile = remapJar
    gameVersions = supported_minecraft_versions.tokenize(",")
    loaders = [project.name]
    changelog.set(provider { file("${rootDir}/CHANGELOG.md").getText('UTF-8') })
}

tasks.curseforge.dependsOn(rootProject.generateChangelog)
tasks.modrinth.dependsOn(rootProject.generateChangelog)

tasks.register('publishToModSites') {
    publishToModSites.dependsOn(tasks.modrinth)
    publishToModSites.dependsOn(tasks.curseforge)
}