plugins {
    id 'java'
}

// Transfer version properties from settings.gradle to project
project.gradle.ext.getProperties().each { prop ->
    rootProject.ext.set(prop.key, prop.value)
}

// Version string for archives
rootProject.ext.versionStr = rootProject.mod_version + "-" + rootProject.minecraft_version
rootProject.allprojects {
    version = (it == rootProject ? "" : it.name + "-") + rootProject.versionStr
    group = rootProject.maven_group

    // Custom javadoc tags for all subprojects
    plugins.withType(JavaPlugin) {
        javadoc {
            options.tags(
                'todo:X"',
                'apiNote:a:API Note:',
                'implSpec:a:Implementation Requirements:',
                'implNote:a:Implementation Note:'
            )
        }
    }
}

// Create build.properties with preprocessor definitions
def writePreprocessorDefinitions() {
    StringBuilder sb = new StringBuilder()
    sb.append("# DON'T TOUCH THIS FILE, This is handled by the build script\n")

    gradle.ext.mcVers.eachWithIndex { ver, idx ->
        sb.append("MC_${ver.replace('.', '_')}=${idx}\n")
        if (gradle.ext.mcIndex == idx)
            sb.append("MC_VER=${idx}\n")
    }

    if (rootProject.mod_version.toLowerCase().contains("dev")) {
        sb.append("DEV_BUILD=\n")
    }

    new File(rootDir, "build.properties").text = sb.toString()
}
writePreprocessorDefinitions()

// Wire JVMDowngrader to process remapped jars
gradle.projectsEvaluated {
    rootProject.subprojects.each {
        if (it.tasks.findByName('remapJar') == null) return
        it.tasks.downgradeJar.inputFile = it.tasks.remapJar.archiveFile
        it.tasks.jar.finalizedBy(it.tasks.remapJar)
        it.tasks.remapJar.finalizedBy(it.tasks.shadeDowngradedApi)
    }
}
