buildscript {
    repositories {
        maven { url = 'https://files.minecraftforge.net/maven' }
        jcenter()
        mavenCentral()
    }
    dependencies {
        classpath group: 'net.minecraftforge.gradle', name: 'ForgeGradle', version: '3.+', changing: true
    }
}

plugins {
    id "com.matthewprenger.cursegradle" version "1.3.0"
    id "maven-publish"
}

apply plugin: 'net.minecraftforge.gradle'

def build_number = System.getenv('BUILD_NUMBER') != null ? System.getenv('BUILD_NUMBER') : 'CUSTOM'
def username = project.hasProperty('dev_username') ? "${dev_username}" : "${mod_name}"

group = package_group
archivesBaseName = mod_name
version = "${mc_version}-${mod_version}-${build_number}"

repositories {
    maven { url "http://dvs1.progwml6.com/files/maven" }
    maven { url "http://tehnut.info/maven" }
}

dependencies {
    minecraft "net.minecraftforge:forge:${mc_version}-${forge_version}"
    compile fg.deobf("mezz.jei:jei-${mc_version}:${jei_version}")
    compile fg.deobf("mcp.mobius.waila:Hwyla:${waila_version}")
}

minecraft {
    mappings channel: "snapshot", version: "20190912-1.14.3"

    runs {
        client {
            workingDirectory project.file('run')
            property 'forge.logging.markers', ''
            property 'forge.logging.console.level', 'info'
            property 'username', username

            mods {
                bloodmagic {
                    source sourceSets.main
                }
            }
        }

        server {
            workingDirectory project.file('run/server')
            property 'forge.logging.markers', ''
            property 'forge.logging.console.level', 'info'

            mods {
                bloodmagic {
                    source sourceSets.main
                }
            }
        }
    }
}

apply from: "gradle/process_mod_info.gradle"

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

    from(sourceSets.main.resources.srcDirs) {
        include '**/*.toml'

        expand 'version': project.version
    }
    from(sourceSets.main.resources.srcDirs) {
        exclude '**/*.toml'
    }
}

allprojects {
    tasks.withType(Javadoc) {
        options.addStringOption('Xdoclint:none', '-quiet')
    }
}

jar {
    classifier = ''
    from sourceSets.main.output
    manifest.mainAttributes(
            "Built-By": System.getProperty('user.name'),
            "Created-By": "${System.getProperty('java.vm.version')} + (${System.getProperty('java.vm.vendor')})",
            "Implementation-Title": project.name,
            "Implementation-Version": project.version,
            "Built-On": "${mc_version}-${forge_version}"
    )
}

// API jar
task apiJar(type: Jar) {
    classifier = 'api'
    include 'com/WayofTime/bloodmagic/api/**/*'
    exclude 'com/WayofTime/bloodmagic/api/impl/**/*'
    from sourceSets.main.allSource
    from sourceSets.main.output
}

task sourcesJar(type: Jar) {
    classifier = 'sources'
    from sourceSets.main.allJava
}

tasks.build.dependsOn apiJar, sourcesJar

tasks.withType(JavaCompile) { task ->
    task.options.encoding = 'UTF-8'
}

publishing {
    tasks.publish.dependsOn 'build'
    publications {
        mavenJava(MavenPublication) {
            artifact jar
            artifact sourcesJar
            artifact apiJar
        }
    }
    repositories {
        if (project.hasProperty('maven_repo')) {
            maven { url maven_repo }
        } else {
            mavenLocal()
        }
    }
}

String getChangelogText() {
    def changelogFile = new File('changelog.txt')
    String str = ''
    String separator = '---'
    int lineCount = 0
    boolean done = false
    changelogFile.eachLine {
        if (done || it == null) {
            return
        }
        if (lineCount < 3) {
            lineCount++
            if (it.startsWith(separator)) {
                return
            }
        }
        if (!it.startsWith(separator)) {
            str += "$it" + (lineCount < 3 ? ':\n\n' : '\n')
            return
        }
        done = true // once we go past the first version block, parse no more
    }
    return str
}

def curseRelations = {
    optionalLibrary 'jei'
    optionalLibrary 'hwyla'
}

curseforge {
    if (project.hasProperty('curse_key_TehNut'))
        apiKey = project.curse_key_TehNut

    project {
        id = "${curse_id}"
        changelog = getChangelogText()
        releaseType = 'beta'

        relations curseRelations

        addArtifact sourcesJar
        addArtifact apiJar
    }
}