plugins {
    id 'java'
    id 'signing'
    id 'maven-publish'
}

java {
    withSourcesJar()
    withJavadocJar()
}

group = 'com.kuzudb'
def projectRoot = projectDir.parentFile.parentFile
def cmakeFile = new File(projectRoot, 'CMakeLists.txt').text
def versionLine = cmakeFile.readLines().find { it.startsWith('project(Kuzu VERSION ') }
def cmakeVersion = versionLine.split(' ')[2]
def versionParts = cmakeVersion.split('\\.')
if (versionParts.length > 3) {
    version = versionParts[0] + '.' + versionParts[1] + '.' + versionParts[2] + '-SNAPSHOT'
} else {
    version = cmakeVersion
}

println "Project version: $version"

repositories {
    mavenCentral()
}

dependencies {
    testImplementation platform('org.junit:junit-bom:5.10.0')
    testImplementation 'org.junit.jupiter:junit-jupiter'
}

if (System.getenv('SKIP_CMAKE_BUILD') != 'true') {
    processResources {
        dependsOn 'copyNativeLibs'
    }
}

publishing {
    publications {
        mavenJava(MavenPublication) {
            pom {
                name = 'kuzu'
                description = 'Kuzu is working on something new! We have archived the KuzuDB project.'
                inceptionYear = '2020'
                properties = [
                        'project.build.sourceEncoding'    : 'UTF-8',
                        'project.reporting.outputEncoding': 'UTF-8',
                        'java.version'                    : '11',
                        'maven.compiler.source'           : '11',
                        'maven.compiler.target'           : '11'
                ]
                licenses {
                    license {
                        name = 'MIT License'
                        url = 'https://github.com/kuzudb/kuzu/blob/master/LICENSE'
                    }
                }
                developers {
                    developer {
                        id = 'kuzudb'
                        name = 'Kùzu Inc.'
                        email = 'contact@kuzudb.com'
                    }
                }
                scm {
                    connection = 'scm:git:git://github.com/kuzudb/kuzu.git'
                    developerConnection = 'scm:git:ssh://github.com/kuzudb/kuzu.git'
                    url = 'https://github.com/kuzudb/kuzu'
                }
            }
            from components.java
        }
    }

    repositories {
        // The local repository is used for manual publishing to Maven Central
        // Portal. It is located in `build/repo`.
        // To publish, run `././gradlew publishMavenJavaToLocalRepo`
        // This is a workaround to force Gradle handle signing and checksum
        maven {
            name = 'LocalRepo'
            url = uri("$buildDir/repo")
        }
        maven {
            name = 'GitHubPackages'
            url = uri('https://maven.pkg.github.com/kuzudb/kuzu')
            credentials {
                username = System.getenv('GITHUB_ACTOR')
                password = System.getenv('GITHUB_TOKEN')
            }
        }
    }
}

signing {
    def signingKey = providers
            .environmentVariable("GPG_SIGNING_KEY")
    def signingPassphrase = providers
            .environmentVariable("GPG_SIGNING_PASSPHRASE")
    if (signingKey.isPresent() && signingPassphrase.isPresent()) {
        useInMemoryPgpKeys(signingKey.get(), signingPassphrase.get())
        sign publishing.publications.mavenJava
    } else {
        println "Skipping signing, no GPG key found"
    }
}

task cmake {
    doLast {
        exec {
            workingDir '../..'
            commandLine 'make', 'java'
            environment NUM_THREADS: Runtime.getRuntime().availableProcessors()
        }
    }
}

task copyNativeLibs(type: Copy) {
    from('build') {
        include 'libkuzu_java_native.*'
    }
    into 'src/main/resources'
    dependsOn 'cmake'
}

tasks.sourcesJar {
    mustRunAfter 'copyNativeLibs'
}

test {
    useJUnitPlatform()
}

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