/*
 * Copyright (c) 2019-2020 5zig Reborn
 *
 * This file is part of The 5zig Mod
 * The 5zig Mod is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * The 5zig Mod is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with The 5zig Mod.  If not, see <http://www.gnu.org/licenses/>.
 */
import org.apache.tools.ant.filters.EscapeUnicode

apply plugin: 'java'

repositories {
    maven {
        name = "forge"
        url = "https://maven.minecraftforge.net"
    }
    maven {
        name = "roccodev-snapshots"
        url = "https://repo.rocco.dev/snapshots" // For Mixin
    }
}

sourceSets.main {
    java {
        srcDirs '../../mod/src/main/java'
        srcDirs '../common/src/main/java'
        srcDirs '../../api/src/main/java'
    }
    resources {
        srcDirs '../../mod/src/main/resources'
        srcDirs '../../i18n/resources'
    }
}

configurations {
    embed
    compile.extendsFrom embed
}

compileJava {
    sourceCompatibility = '1.8'
    targetCompatibility = '1.8'
    options.encoding = 'UTF-8'
}

dependencies {
    compile fileTree(dir: '../../mod/libs', include: ['*.jar'])

    compile group: 'org.lwjgl.lwjgl', name: 'lwjgl', version: '2.9.3'
    compile group: 'org.lwjgl.lwjgl', name: 'lwjgl_util', version: '2.9.3'
    compile group: 'com.sun.jna', name: 'jna', version: '3.0.9'
    compile group: 'com.google.guava', name: 'guava', version: '18.0'
    compile group: 'com.google.code.gson', name: 'gson', version: '2.8.5'
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.1'
    compile group: 'com.hynnet', name: 'jacob', version: '1.18'
    compile group: 'com.h2database', name: 'h2', version: '1.4.199'
    compile group: 'io.netty', name: 'netty-all', version: '4.1.36.Final'
    compile group: 'commons-io', name: 'commons-io', version: '2.6'
    compile group: 'org.apache.commons', name: 'commons-lang3', version: '3.9'
    compile group: 'commons-codec', name: 'commons-codec', version: '1.12'
    compile group: 'org.apache.commons', name: 'commons-compress', version: '1.18'

    embed group: 'org.hsqldb', name: 'hsqldb', version: '2.4.0'
    embed 'com.github.5zig-reborn:DiscordIPC:ac1dbc6c73'
    embed group: 'com.hynnet', name: 'jacob', version: '1.18'
}

tasks.withType(ProcessResources).each { task ->
    task.from(task.getSource()) {
        include '**/*.properties'
        filter(EscapeUnicode) // native2ascii for i18n files
    }
}

task buildInfo {
    def cmd = "git rev-list --count master"
    def proc = cmd.execute()
    project.ext.set("buildNumber", proc.text.trim())
}

jar.dependsOn(buildInfo)

jar {
    manifest.attributes (
            'Main-Class': 'eu.the5zig.mod.installer.Frame',
            'MixinConfigs': 'mixins.json',
            'TweakClass': 'eu.the5zig.mod.asm.ClassTweaker',
            'MixinConnector': 'eu.the5zig.mod.MixinConnector5zig',
            'TweakOrder': '-900',
            '5zig-Version': project.ext.get("buildNumber"),
            'Minecraft-Version': project.ext.get("customVer") != null ? project.ext.get("customVer") : project.name
    )

    version = "b" + project.ext.get("buildNumber")

    if (System.getenv("TRAVIS_COMMIT") != null && (System.getenv("TRAVIS_TAG") == null || System.getenv("TRAVIS_TAG").isEmpty())) {
        manifest.attributes.put("5zig-Build", System.getenv("TRAVIS_COMMIT"))
    }

    from {
        duplicatesStrategy DuplicatesStrategy.EXCLUDE
        configurations.embed.collect {
            it.isDirectory() ? it : zipTree(it)
        }
    }

    eachFile { file ->
        if(file.name.endsWith(".lang")) {
            file.name = file.name.toLowerCase(Locale.ROOT)
            File f = file.getFile()
            File newJson = new File(f.getParentFile(), file.name.replace(".lang", ".json"))
            StringBuilder content = new StringBuilder()
            content.append("{\n")
            f.eachLine { line ->
                String[] pair = line.split("=");
                String key = pair[0].trim()
                String value = pair[1].trim()
                content.append('"').append(key).append('"').append(':').append('"').append(value).append('"').append(",\n")
            }
            content.setLength(content.length() - 2)
            content.append("\n}")
            newJson.write(content.toString())
            include newJson.getName()
        }
    }

    exclude 'LICENSE.txt'
    exclude 'META-INF/*.RSA'
    exclude 'META-INF/*.SF'
}
