/*
 * 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.misc.QuietExec

// Regenerates "ForUtil.java" code used by codecs.

void addGenerateFromPythonTask(Project project, File genDir, String taskName, String pyScript, String javaFile) {
  var generateTaskProvider = project.getTasks().register(
      "generate" + taskName + "Internal", QuietExec, {
        description = "Regenerate " + javaFile + " using " + pyScript + " in " + genDir
        group = "generation"

        def genScript = project.file("${genDir}/${pyScript}")
        def genOutput = project.file("${genDir}/${javaFile}")
        inputs.file genScript
        outputs.file genOutput

        workingDir = genDir
        executable = buildGlobals.externalTool("python3")
        args = ['-B', genScript.toString()]
      })

  var wrapped = project.wrapWithPersistentChecksums2(generateTaskProvider, [
    andThenTasks: ["applyGoogleJavaFormat"],
    mustRunBefore: ["compileJava"]
  ])

  project.getTasks().named("regenerate").configure {
    dependsOn wrapped
  }
}

configure(project(":lucene:core")) {
  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/codecs/lucene104"), "ForUtil", "gen_ForUtil.py", "ForUtil.java")
}

configure(project(":lucene:backward-codecs")) {
  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene84"), "ForUtil84", "gen_ForUtil.py", "ForUtil.java")

  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene90"), "ForUtil90", "gen_ForUtil.py", "ForUtil.java")

  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene99"), "ForUtil99", "gen_ForUtil.py", "ForUtil.java")

  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene912"), "ForUtil912", "gen_ForUtil.py", "ForUtil.java")
  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene912"), "ForDeltaUtil912", "gen_ForDeltaUtil.py", "ForDeltaUtil.java")

  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene101"), "ForUtil101", "gen_ForUtil.py", "ForUtil.java")
  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene101"), "ForDeltaUtil101", "gen_ForDeltaUtil.py", "ForDeltaUtil.java")

  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene103"), "ForUtil103", "gen_ForUtil.py", "ForUtil.java")
  addGenerateFromPythonTask(project,
      file("src/java/org/apache/lucene/backward_codecs/lucene103"), "ForDeltaUtil103", "gen_ForDeltaUtil.py", "ForDeltaUtil.java")
}
