import org.apache.tools.ant.taskdefs.condition.Os

apply plugin: 'org.jetbrains.dokka'

dependencies {
    implementation rootProject
}

def internalPackagePrefixes(sourceDirs) {
    def prefixes = []
    // Kotlin allows packages to deviate from the directory structure, but let's assume they don't:
    sourceDirs.collect { sourceDir ->
        sourceDir.traverse(type: groovy.io.FileType.DIRECTORIES) {
            if (it.name == 'internal') {
                prefixes.add sourceDir.toPath().relativize(it.toPath()).toString().replace(File.separator, '.')
            }
        }
    }
    prefixes
}

ext {
    archivedApiDocsBaseFilename = 'api-docs'
}

jar {
    enabled = false
}

dokkaHtml {
    outputDirectory = file("${rootProject.rootDir}/docs/build/html/api/html")
}

dokkaJavadoc {
    outputDirectory = file("${rootProject.rootDir}/docs/build/html/api/javadoc")
}

[dokkaHtml, dokkaJavadoc].forEach {
    it.dokkaSourceSets {
        customSourceSet {
            sourceRoot(file('../core/src/main/kotlin'))
            sourceRoot(file('../client/rpc/src/main/kotlin'))
            sourceRoot(file('../finance/workflows/src/main/kotlin'))
            sourceRoot(file('../finance/contracts/src/main/kotlin'))
            sourceRoot(file('../client/jackson/src/main/kotlin'))
            sourceRoot(file('../testing/test-utils/src/main/kotlin'))
            sourceRoot(file('../testing/node-driver/src/main/kotlin'))
            sourceRoot(file('../core/src/main/kotlin'))
            sourceRoot(file('../client/rpc/src/main/kotlin'))
            sourceRoot(file('../client/rpc/src/main/kotlin'))

            externalDocumentationLink {
                url.set(new URL("https://fasterxml.github.io/jackson-core/javadoc/2.9/"))
            }
            externalDocumentationLink {
                url.set(new URL("https://docs.oracle.com/javafx/2/api/"))
            }
            externalDocumentationLink {
                url.set(new URL("https://www.bouncycastle.org/docs/docs1.5on/"))
            }
        }
    }
}

task apidocs(dependsOn: ['dokkaHtml', 'dokkaJavadoc']) {
    group "Documentation"
    description "Build API documentation"
}

task makeHTMLDocs(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine "docker", "run", "--rm", "-v", "${project.projectDir}:/opt/docs_builder", "-v", "${project.projectDir}/..:/opt", "corda/docs-builder:latest", "bash", "-c", "make-docsite-html.sh"
    } else {
        commandLine "bash", "-c", "docker run --rm --user \$(id -u):\$(id -g) -v ${project.projectDir}:/opt/docs_builder -v ${project.projectDir}/..:/opt corda/docs-builder:latest bash -c make-docsite-html.sh"
    }
}

task makePDFDocs(type: Exec) {
    if (Os.isFamily(Os.FAMILY_WINDOWS)) {
        commandLine "docker", "run", "--rm", "-v", "${project.projectDir}:/opt/docs_builder", "-v", "${project.projectDir}/..:/opt", "corda/docs-builder:latest", "bash", "-c", "make-docsite-pdf.sh"
    } else {
        commandLine "bash", "-c", "docker run --rm --user \$(id -u):\$(id -g) -v ${project.projectDir}:/opt/docs_builder -v ${project.projectDir}/..:/opt corda/docs-builder:latest bash -c make-docsite-pdf.sh"
    }
}

task makeDocs(dependsOn: ['makeHTMLDocs', 'makePDFDocs'])
apidocs.shouldRunAfter makeDocs

task archiveApiDocs(type: Tar) {
    dependsOn apidocs
    from buildDir
    include 'html/**'
    extension 'tgz'
    compression Compression.GZIP
}

publishing {
    publications {
        if (System.getProperty('publishApiDocs') != null) {
            apply plugin: 'corda.common-publishing'

            archivedApiDocs(MavenPublication) {
                artifact archiveApiDocs {
                    artifactId archivedApiDocsBaseFilename
                }
            }
        }
    }
}
