import java.text.SimpleDateFormat

plugins {
    id("java-library")
    id("maven-publish")
}

group = "com.esaulpaugh"
version = "13.3.2-SNAPSHOT"

final String versionStr = getProject().getGradle().getGradleVersion()
final int gradleMajorVersion = Integer.parseUnsignedInt(versionStr.substring(0, versionStr.indexOf((int)".".charAt(0))))

if (gradleMajorVersion >= 5) {
    java {
        sourceCompatibility = JavaVersion.VERSION_1_8
        targetCompatibility = JavaVersion.VERSION_1_8
    }
} else {
    sourceCompatibility = JavaVersion.VERSION_1_8
    targetCompatibility = JavaVersion.VERSION_1_8
}

compileJava {
    if (JavaVersion.current() >= JavaVersion.VERSION_1_10) {
        options.compilerArgs.addAll(["--release", "8"])
    }
}

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

test {
    useJUnitPlatform()
    maxParallelForks = Runtime.runtime.availableProcessors()
}

task javadocJar(type: Jar, dependsOn: javadoc) {
    if (gradleMajorVersion >= 6) getArchiveClassifier().set("javadoc")
    else classifier = "javadoc"
    from javadoc.destinationDir
}

task sourcesJar(type: Jar, dependsOn: classes) {
    if (gradleMajorVersion >= 6) getArchiveClassifier().set("sources")
    else classifier = "sources"
    from sourceSets.main.allSource
}

static String todayUTC() {
    SimpleDateFormat sdf = new SimpleDateFormat("MMMM d yyyy")
    sdf.setTimeZone(TimeZone.getTimeZone("UTC"))
    return sdf.format(new Date())
}

jar {
    manifest {
        attributes(
                "Implementation-Title": project.name,
                "Implementation-Version": project.version,
                "Automatic-Module-Name": "com.esaulpaugh.headlong",
                "Created-By": "Gradle",
                "Build-Date": todayUTC()
        )
    }
    finalizedBy(sourcesJar)
}

artifacts {
    archives javadocJar, sourcesJar
}

publishing {
    publications {
        headlong(MavenPublication) {
            from components.java
            artifact sourcesJar
            artifact javadocJar
        }
    }
}

sourceSets {
    jmh {
        java.srcDirs = ["src/jmh/java"]
        compileClasspath += sourceSets.main.runtimeClasspath
    }
}

repositories {
    mavenCentral()
}

final String junitVersion = "5.11.4"
final String jmhVersion = "1.37"
final String bcVersion = "1.82"

dependencies {
    implementation("com.google.code.gson:gson:[2.1, 2.13.2]") {
        exclude(group: "com.google.errorprone", module: "error_prone_annotations")
    }

    testImplementation("org.junit.jupiter:junit-jupiter-api:" + junitVersion)
    testRuntimeOnly("org.junit.jupiter:junit-jupiter-engine:" + junitVersion)
    testRuntimeOnly("org.junit.platform:junit-platform-launcher:1.11.4")
    testImplementation("org.bouncycastle:bcprov-jdk14:" + bcVersion)

    jmhImplementation("org.openjdk.jmh:jmh-core:" + jmhVersion)
    jmhAnnotationProcessor("org.openjdk.jmh:jmh-generator-annprocess:" + jmhVersion)
}

task jmh(type: JavaExec, dependsOn: jmhClasses) { // run benchmarks with `gradle jmh`
    if (gradleMajorVersion >= 7) getMainClass().set("org.openjdk.jmh.Main")
    else main = "org.openjdk.jmh.Main"
    classpath = sourceSets.jmh.compileClasspath + sourceSets.jmh.runtimeClasspath
}

classes.finalizedBy(jmhClasses)

// tasks.withType(AbstractArchiveTask).configureEach {
tasks.withType(AbstractArchiveTask) {
    preserveFileTimestamps = false
    reproducibleFileOrder = true
}