plugins {
    id 'java-library'
    id 'fabric-loom' version '0.2.6-SNAPSHOT'
    id 'maven-publish'
}

targetCompatibility = JavaVersion.VERSION_1_8
sourceCompatibility = JavaVersion.VERSION_1_8

def createVersion() {
    def parts = []

    if (project.release != 'RELEASE') {
        parts.push project.release
    }

    if (System.env.TRAVIS_BRANCH != null && System.env.TRAVIS_BRANCH != 'master') {
        parts.push System.env.TRAVIS_BRANCH
    }

    if (System.env.TRAVIS_BRANCH != project.minecraft_version) {
       parts.push project.minecraft_version
    }

    def ver = project.version
    if (parts.size > 0) {
      return ver + '-' + parts.join('-')
    }
    return ver
}

version = createVersion()
group = project.group
description = project.displayname
archivesBaseName = project.name

minecraft {
    refmapName = 'minelp.mixin.refmap.json'
}

repositories {
    maven {
        name = 'minelp'
        url = 'https://repo.minelittlepony-mod.com/maven/snapshot'
    }
    maven {
        name = 'minelp-release'
        url = 'https://repo.minelittlepony-mod.com/maven/release'
    }
}

dependencies {
    minecraft "com.mojang:minecraft:${project.minecraft_version}"
    mappings "net.fabricmc:yarn:${project.yarn_mappings}"
    modApi "net.fabricmc:fabric-loader:${project.loader_version}"

    compileOnly "com.google.code.findbugs:jsr305:3.0.2"

    modApi "net.fabricmc.fabric-api:fabric-api-base:0.1.2+"
    modApi "net.fabricmc.fabric-api:fabric-events-lifecycle-v0:0.1.2+b7f9825d0a"
    modApi "net.fabricmc.fabric-api:fabric-resource-loader-v0:0.1.8+"

    modApi "com.minelittlepony:Kirin:${project.kirin_version}"
    include "com.minelittlepony:Kirin:${project.kirin_version}"

    modApi "com.minelittlepony:Mson:${project.mson_version}"
    include "com.minelittlepony:Mson:${project.mson_version}"

    modImplementation "com.minelittlepony:HDSkins:${project.hd_skins_version}"
    modCompileOnly "io.github.prospector:modmenu:${project.modmenu_version}"
}

//
// Imports the Background Ponies from the MLP Community Skin Pack
//
task copyBGPones(type: Copy) {

    def illegals = /[^a-z0-9_\/.-]/

    from "skins/Background Ponies"
    into temporaryDir

    eachFile {
        if (it.name =~ illegals) {
            logger.warn("Sanitizing file with illegal characters: ${it.path}")
            it.name = it.name.replaceAll(/\s/, '_').replaceAll(illegals, '')
        }
    }
}

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

    from(sourceSets.main.resources.srcDirs) {
        include "fabric.mod.json"
        expand "version": project.version
    }

    from(sourceSets.main.resources.srcDirs) {
        exclude "fabric.mod.json"
    }

    from 'LICENSE'

    exclude "/assets/minelittlepony/textures/entity/pony"
    from(copyBGPones) {
        into "/assets/minelittlepony/textures/entity/pony"
    }
}

tasks.withType(JavaCompile) {
    options.encoding = "UTF-8"
}

task sourcesJar(type: Jar, dependsOn: classes) {
    classifier = "sources"
    from sourceSets.main.allSource
}

publishing {
    publications {
        maven(MavenPublication) {
            afterEvaluate {
                artifact(remapJar)
            }
            artifact(sourcesJar) {
                builtBy remapSourcesJar
            }

            pom {
                name = "MineLittlePony"
                description = "HD Skins support for Minecraft"
                url = "https://minelittlepony-mod.com"
                licenses {
                    license {
                        name = "MIT Public License"
                        url = "https://tlo.mit.edu/learn-about-intellectual-property/software-and-open-source-licensing"
                    }
                }
                developers {
                    developer {
                        id = "killjoy1221"
                        name = "Matthew Messinger"
                        email = "mattmess1221@gmail.com"
                    }
                    developer {
                        id = "sollace"
                    }
                }
                scm {
                    connection = 'scm:git:git://github.com/MineLittlePony/MineLittlePony.git'
                    developerConnection = 'scm:git:ssh://github.com/MineLittlePony/MineLittlePony.git'
                    url = 'https://github.com/MineLittlePony/MineLittlePony'
                }
            }
        }
    }

    repositories {
        maven {
            name = "MineLittlePony"
            def mvn = 's3://repo.minelittlepony-mod.com/maven'
            url = release == 'SNAPSHOT' ? "$mvn/snapshot" : "$mvn/release"

            credentials(AwsCredentials) {
                accessKey = System.env.ACCESS_KEY
                secretKey = System.env.SECRET_KEY
            }
        }
    }
}