buildscript {
    repositories {
        mavenCentral()
        jcenter()
        maven {
            name = "forge"
            url = "http://files.minecraftforge.net/maven"
        }
        maven {
            name = "sonatype"
            url = "https://oss.sonatype.org/content/repositories/snapshots/"
        }
    }
    dependencies {
        classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
    }
}

apply plugin: 'scala'
apply plugin: 'forge'
apply plugin: 'maven-publish'

ext.buildProps = file "build.properties"

buildProps.withReader {
    def prop = new Properties()
    prop.load(it)
    ext.config = new ConfigSlurper().parse prop
}

version = "${config.version.mod.major}.${config.version.mod.minor}.${config.version.mod.revis}"

if (System.getenv("TEAMCITY_BUILDCONF_NAME")?.equalsIgnoreCase("Production")) {
    group = "com.calclavia.resonantinduction"
    archivesBaseName = System.getenv("TEAMCITY_PROJECT_NAME")?.replaceAll(" ", "-")
} else {
    group = "dev.calclavia.resonantinduction"
    archivesBaseName = System.getenv("TEAMCITY_PROJECT_NAME")?.replaceAll(" ", "-") + "-" + System.getenv("TEAMCITY_BUILDCONF_NAME")
}

minecraft {
    version = "${config.version.minecraft}-${config.version.forge}"

    replaceIn "References.scala"
    replace "@MAJOR@", config.version.mod.major
    replace "@MINOR@", config.version.mod.minor
    replace "@REVIS@", config.version.mod.revis
    replace "@BUILD@", System.getenv("BUILD_NUMBER")
}

if (System.getenv("BUILD_NUMBER") != null)
    version += ".${System.getenv("BUILD_NUMBER")}"

processResources {
    inputs.property "version", project.version
    inputs.property "mcversion", project.minecraft.version

    // replace stuff in text files, not binary ones.
    from(sourceSets.main.resources.srcDirs) {
        include '**/*.info'

        // replace version and MCVersion
        // forge version is also accessible via project.minecraftforgeVersion
        // it contains the full minecraft version, including buildNumber
        expand 'version': project.version, 'mcversion': project.minecraft.version
    }

    // copy everything else, thats not text
    from 'build.properties'
}

task copyBuildXml(type: Copy) {
    from 'build.properties'
    into 'output'
}

/**
 * Generates a TeamCity XML changelog via the REST API.
 */
task("createChangelog").doLast {

    def teamCityURL = "http://ci.calclavia.com/"

    /**
     * Create a new file
     */
    def file = new FileOutputStream("output/changelog.xml")
    def out = new BufferedOutputStream(file)

    /**
     * Grab the build first, parse the XML to find the changelog XML URL
     */
    def changesXML = new XmlSlurper().parse(teamCityURL + "guestAuth/app/rest/changes?locator=build:(id:" + teamcity["teamcity.build.id"] + ")")
    def changes = changesXML.change

    /**
     * Add the XML definition header in the front of the file and remove all other occurrences of the XML header
     */
    out << ("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?><changes>")

    println("createChangelog: Identified " + changes.size() + " changes to be written into the changelog.")

    for (int i = 0; i < changes.size(); i++) {
        /**
         * Write each changelog XML into the URL
         */
        def changelogURL = teamCityURL + "guestAuth/app/rest/changes/id:" + changes[i].@id.text()
        out << new URL(changelogURL).getText().replaceAll("<\\?xml version=\"1\\.0\" encoding=\"UTF-8\" standalone=\"yes\"\\?>", "")
    }

    out << "</changes>"

    out.close()
}

jar {
    dependsOn copyBuildXml, createChangelog
    classifier = 'core'
    destinationDir = file 'output'
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            artifact jar

            artifact("output/changelog.xml") {
                classifier "changelog"
                extension "xml"
            }

            artifact("output/build.properties") {
                classifier "build"
                extension "properties"
            }
        }
    }
    repositories {
        maven {
            url "file://var/www/maven"
        }
    }
}

repositories {
    maven {
        name "Calclavia Maven"
        url "http://calclavia.com/maven"
    }
    maven {
        name = "forge"
        url = "http://files.minecraftforge.net/maven"
    }
    maven {
        name "Mobius Repo"
        url "http://mobiusstrip.eu/maven"
    }
    maven {
        name "chickenbones"
        url "http://chickenbones.net/maven"
    }
    maven { 
        name 'OC Maven'
        url = "http://maven.cil.li/" 
    }
	
    mavenCentral()
    maven {
        name = "ic2"
        url = "http://maven.ic2.player.to/"
    }
}

dependencies {
	if (System.getenv("TEAMCITY_BUILDCONF_NAME") == "Development")
	{
		//compile group: 'dev.calclavia.universalelectricity', name: 'universal-electricity', version: "${rootProject.config.version.universalelectricity}", classifier: "dev"
		compile group: 'dev.calclavia.resonantengine', name: 'resonant-engine', version: "${config.version.resonantengine}", classifier: "dev"
	}
	else
	{
		//compile group: 'com.calclavia.universalelectricity', name: 'universal-electricity', version: "${rootProject.config.version.universalelectricity}", classifier: "dev"
		compile group: 'com.calclavia.resonantengine', name: 'resonant-engine', version: "${config.version.resonantengine}", classifier: "dev"
	}
	
    testCompile 'junit:junit:4.11'
    testCompile 'jmock:jmock:1.1.0'
    compile "li.cil.oc:OpenComputers:MC1.7.10-1.4.0.+:api"
    compile "codechicken:CodeChickenLib:${config.version.minecraft}-${config.version.cclib}:dev"
    compile "codechicken:ForgeMultipart:${config.version.minecraft}-${config.version.fmp}:dev"
    compile "codechicken:CodeChickenCore:${config.version.minecraft}-${config.version.cccore}:dev"
    compile "codechicken:NotEnoughItems:${config.version.minecraft}-${config.version.nei}:dev"
    compile "mcp.mobius.waila:Waila:1.5.6_dev1_1.7.10"
    compile "net.industrial-craft:industrialcraft-2:2.2.654-experimental:dev"
}