
plugins {
    id 'org.beryx.jlink' version '3.1.1'
    id "org.asciidoctor.jvm.convert" version "4.0.3"
    id 'org.jreleaser' version '1.15.0'
    id("com.netflix.nebula.ospackage") version "11.10.0"
    id 'org.gradle.crypto.checksum' version '1.4.0'
    id 'signing'
}

repositories {
    mavenCentral()
}

task dist(type: DefaultTask) {}


run {
    enabled = false
}

distTar {
    enabled = false;
}

distZip {
    enabled = false;
}

import org.gradle.crypto.checksum.Checksum

import java.util.stream.Collectors

def distDir = layout.buildDirectory.get().dir('dist')
task createChecksums(type: Checksum) {
    inputFiles.setFrom(distDir.dir('artifacts').getAsFileTree().files)
    outputDirectory.set(layout.buildDirectory.dir("dist/checksums/artifacts"))
    checksumAlgorithm.set(Checksum.Algorithm.SHA256)

    doLast {
        def artifactChecksumsSha256Hex = new HashMap<String, String>()
        for (final def file in outputDirectory.get().getAsFileTree().files) {
            def name = file.name.lastIndexOf('.').with {it != -1 ? file.name[0..<it] : file.name}
            if (name.endsWith('mapping.map') || name.endsWith('.asc')) {
                continue
            }

            artifactChecksumsSha256Hex.put(name, file.text.trim())
        }

        file(layout.buildDirectory.dir("dist/checksums/sha256sums.txt")).text = artifactChecksumsSha256Hex.entrySet().stream()
                .map(e -> e.getValue() + ' ' + e.getKey())
                .collect(Collectors.joining('\n'))
    }
}

def getArtifactChecksumSha256Hex(String name) {
    var file = layout.buildDirectory.file("dist/checksums/artifacts/${name}.sha256")
    return file.get().getAsFile().exists() ? file.get().getAsFile().text : "";
}

def getArtifactChecksumSha256Base64(String name) {
    return Base64.getEncoder().encodeToString(HexFormat.of().parseHex(getArtifactChecksumSha256Hex(name)))
}

clean {
    doFirst {
        // Fix clean failing when file is read-only
        if (file("$distDir").exists()) {
            file("$distDir").traverse { f -> if (f.exists() && f.isFile()) f.writable = true }
        }
    }
}

apply from: 'base.gradle'
apply from: 'jpackage.gradle'

if (rootProject.fullVersion) {
    apply from: 'train.gradle'
    apply from: 'base_full.gradle'
    apply from: 'cli.gradle'
    apply from: 'portable.gradle'
    apply from: 'proguard.gradle'

    if (org.gradle.internal.os.OperatingSystem.current().isLinux()) {
        apply from: 'linux_packages.gradle'
    } else if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
        apply from: 'msi.gradle'
    } else if (org.gradle.internal.os.OperatingSystem.current().isMacOsX()) {
        apply from: 'pkg.gradle'
    }

    apply from: 'jreleaser.gradle'
    apply from: 'aur.gradle'
    apply from: 'nix.gradle'
    apply from: 'choco.gradle'
    apply from: 'winget.gradle'
    apply from: 'install.gradle'
    apply from: 'i18n.gradle'

    signing {
        useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
    }

    task signArtifacts(type: Sign) {
        def dir = layout.buildDirectory.dir("dist/artifacts").get()
        dir.asFileTree.files.forEach {sign(it)}
    }

    task signChecksums(type: Sign) {
        def checksums = layout.buildDirectory.file("dist/checksums/sha256sums.txt").get().asFile
        sign(checksums)
    }
}

distTar {
    enabled = false;
}

distZip {
    enabled = false;
}

assembleDist {
    enabled = false;
    dependsOn.clear()
}