import java.nio.file.Files
import org.gradle.util.GradleVersion

apply plugin: 'groovy'

/*
 * Build checks
 */

String minimumGradleVersion = file('src/main/resources/minimumGradleVersion').text.trim()
if (GradleVersion.current() < GradleVersion.version(minimumGradleVersion)) {
    throw new GradleException("Gradle ${minimumGradleVersion}+ is required to build es-hadoop")
}

if (JavaVersion.current() < JavaVersion.VERSION_11) {
    throw new GradleException('At least Java 11 is required to use elasticsearch gradle tools')
}

/*
 * Configure Project Versions
 */

boolean localRepo = project.getProperties().containsKey("localRepo")

Properties props = new Properties()
props.load(project.file('esh-version.properties').newDataInputStream())
String eshVersion = props.getProperty('eshadoop')
String esVersion = props.getProperty('elasticsearch')
String buildToolsVersion = props.getProperty('build-tools')

// determine if we're building a prerelease or candidate (alphaX/betaX/rcX)
String qualifier = System.getProperty("build.version_qualifier", "")
if (qualifier.isEmpty() == false) {
    if (qualifier.matches("(alpha|beta|rc)\\d+") == false) {
        throw new IllegalStateException("Invalid qualifier: " + qualifier)
    }
    eshVersion += "-" + qualifier
    esVersion += "-" + qualifier
    buildToolsVersion += "-" + qualifier
}

// determine if we're building a snapshot or not (by default we will be)
boolean snapshot = "true".equals(System.getProperty("build.snapshot", "true"))
if (snapshot) {
    // we update the version property to reflect if we are building a snapshot or a release build
    eshVersion += "-SNAPSHOT"
    esVersion += "-SNAPSHOT"
    buildToolsVersion += "-SNAPSHOT"
}

props.put("eshadoop", eshVersion)
props.put("elasticsearch", esVersion)
props.put("build-tools", buildToolsVersion)

repositories {
    gradlePluginPortal()
    mavenCentral()
    // For Elasticsearch snapshots.
    if (localRepo) {
        // For some reason the root dirs all point to the buildSrc folder. The local Repo will be one above that.
        flatDir { dirs new File(project.rootDir, "../localRepo") }
    } else {
        maven { url = "https://oss.sonatype.org/content/repositories/snapshots" }
        maven { url = "https://artifacts-snapshot.elastic.co/elasticsearch/${esVersion}/maven" }
    }
}

dependencies {
    compileOnly gradleApi()
    compileOnly localGroovy()

    // Required for dependency licenses task
    implementation 'org.apache.rat:apache-rat:0.11'
    implementation 'commons-codec:commons-codec:1.12'
    implementation 'com.gradleup.nmcp:nmcp:0.1.5'

    if (localRepo) {
        implementation name: "build-tools-${buildToolsVersion}"
    } else {
        implementation "org.elasticsearch.gradle:build-tools:$buildToolsVersion"
    }
}

// write the updated properties to a temp property file
File tempPropertiesFile = new File(project.buildDir, "esh-version.properties")
task writeVersionProperties {
    inputs.properties(props)
    outputs.file(tempPropertiesFile)
    doLast {
        OutputStream stream = Files.newOutputStream(tempPropertiesFile.toPath())
        try {
            props.store(stream, "UTF-8")
        } finally {
            stream.close()
        }
    }
}

// copy the saved property file to the resources dir
processResources {
    dependsOn writeVersionProperties
    from tempPropertiesFile
}
