buildscript {
    repositories {
        maven {
            credentials {
                username mavenUsername
                password mavenPassword
            }
            url mavenRepository
        }
    }
    dependencies {
        if (file("libs/org/docToolchain/core-${project.properties.dtc_version}.jar").exists()) {
            classpath files("libs/org/docToolchain/core-${project.properties.dtc_version}.jar")
        } else {
            classpath("org.docToolchain:core")
        }
    }
}

import org.docToolchain.tasks.VerifyConfluenceApiAccessTask
import org.docToolchain.tasks.WipeConfluenceSpaceTask
import org.docToolchain.tasks.Asciidoc2ConfluenceTask
import org.docToolchain.scripts.*

//tag::verifyConfluenceApiAccess[]
task verifyConfluenceApiAccess(
    description: 'verifies confluence API URL and credentials are set correctly',
    group: 'docToolchain'
) {
    doLast {
        //TODO default should be false, if the V1 has been removed in cloud
        config.confluence.useV1Api = findProperty("confluence.useV1Api") != null ?
            findProperty("confluence.useV1Api") : config.confluence.useV1Api != [:] ?
            config.confluence.useV1Api :true
        new VerifyConfluenceApiAccessTask(config).execute()
    }
}
//end::verifyConfluenceApiAccess[]

//tag::wipeConfluenceSpace[]
task wipeConfluenceSpace(
    description: 'deletes all pages in the configured confluence space',
    group: 'docToolchain'
) {
    doLast {
        ant.input(message: "${'Do you really want to delete all pages in ' + config.confluence.spaceKey}", validargs: 'y,n', addproperty: 'confirm')
        if(ant.confirm.toBoolean()) {
            //TODO default should be false, if the V1 has been removed in cloud
            config.confluence.useV1Api = findProperty("confluence.useV1Api") != null ?
                findProperty("confluence.useV1Api") : config.confluence.useV1Api != [:] ?
                config.confluence.useV1Api :true
            new WipeConfluenceSpaceTask(config).execute()
        } else {
            println("Aborting wipe confluence space, this task needs to be confirmed with 'y'")
        }

    }
}
//end::wipeConfluenceSpace[]

//tag::publishToConfluence[]
task publishToConfluence(
        description: 'publishes the HTML rendered output to confluence',
        group: 'docToolchain'
) {
    doLast {
        logger.info("docToolchain> docDir: "+docDir)
        config.confluence.api = findProperty("confluence.api")?:config.confluence.api
        //TODO default should be false, if the V1 has been removed in cloud
        config.confluence.useV1Api = findProperty("confluence.useV1Api") != null ?
            findProperty("confluence.useV1Api") : config.confluence.useV1Api != [:] ?
            config.confluence.useV1Api :true
        Asciidoc2ConfluenceTask.From(config, docDir).execute()
    }
}
//end::publishToConfluence[]
project.afterEvaluate {
    project.tasks.publishToConfluence.dependsOn generateHTML
}
