import java.nio.file._
import scala.jdk.CollectionConverters.*

val copyTestResources = inputKey[Unit]("Copy the native libraries to the base directory")
val appendToLibraryPath = taskKey[Unit]("Append the base directory to the java.library.path system property")
val dropLibraryPath = taskKey[Unit]("Drop the last path from the java.library.path system property")
val wrappedRun = taskKey[Unit]("Run with modified java.library.path")
val wrappedTest = taskKey[TestResult]("Test with modified java.library.path")

def wrap[A1](task: InputKey[A1]): Def.Initialize[Task[Unit]] =
  Def.sequential(appendToLibraryPath, task.toTask(""), dropLibraryPath)

// ThisBuild / turbo := true

val root = (project in file(".")).settings(
  scalaVersion := "2.12.21",
  javacOptions ++= Seq("-source", "1.8", "-target", "1.8", "-h",
  sourceDirectory.value.toPath.resolve("main/native/include").toString),
  libraryDependencies += "com.lihaoyi" %% "utest" % "0.6.6" % "test",
  testFrameworks := Seq(new TestFramework("utest.runner.Framework")),
  copyTestResources := Def.uncached {
    val key = Def.spaceDelimited().parsed.head
    val base = baseDirectory.value.toPath
    val resources = (baseDirectory.value / "src" / "main" / "resources" / key).toPath
    Files.walk(resources).iterator.asScala.foreach { p =>
      Files.copy(p, base.resolve(p.getFileName), StandardCopyOption.REPLACE_EXISTING)
    }
    ()
  },
  appendToLibraryPath := Def.uncached {
    val cp = System.getProperty("java.library.path", "").split(":")
    val newCp = if (cp.contains(".")) cp else cp :+ "."
    System.setProperty("java.library.path", newCp.mkString(":"))
    ()
  },
  dropLibraryPath := Def.uncached {
    val cp = System.getProperty("java.library.path", "").split(":").dropRight(1)
    System.setProperty("java.library.path", cp.mkString(":"))
    ()
  },
  wrappedRun := wrap(Runtime / run).value,
  wrappedTest := Def.uncached {
    wrap(Test / testOnly).value
    TestResult.Passed
  }
)
