/*
 * This file was generated by the Gradle 'init' task.
 */

plugins {
    // Apply the java Plugin to add support for Java.
    id 'java'
    id 'maven-publish'
    id 'pmd'
    id 'com.palantir.git-version'
}

import java.time.LocalDate

def currentYear = LocalDate.now().getYear()

project.version = gitVersion(prefix:'release@')

repositories {
    
    maven {
        name = "GiteaMaven"
        url = uri("https://gitea.egothor.org/api/packages/Egothor/maven")
    }
    
    // Use Maven Central for resolving dependencies.
    mavenCentral()
}

dependencies {
    constraints {
        // Define dependency versions as constraints
        implementation 'org.apache.commons:commons-text:1.15.0'
        implementation 'commons-cli:commons-cli:1.11.0'
        implementation 'org.bouncycastle:bcpkix-jdk18on:1.83'
        implementation 'org.egothor:conflux:[1.0,2.0)'
        implementation 'org.apache.commons:commons-imaging:1.0.0-alpha6'
    }

    // Use JUnit Jupiter for testing.
    testImplementation 'org.junit.jupiter:junit-jupiter:5.10.2'

    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

pmd {
	consoleOutput = true
	toolVersion = '7.19.0'
	sourceSets = [sourceSets.main]
	ruleSetFiles = files(rootProject.file(".ruleset"))
}

tasks.withType(Pmd) {
    maxHeapSize = "16g"
}

// Apply a specific Java toolchain to ease working on different environments.
java {
    toolchain {
        languageVersion = JavaLanguageVersion.of(21)
    }
}

javadoc {
	failOnError = false
	
    options.addStringOption('Xdoclint:all,-missing', '-quiet')
    options.addBooleanOption('html5', true)
    options.tags('apiNote:a:API Note:')
    options.tags('implSpec:a:Implementation Requirements:')
    options.tags('implNote:a:Implementation Note:')
    options.tags('param')
    options.tags('return')
    options.tags('throws')
    options.tags('since')
    options.tags('version')
    options.tags('serialData')
    options.tags('factory')
    options.tags('see')
    
    options.use = true
    options.author = true
    options.version = true
    options.windowTitle = 'ZeroEcho'
    options.docTitle = 'ZeroEcho API'
    
    options.bottom = '<div style="text-align: right; padding: 5px;">Copyright &copy; ' + currentYear +
                     ' Egothor - Version ' + version +
                     ' - <a href="https://gitea.egothor.org/Egothor/ZeroEcho/raw/branch/main/LICENSE">License</a>' +
                     '</div>'
                     	
    source = sourceSets.main.allJava
}

task uploadJavadoc(type: Exec) {
    dependsOn javadoc

    doFirst {
        def javadocDir = tasks.javadoc.destinationDir
        def relativeJavadocDir = project.projectDir.toPath().relativize(javadocDir.toPath()).toString()
        def moduleName = project.name  // Dynamically get the module name

        println "Uploading Javadoc for module: ${moduleName}"
        println "Uploading from relative path: $relativeJavadocDir"

        // Upload to a folder named after the module
        commandLine "rsync", "-avz", "--delete",
          "-e", "ssh -i ${javadocKeyPath} -o IdentitiesOnly=yes",
          relativeJavadocDir + '/', "${javadocUser}@${javadocHost}:${javadocPath}/${project.name}"
    }
}

tasks.named('test') {
    // Use JUnit Platform for unit tests.
    useJUnitPlatform()
}

if (project.hasProperty('giteaToken') && project.giteaToken) {
    publishing {
        publications {
            mavenJava(MavenPublication) {
                from components.java
                artifactId = "${rootProject.name}-${project.name}"
            }
        }
        repositories {
            maven {
                name = "GiteaMaven"
                url = uri("https://gitea.egothor.org/api/packages/Egothor/maven")

                credentials(HttpHeaderCredentials) {
                    name = "Authorization"
                    value = "token ${giteaToken}"
                }

                authentication {
                    header(HttpHeaderAuthentication)
                }
            }
        }
    }
} else {
    println "No giteaToken defined - skipping publishing configuration"
}

gradle.taskGraph.whenReady { taskGraph ->
    def banner = """
\u001B[34m

8888888888 .d8888b.   .d88888b. 88888888888 888    888  .d88888b.  8888888b.  
888       d88P  Y88b d88P" "Y88b    888     888    888 d88P" "Y88b 888   Y88b 
888       888    888 888     888    888     888    888 888     888 888    888 
8888888   888        888     888    888     8888888888 888     888 888   d88P 
888       888  88888 888     888    888     888    888 888     888 8888888P"  
888       888    888 888     888    888     888    888 888     888 888 T88b   
888       Y88b  d88P Y88b. .d88P    888     888    888 Y88b. .d88P 888  T88b  
8888888888 "Y8888P88  "Y88888P"     888     888    888  "Y88888P"  888   T88b 

\u001B[36m
 Project : ${project.name}
 Version : ${project.version}
\u001B[0m
"""
    println banner
}
