plugins {
    id 'com.github.johnrengelman.shadow' version '8.1.1'
    id 'edu.sc.seis.launch4j' version '4.0.0'
    id 'maven-publish'
    id 'application'
    id 'java'
}

group = 'net.foulest'
version = '1.3.4'
description = 'RepairKit'

// Set the language level to Java 17
java {
    toolchain {
        languageVersion.set(JavaLanguageVersion.of(17))
    }
}

repositories {
    // Maven central repository
    mavenCentral()
    mavenLocal()

    // Local libraries
    flatDir {
        dirs 'libs'
    }
}

dependencies {
    // JNA platform - for registry access
    // https://mvnrepository.com/artifact/net.java.dev.jna/jna-platform
    implementation group: 'net.java.dev.jna', name: 'jna-platform', version: '5.18.0'

    // Google Gson - for JSON serialization and deserialization
    // https://mvnrepository.com/artifact/com.google.code.gson/gson
    implementation group: 'com.google.code.gson', name: 'gson', version: '2.13.2'

    // two-slices - for sending toasts
    // https://github.com/sshtools/two-slices
    implementation group: 'com.sshtools', name: 'two-slices', version: '0.9.4'

    // JetBrains Annotations - for code inspection and documentation
    // https://mvnrepository.com/artifact/org.jetbrains/annotations
    compileOnly group: 'org.jetbrains', name: 'annotations', version: '26.0.2-1'

    // Lombok - for reducing boilerplate code
    // https://projectlombok.org
    compileOnly group: 'org.projectlombok', name: 'lombok', version: '1.18.42'
    annotationProcessor group: 'org.projectlombok', name: 'lombok', version: '1.18.42'
}

launch4j {
    // Executable file information
    outfile = "${project.name}-${project.version}.exe"
    mainClassName = "${project.group}.repairkit.RepairKit"
    icon = "${projectDir}/src/main/resources/icons/RepairKit.ico"

    // Java runtime information
    // Uses Java 8 as the minimum version; bundled JRE (17) is in the 'jre' directory
    jreMinVersion = '1.8.0'
    bundledJrePath = 'jre'
    initialHeapSize = 128
    maxHeapSize = 1024

    // Application version information
    version = "${project.version}"
    textVersion = "${project.version}"
    fileDescription = 'RepairKit by Foulest'
    copyright = '2024'
    productName = "${project.name}"
    internalName = "${project.name}"

    // Custom manifest file for Windows
    manifest = "${projectDir}/RepairKit.manifest"
}

tasks {
    shadowJar {
        mainClassName = 'net.foulest.repairkit.RepairKit'
        archiveFileName.set("${project.name}-${project.version}.jar")
    }

    jar {
        duplicatesStrategy = DuplicatesStrategy.INCLUDE
        archiveFileName.set("${project.name}-${project.version}.jar")

        // Set the 'Main-Class' attribute in the JAR manifest
        manifest {
            attributes 'Main-Class': 'net.foulest.repairkit.RepairKit'
        }
    }

    // Zip the launch4j directory for distribution
    tasks.register('zipLaunch4jDir', Zip) {
        dependsOn(createExe)
        from "${projectDir}/build/launch4j"
        archiveFileName = "${project.name}-${project.version}.zip"
        destinationDirectory = file("${projectDir}/build")
    }

    // Create a properties file with the version number
    tasks.register('createProperties') {
        dependsOn processResources

        doLast {
            new File("${projectDir}/build/resources/main/version.properties").withWriter { final writer ->
                final Properties properties = new Properties()
                properties['version'] = project.version.toString()
                properties.store writer, null
            }
        }
    }

    createExe {
        dependsOn(shadowJar)

        doLast {
            // Copy the JRE to the launch4j directory
            copy {
                from "${projectDir}/jre"
                into "${projectDir}/build/launch4j/jre"
            }

            // Copy the config folder to the launch4j directory
            copy {
                from "${projectDir}/src/main/resources/config"
                into "${projectDir}/build/launch4j/config"
            }

            // Delete the lib folder in the launch4j directory
            delete "${projectDir}/build/launch4j/lib"
        }

        finalizedBy(zipLaunch4jDir)
    }

    compileJava {
        dependsOn(clean)
        options.encoding = 'UTF-8'
    }

    classes {
        dependsOn createProperties
    }

    build {
        dependsOn(shadowJar)
    }

    distZip {
        dependsOn(shadowJar)
    }

    distTar {
        dependsOn(shadowJar)
    }

    startScripts {
        dependsOn(shadowJar)
    }

    startShadowScripts {
        dependsOn(jar)
    }

    tasks.register('sourceJar', Jar) {
        from sourceSets.main.allJava
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            groupId = 'net.foulest.repairkit'
            artifactId = project.name
            version = project.version

            from components.java
        }
    }
}
