
plugins {
    id 'org.beryx.jlink' version '3.2.1'
    id("com.netflix.nebula.ospackage") version "12.2.0"
    id 'org.gradle.crypto.checksum' version '1.4.0'
    id 'signing'
}

repositories {
    mavenCentral()
}

tasks.register('dist', DefaultTask) {}


run {
    enabled = false
}

distTar {
    enabled = false
}

distZip {
    enabled = false
}

import org.gradle.crypto.checksum.Checksum

import java.util.stream.Collectors

tasks.register('createGithubChangelog', DefaultTask) {
    doLast {
        def template = file("$projectDir/changelog_format/github.md").text
        def argument = incrementalChangelogFile.exists() ? incrementalChangelogFile.text : changelogFile.text
        def text = template.formatted(argument)
        def dir = layout.buildDirectory.dir("dist/release").get()
        mkdir(dir)
        def target = dir.file("github-changelog.md").asFile
        target.text = text
    }
}

tasks.register('createDiscordChangelog', DefaultTask) {
    doLast {
        def template = file("$projectDir/changelog_format/discord.md").text
        def argument = incrementalChangelogFile.exists() ? incrementalChangelogFile.text : changelogFile.text
        def text = template.formatted(productName, versionString, argument, productName, kebapProductName)
        def dir = layout.buildDirectory.dir("dist/release").get()
        mkdir(dir)
        def target = dir.file("discord-changelog.md").asFile
        target.text = text
    }
}

def distDir = layout.buildDirectory.get().dir('dist')
tasks.register('createChecksums', 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.strip())
        }

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

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

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 (fullVersion) {
    apply from: 'train.gradle'
    apply from: 'base_full.gradle'
    apply from: 'cli.gradle'
    apply from: 'portable.gradle'
    apply from: 'proguard.gradle'
    apply from: 'github.gradle'
    apply from: 'install.gradle'
    apply from: 'i18n.gradle'

    if (os.isLinux()) {
        apply from: 'linux_packages.gradle'
        apply from: 'appimage/app_image.gradle'
        apply from: 'nix/nix.gradle'
        apply from: 'aur/aur.gradle'
        apply from: 'homebrew/homebrew.gradle'
        apply from: 'choco/choco.gradle'
    } else if (os.isWindows()) {
        apply from: 'msi/msi.gradle'
        apply from: 'winget/winget.gradle'
    } else if (os.isMacOsX()) {
        apply from: 'pkg/pkg.gradle'
    }

    signing {
        useInMemoryPgpKeys(signingKeyId, signingKey, signingPassword)
    }

    tasks.register('signArtifacts', Sign) {
        def dir = layout.buildDirectory.dir("dist/artifacts").get()
        dir.asFileTree.files.forEach { sign(it) }
    }

    tasks.register('signChecksums', 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()
}

