buildscript {
    repositories {
        mavenCentral()
        maven {
            name = "forge"
            url = "https://files.minecraftforge.net/maven"
        }
        maven {
            name = "sonatype"
            url = "https://oss.sonatype.org/content/repositories/snapshots/"
        }
        jcenter()
    }
    dependencies {
        classpath 'net.minecraftforge.gradle:ForgeGradle:1.2-SNAPSHOT'
        classpath 'de.undercouch:gradle-download-task:1.2'
        classpath 'com.jfrog.bintray.gradle:gradle-bintray-plugin:1.6'
    }
}

plugins {
  id 'org.jetbrains.kotlin.jvm' version '1.3.11'
  id 'com.matthewprenger.cursegradle' version '1.0.8'
}

apply plugin: 'forge'
apply plugin: 'de.undercouch.download'
apply plugin: 'java'
apply plugin: 'com.jfrog.bintray'
apply plugin: 'maven-publish'

apply from: 'gradle.properties'

group = GROUP
archivesBaseName = ARCHIVE_NAME
version = MAJORVERSION + "." + MINORVERSION + "." + REVISION

targetCompatibility = '1.8'
sourceCompatibility = '1.8'

sourceSets {
    main {
        java.srcDirs = ['src/main/java']
        resources.srcDirs = ['src/main/resources']
        resources.excludes = ['*']
        kotlin.srcDirs = ['src/main/java']
    }
}

minecraft {
    version = "1.7.10-10.13.4.1614-1.7.10"
    runDir = "run"

    // TODO(Baughn): After moving to 1.10, this should be replaced with the shadow plugin,
    // e.g. as used in https://github.com/Emberwalker/Laundarray/blob/master/build.gradle
    srgExtra "PK: org/apache/commons/math3 mods/eln/libs/org/apache/commons/math3"
    srgExtra "PK: kotlin mods/eln/libs/kotlin"
    srgExtra "PK: org/jetbrains/annotations mods/eln/libs/annotations"

    replaceIn "Version.java"
    replace "@VERSION@", project.version
    replace "@MAJORVERSION@", MAJORVERSION
    replace "@MINORVERSION@", MINORVERSION
    replace "@REVISION@", REVISION
}

configurations {
    external
    compile.extendsFrom external
    shade
    compile.extendsFrom shade
}

repositories {
    mavenCentral()
    maven {
        name = "codechicken"
        url = "http://chickenbones.net/maven"
    }
    maven {
        name = "mobiusstrip"
        url = "http://default.mobiusstrip.eu/maven/"
    }
}

dependencies {
    external files("libs/commons-math3-3.3.jar")
    shade "org.jetbrains.kotlin:kotlin-stdlib-jdk8"
    compile "mcp.mobius.waila:Waila:1.5.11-RC2-NONEI_1.7.10:dev"
}

jar {
    manifest {
        attributes 'Built-By': 'Electrical-Age Team'
        attributes 'Build-Date': new Date().format("yyyy-MM-dd HH:mm:ss")
        attributes 'Built-JDK': System.getProperty('java.version')
        attributes 'Implementation-Title': project.name
        attributes 'Implementation-URL': 'https://github.com/Electrical-Age'
        attributes 'Implementation-Vendor': 'Electrical-Age Team'
        attributes 'Implementation-Vendor-Id': 'net.electricalage.eln'
        attributes 'Project-Url': 'https://electrical-age.net/'
        attributes 'Source-Compatibility': project.sourceCompatibility
        attributes 'Target-Compatibility': project.targetCompatibility
    }

    // Copy libs jar files
    from { configurations.external.collect { it.isDirectory() ? it : zipTree(it) } }
    exclude(['dan200/**', 'ic2/**', 'li/**', 'buildcraft/**'])

    // Add README and LICENSE to the release jar
    from(['README.md', 'LICENSE.md'])

    // Asset whitelist to copy the required asset files into the release jar.
    // Ignore files not used in Minecraft to reduce the release jar size.
    // Copy main files
    from('src/main/resources/assets/eln/') {
        include 'logo.png'
        include 'sounds.json'
        into 'assets/eln/'
    }

    // Copy language files
    from('src/main/resources/assets/eln/lang') {
        include '*.lang'
        into 'assets/eln/lang'
    }

    // Copy model
    from('src/main/resources/assets/eln/model') {
        exclude '_Common'
        exclude '_TEMPLATES'
        exclude 'export_*.png'
        include '**/*.mtl'
        include '**/*.obj'
        include '**/*.txt'
        include '**/*.png'
        into 'assets/eln/model'
    }

    // Copy sounds
    from('src/main/resources/assets/eln/sounds') {
        include '*.ogg'
        into 'assets/eln/sounds'
    }

    // Copy sprites
    from('src/main/resources/assets/eln/sprites') {
        include '**/*.png'
        include '**/*.xcf'
        into 'assets/eln/sprites'
    }

    // Copy sprites
    from('src/main/resources/assets/eln/textures') {
        include '**/*.png'
        into 'assets/eln/textures'
    }
    
    configurations.shade.each { dep ->
        from(project.zipTree(dep)){
            exclude 'META-INF', 'META-INF/**'
        }
    }
}

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
    }
}

clean {
    // Clean generated/built files. Saves are NOT deleted.
    delete("${minecraft.runDir}/logs")
    delete("${minecraft.runDir}/crash-reports")
}

def getMinecratfDir() {
    String outDir = ""
    final String osName = System.getProperty("os.name").toLowerCase()
    if (osName.contains('windows'))
        outDir = System.getenv("APPDATA") + "/.minecraft"
    else if (osName.contains('os x'))
        outDir = System.getProperty("user.home") + "/Library/Application Support/minecraft"
    else if (osName.contains('linux'))
        outDir = System.getProperty("user.home") + "/.minecraft"
    return outDir
}

// Custom task to build and copy the mod Jar to the default local Minecraft folder
task buildAndCopyJar(dependsOn: 'build', type: Copy) {
    group = 'ELN'
    description = 'Build and copy the mod Jar to the default local Minecraft folder.'

    outputs.upToDateWhen { false } // Force to run this task
    duplicatesStrategy = DuplicatesStrategy.INCLUDE // Overwrite the file if exists

    from("build/libs")
    into(getMinecratfDir() + "/mods")
    include("*.jar")
}

import de.undercouch.gradle.tasks.download.Download

// Download the tutorial map as a zip file to the local 'run/saves' folder
task downloadTutoMap(type: Download) {
    group = 'ELN'
    description = 'Download the tutorial map as a zip file to the local \'run/saves\' folder.'

    src(MAPURL)
    dest(new File(minecraft.runDir + "/saves", MAPURL.substring(MAPURL.lastIndexOf("/") + 1, MAPURL.length())))
    overwrite true
}

// Unzip the local tutorial map after downloading it. Overwrite the map content if already exist
task unzipTutoMap(dependsOn: downloadTutoMap, type: Copy) {
    group = 'ELN'
    description = 'Unzip the local tutorial map after downloading it.'

    outputs.upToDateWhen { false } // Force to run this task

    from zipTree(downloadTutoMap.dest)
    into(minecraft.runDir + "/saves")
}

// Update the master language file
task updateMasterLanguageFile(type: JavaExec, dependsOn: 'classes') {
    group = 'ELN'
    description = 'Generate or update the master language file.'

    main = 'mods.eln.i18n.LanguageFileUpdater'
    classpath = sourceSets.main.runtimeClasspath
    args = ['./src', './src/main/resources/assets/eln/lang/en_US.lang']
}

jar.dependsOn(updateMasterLanguageFile)

publishing {
    publications {
        mod(MavenPublication) {
            from components.java
            groupId GROUP
            artifactId ARCHIVE_NAME
            version project.version
        }
    }
}

bintrayUpload {}.dependsOn(build)

bintray {
    user = System.getenv('BINTRAY_USER')
    key = System.getenv('BINTRAY_KEY')
    publications = ['mod']
    pkg {
        repo = 'eln'
        name = 'ElectricalAge'
        userOrg = 'electrical-age'
        licenses = ['LGPL V3.0', 'CC BY-NC-SA 3.0']
        vcsUrl = 'https://github.com/Electrical-Age/ElectricalAge.git'
        version {
            name = project.version
        }
    }
}

curseforge {
  // Put this in ~/.gradle/gradle.properties, absolutely not in the repository.
  apiKey = project.hasProperty('curseForgeApiKey') ? project.curseForgeApiKey : ""
  project {
    id = '253045'
    changelogType = 'markdown'
    changelog = file('changelog.md')
    releaseType = 'release'
    addGameVersion "1.7.10"
    mainArtifact(jar) {
      displayName = "Electrical Age $project.version"
    }
    // addArtifact(sourcesJar) {
    //     displayName = "Electrical Age $project.version Sources"
    // }
  }
}

idea { module { inheritOutputDirs = true } }
