buildscript {
    repositories {
        jcenter()
        maven {
            name = "forge"
            url = "http://files.minecraftforge.net/maven"
        }
    }
    dependencies {
        classpath 'net.minecraftforge.gradle:ForgeGradle:2.3-SNAPSHOT'
        classpath "gradle.plugin.se.bjurr.gitchangelog:git-changelog-gradle-plugin:1.47"
    }
}

plugins {
    id "com.matthewprenger.cursegradle" version "1.1.0"
    id 'se.bjurr.gitchangelog.git-changelog-gradle-plugin' version '1.64'
}
apply plugin: 'idea'
apply plugin: 'maven'

allprojects {
    apply plugin: 'net.minecraftforge.gradle.forge'

    version = version_major + '.' + version_minor + '.' + version_patch
    if (System.getenv("BUILD_NUMBER") != null) {
        version = version + "." + System.getenv("BUILD_NUMBER")
    } else {
        version = version + ".9999"
    }

    minecraft {
        version = mcversion + "-" + forgeversion
        mappings = mcp_mappings
        runDir = "run"

        replace '@VERSION@', project.version
    }

    group = "binnie" // http://maven.apache.org/guides/mini/guide-naming-conventions.html

    sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
    compileJava {
        sourceCompatibility = targetCompatibility = JavaVersion.VERSION_1_8
    }

    repositories {
        maven {
            name = "jei"
            url "http://dvs1.progwml6.com/files/maven"
        }
        maven {
            name = "ic2, forestry"
            url = "http://maven.ic2.player.to/"
        }
        maven {
            name = "CurseForge"
            url = "https://minecraft.curseforge.com/api/maven/"
        }
		maven {
			url = "https://mod-buildcraft.com/maven"
		}
		maven {
			url "http://maven.blamejared.com"
		}
    }

    dependencies {
        deobfCompile("net.sengir.forestry:forestry_${forestry_mcversion}:${forestry_version}") {
            transitive = false
        }
        deobfProvided "mezz.jei:jei_${jei_mcversion}:${jei_version}:api"
        runtime "mezz.jei:jei_${jei_mcversion}:${jei_version}"
        runtime "jei-bees:jeibees:${jeibees_version}:mc${jeibees_mcversion}"
        deobfProvided "net.industrial-craft:industrialcraft-2:${ic2_version}:api"
        runtime "net.industrial-craft:industrialcraft-2:${ic2_version}"
        deobfProvided "CraftTweaker2:CraftTweaker2-API:${crafttweaker_version}"
		deobfProvided "com.mod-buildcraft:buildcraft-api:${buildcraft_version}"
    }

    processResources {
        // this will ensure that this task is redone when the versions change.
        inputs.property "version", project.version
        inputs.property "mcversion", project.minecraft.version

        // replace stuff in mcmod.info, nothing else
        from(sourceSets.main.resources.srcDirs) {
            include 'mcmod.info'

            // replace version and mcversion
            expand 'version': project.version, 'mcversion': project.minecraft.version
        }

        // copy everything else, thats not the mcmod.info
        from(sourceSets.main.resources.srcDirs) {
            exclude 'mcmod.info'
        }
    }

    // prevent java 8's strict doclint for javadocs from failing builds
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
    }
}

def mainProjects = [':core', ':core-api', ':botany', ':botany-api', ':design', ':design-api', ':extrabees', ':extratrees', 'extratrees-api', ':genetics', 'genetics-api']
        .collect{ subproject -> project(subproject) }
mainProjects.each { subproject -> evaluationDependsOn( subproject.path ) }
jar.dependsOn mainProjects.tasks['classes']
jar {
    mainProjects.each { subproject ->
        from subproject.sourceSets.main.output.classesDirs
        from subproject.sourceSets.api.output.classesDirs
        from subproject.sourceSets.main.output.resourcesDir
        from subproject.sourceSets.api.output.resourcesDir
    }
}

task sourcesJar(type: Jar, dependsOn: mainProjects.tasks['classes']) {
    classifier = 'sources'
    mainProjects.each { subproject ->
        from(subproject.sourceSets.main.allSource) {
            exclude 'assets'
            exclude 'mcmod.info'
        }
        from(subproject.sourceSets.api.allSource)
    }
}

task copyJars(type: Copy) {
    from mainProjects.collect { it.tasks.withType(Jar) }
    into "$buildDir/libs"
}

artifacts {
    archives sourcesJar
}

uploadArchives {
    repositories {
        if (project.hasProperty('mavenDir')) {
            mavenDeployer {
                repository(url: "file://" + mavenDir)
            }
        } else {
            println 'Archives upload disabled, mavenDir in gradle.properties is missing.'
        }
    }
}

task makeChangelog(type: se.bjurr.gitchangelog.plugin.gradle.GitChangelogTask) {
    file = new File("changelog.html")
    untaggedName = "Current release ${project.version}"
    fromCommit = "dbc5c1a564c39e4a9c042411d1bdbe450985ce2c"
    toRef =  "HEAD"
    templateContent = file('changelog.mustache').getText('UTF-8')
}

curseforge {
    apiKey = project.hasProperty('curseforge_apikey') ? project.curseforge_apikey : '0'
    project {
        id = curse_project_id
        changelog = file('changelog.html')
        changelogType = 'html'
        releaseType = 'alpha'
        relations {
            requiredDependency 'forestry'
        }
    }
}

afterEvaluate {
    tasks.curseforge223525.dependsOn.add(makeChangelog)
}
