/*
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */

import org.apache.lucene.gradle.plugins.globals.LuceneBuildGlobalsExtension

if (project != project.rootProject) {
  throw new GradleException("Applicable to rootProject only: " + project.path)
}

configure(rootProject) {
  LuceneBuildGlobalsExtension buildGlobals = rootProject.extensions.getByType(LuceneBuildGlobalsExtension)
  def urlVersion = buildGlobals.baseVersion.replace('.', '_')

  Provider<String> luceneJavadocUrl = buildOptions.addOption("lucene.javadoc.url",
      "External Javadoc URL for documentation generator.", provider {
        if (buildGlobals.snapshotBuild) {
          // non-release build does not cross-link between modules.
          return null
        } else {
          // release build
          return "https://lucene.apache.org/core/${urlVersion}".toString()
        }
      })

  def documentationTask = tasks.register("documentation", {
    group = 'documentation'
    description = 'Generate all documentation'

    dependsOn ':lucene:documentation:assemble'
  })

  assemble.dependsOn documentationTask
}

configure(project(':lucene:documentation')) {
  ext {
    docroot = layout.buildDirectory.dir("site").get().asFile

    markdownSrc = file("src/markdown")
    assets = file("src/assets")
  }

  def documentationTask = tasks.register("documentation", {
    group = 'documentation'
    description = "Generate ${project.name.capitalize()} documentation"

    dependsOn project(":lucene").subprojects.collect { prj ->
      prj.tasks.matching { it.name == 'renderSiteJavadoc' }
    }

    dependsOn 'changesToHtml', 'copyDocumentationAssets',
        'markdownToHtml', 'createDocumentationIndex'
  })

  // in CI, fully build documentation. this is very costly, way too
  // much for a developer workflow, but it validates build machinery,
  // and link references from overview.html, that javac won't catch
  LuceneBuildGlobalsExtension buildGlobals = rootProject.extensions.getByType(LuceneBuildGlobalsExtension)
  if (buildGlobals.isCIBuild) {
    tasks.named("check").configure {
      dependsOn 'documentation'
    }
  }

  tasks.register("copyDocumentationAssets", Copy, {
    includeEmptyDirs = false
    from project.ext.assets
    into project.ext.docroot
  })

  assemble {
    dependsOn documentationTask
  }

  configurations {
    site
  }

  artifacts {
    site project.ext.docroot, {
      builtBy documentationTask
    }
  }
}

configure(project(":lucene")) {
  ext {
    docroot = project('documentation').docroot
  }
}
