/*
 * Copyright © 2019 Apple Inc. and the ServiceTalk project authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

def servicetalkRoot = "../../"

task buildLocalSite(type: NodeTask) { task ->
  group = "Build"
  description = "Builds the local site with local sources"
  dependsOn installAntora

  def outputDir = file("$buildDir/local")

  inputs.file("site-local.yml")
  inputs.dir("$servicetalkRoot/docs")
  inputs.dir("$servicetalkRoot/servicetalk-examples/docs")

  outputs.dir(outputDir)

  script = file('./node_modules/.bin/antora')
  args = [
      "--stacktrace",
      "--attribute", isReleaseBuild ? "is-release-version=defined" : "is-snapshot-version=defined",
      "--attribute", "source-root=file://${file(servicetalkRoot)}",
      "site-local.yml",
  ]

  // workaround for https://gitlab.com/antora/antora/issues/390
  execOverrides {
    it.environment "NODE_TLS_REJECT_UNAUTHORIZED", 0
  }

  doLast {
    println("Generated local site at: file://$outputDir/index.html")
  }
}

task buildRemoteSite(type: NodeTask) { task ->
  group = "Build"
  description = "Builds the remote site for all versions"
  dependsOn installAntora

  def outputDir = file("$buildDir/remote")

  inputs.file("site-remote.yml")
  outputs.dir(outputDir)

  if (project.ext.isCiBuild) {
    execOverrides {
      it.environment "GIT_CREDENTIALS": "https://${file(System.getProperty("antoraKeyPath")).text}:@github.com"
    }
  }

  script = file('./node_modules/.bin/antora')
  args = [
      "--stacktrace",
      "--attribute", isReleaseBuild ? "is-release-version=defined" : "is-snapshot-version=defined",
      "site-remote.yml"
  ]

  // workaround for https://gitlab.com/antora/antora/issues/390
  execOverrides {
    it.environment "NODE_TLS_REJECT_UNAUTHORIZED", 0
  }

  doLast {
    println("Generated remote site at: file://$outputDir/index.html")
  }
}
