buildscript {
    repositories {
        mavenLocal()
        mavenCentral()
    }
    dependencies {
        classpath "io.opencaesar.owl:owl-fuseki-gradle:[2.0,3.0)"
        classpath "io.opencaesar.owl:owl-shacl-fuseki-gradle:[2.0,3.0)"
        classpath "io.opencaesar.owl:owl-query-gradle:[2.0,3.0)"
        classpath "io.opencaesar.owl:owl-load-gradle:[2.0,3.0)"
        classpath "io.opencaesar.owl:owl-reason-gradle:[2.0,3.0)"
        classpath "io.opencaesar.owl:owl-doc-gradle:[2.0,3.0)"
        //classpath "io.opencaesar.oml:oml-bikeshed-gradle:[2.0,3.0)"
        classpath "io.opencaesar.oml:oml-merge-gradle:[2.0,3.0)"
        classpath "io.opencaesar.adapters:oml2owl-gradle:[2.0,3.0)"
    }
}

plugins {
    id "java"
    id "maven-publish"
    id "eclipse"
    id "signing"
}

ext.title = "System Design Ontology - 2 Layers"
description = "System design ontology considering only 2 layers (types and instances)."
group = "be.ua"
version = "1.0.0"

ext {
    dataset = "SystemDesignOntology2Layers"
    rootIri = "http://ua.be/sdo2l/description/bundle"
    //url = ""
    //bikeshed = "build/bikeshed"
}

repositories {
    mavenLocal()
    mavenCentral()
    maven {
      //url "https://git.rys.one/api/v4/groups/dtdesign/-/packages/maven"
        url "https://git.rys.one/api/v4/projects/${System.getenv('CI_PROJECT_ID')}/packages/maven"
        name "Gitlab"
        credentials(HttpHeaderCredentials) {
            name = "Job-Token"
            value = System.getenv("CI_JOB_TOKEN")
        }
        authentication {
            header(HttpHeaderAuthentication)
        }
    }
}

configurations {
    oml
}

dependencies {
    //oml "io.opencaesar.ontologies:core-vocabularies:[4.0,5.0)"
    oml "io.opencaesar.ontologies:metrology-vocabularies:[6.0,7.0)"
}

task downloadDependencies(type: io.opencaesar.oml.merge.OmlMergeTask) {
    inputZipPaths = configurations.oml.files
    outputCatalogFolder = file("build/oml")
}

//task omlToBikeshed(type: io.opencaesar.oml.bikeshed.Oml2BikeshedTask, dependsOn: downloadDependencies) {
//    inputCatalogPath = file("catalog.xml")
//    inputCatalogTitle = project.title
//    inputCatalogVersion = project.version
//    rootOntologyIri = "$rootIri".toString()
//    outputFolderPath = file("$bikeshed")
//    publishUrl = "$url".toString()
//}

//task generateDocs(type: Exec, dependsOn: omlToBikeshed) {
//    inputs.files(fileTree("$bikeshed").include("**/*.bs"))
//    outputs.files(fileTree("$bikeshed").include("**/*.html"))
//    if (org.gradle.internal.os.OperatingSystem.current().isWindows()) {
//        commandLine "$bikeshed/publish.bat"
//    } else {
//        commandLine "$bikeshed/publish.sh"
//    }
//}

task omlToOwl(type: io.opencaesar.oml2owl.Oml2OwlTask, dependsOn: downloadDependencies) {
    inputCatalogPath = file("catalog.xml")
    outputCatalogPath = file("build/owl/catalog.xml")
}

// A task to run the Openllet reasoner on the OWL catalog
task owlReason(type: io.opencaesar.owl.reason.OwlReasonTask, dependsOn: omlToOwl) {
    // OWL catalog
    catalogPath = file("build/owl/catalog.xml")
    // Input ontology IRI to reason on
    inputOntologyIri = "$rootIri".toString()
    // Entailment statements to generate and the ontologies to persist them in
    specs = [
        "$rootIri/classes = ALL_SUBCLASS".toString(),
        "$rootIri/properties = INVERSE_PROPERTY | ALL_SUBPROPERTY".toString(),
        "$rootIri/individuals = ALL_INSTANCE | DATA_PROPERTY_VALUE | OBJECT_PROPERTY_VALUE | SAME_AS".toString()
    ]
    // Junit error report
    reportPath = file("build/reports/reasoning.xml")
}

task generateDocs(type: io.opencaesar.owl.doc.OwlDocTask, dependsOn: owlReason) {
    inputCatalogPath = file("build/owl/catalog.xml")
    inputCatalogTitle = project.title
    inputCatalogVersion = project.version
    inputOntologyIris = ["$rootIri/classes", "$rootIri/properties", "$rootIri/individuals"]
    outputFolderPath = file("build/docs")
    outputCaseSensitive = org.gradle.internal.os.OperatingSystem.current().isLinux()
}

task startFuseki(type: io.opencaesar.owl.fuseki.StartFusekiTask) {
    configurationPath = file(".fuseki.ttl")
    outputFolderPath = file(".fuseki")
    webUI = true
}

task stopFuseki(type: io.opencaesar.owl.fuseki.StopFusekiTask) {
    outputFolderPath = file(".fuseki")
}

task owlLoad(type: io.opencaesar.owl.load.OwlLoadTask, dependsOn: owlReason) {
    inputs.files(startFuseki.outputFolderPath) // rerun when fuseki restarts
    catalogPath = file("build/owl/catalog.xml")
    endpointURL = "http://localhost:3030/$dataset".toString()
    fileExtensions = ["owl", "ttl"]
    iris = [
        "$rootIri/classes".toString(),
        "$rootIri/properties".toString(),
        "$rootIri/individuals".toString()
    ]
}

task owlQuery(type: io.opencaesar.owl.query.OwlQueryTask, dependsOn: owlLoad) {
    inputs.files(owlLoad.inputs.files) // rerun when the dataset changes
    endpointURL = "http://localhost:3030/$dataset".toString()
    queryPath = file("src/sparql")
    resultPath = file("build/results")
    format = "json"
}

task owlShacl(type: io.opencaesar.owl.shacl.fuseki.OwlShaclFusekiTask, dependsOn: owlLoad) {
    inputs.files(owlLoad.inputs.files) // rerun when the dataset changes
    endpointURL = "http://localhost:3030/$dataset".toString()
    queryPath = file("src/shacl")
    resultPath = file("build/reports")
}

build {
    dependsOn owlReason
}

tasks.named("clean") {

}

task omlZip(type: Zip) {
    from file("src/oml")
    include "**/*.oml"
    destinationDirectory = file("build/libs")
    archiveBaseName = project.name
    archiveVersion = project.version
}

def pomConfig = {
    licenses {
        license {
            name "The Apache Software License, Version 2.0"
            url "http://www.apache.org/licenses/LICENSE-2.0.txt"
            distribution "repo"
        }
    }
    developers {
        developer {
            id "uantwerp"
            name "University of Antwerp"
            email ""
        }
    }
    scm {
        url "https://msdl.uantwerpen.be/git/lucasalbertins/DTDesign/"
    }
}

publishing {
    publications {
        maven(MavenPublication) {
            groupId project.group
            artifactId project.name
            version project.version
            artifact omlZip
            pom {
                packaging = "zip"
                withXml {
                    def root = asNode()
                    if (configurations.find { it.name == "oml" }) {
                        def dependencies = root.appendNode("dependencies")
                        configurations.oml.resolvedConfiguration.resolvedArtifacts.each {
                            def dependency = dependencies.appendNode("dependency")
                            dependency.appendNode("groupId", it.moduleVersion.id.group)
                            dependency.appendNode("artifactId", it.moduleVersion.id.name)
                            dependency.appendNode("version", it.moduleVersion.id.version)
                            if (it.classifier != null) {
                                dependency.appendNode("classifier", it.classifier)
                                dependency.appendNode("type", it.extension)
                            }
                        }
                    }
                    root.appendNode("name", project.ext.title)
                    root.appendNode("description", project.description)
                    root.appendNode("url", "https://msdl.uantwerpen.be/git/lucasalbertins/DTDesign/")
                    root.children().last() + pomConfig
                }
            }
        }
    }
    repositories {
        maven {
          //url "https://git.rys.one/api/v4/groups/dtdesign/-/packages/maven"
            url "https://git.rys.one/api/v4/projects/${System.getenv('CI_PROJECT_ID')}/packages/maven"
            name "Gitlab"
            credentials(HttpHeaderCredentials) {
                name = "Job-Token"
                value = System.getenv("CI_JOB_TOKEN")
            }
            authentication {
                header(HttpHeaderAuthentication)
            }
        }
    }
}

//signing {
//    def pgpSigningKey = project.findProperty("pgpSigningKey")
//    if (pgpSigningKey != null) { pgpSigningKey = new String(pgpSigningKey.decodeBase64()) }
//    def pgpSigningPassword = project.findProperty("pgpSigningPassword")
//    useInMemoryPgpKeys(pgpSigningKey, pgpSigningPassword)
//    sign publishing.publications.maven
//}

//gradle.taskGraph.whenReady { taskGraph ->
//    signMavenPublication.onlyIf { taskGraph.allTasks.any{it.name == "publishMavenPublicationToGitlabRepository"} }
//}

eclipse {
    synchronizationTasks downloadDependencies
}
