package build;

import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.util.Comparator;
import java.util.spi.ToolProvider;

class Bootstrap {
  public static void main(String... args) throws Exception {
    var module = "com.github.sormuras.bach";
    var version = "16-ea+BOOTSTRAP";

    deleteCached("com.github.sormuras.bach@*.jar");
    deleteWorkspaces();

    var classes = Path.of(".bach/workspace/.bootstrap");
    run(
        "javac",
        "--module=" + module,
        "--module-version=" + version + "-" + Instant.now(),
        "--module-source-path=./*/main/java",
        "-g",
        "-parameters",
        "-Werror",
        "-Xlint",
        "-encoding",
        "UTF-8",
        "-d",
        classes.toString());

    var file = module + "@" + version + ".jar";
    var jar = Files.createDirectories(Path.of(".bach/cache")).resolve(file);
    run(
        "jar",
        "--create",
        "--file=" + jar,
        "--main-class=" + module + ".Main",
        "-C",
        classes.resolve(module).toString(),
        ".");
  }

  static void deleteCached(String glob) throws Exception {
    var cache = Path.of(".bach/cache");
    try (var stream = Files.newDirectoryStream(cache, glob)) {
      for (var path : stream) {
        Files.delete(path);
        System.out.println(">> " + path + " deleted");
      }
    }
  }

  static void deleteWorkspaces() throws Exception {
    try (var stream = Files.walk(Path.of(""))) {
      var selected =
          stream
              .filter(path -> path.toUri().toString().contains(".bach/workspace"))
              .sorted(Comparator.reverseOrder())
              .toArray(Path[]::new);
      for (var path : selected) Files.delete(path);
      System.out.println(">> " + selected.length + " workspace files deleted");
    }
  }

  static void run(String name, String... args) {
    System.out.println(">> " + name + " " + String.join(" ", args));
    var tool = ToolProvider.findFirst(name).orElseThrow();
    var code = tool.run(System.out, System.err, args);
    if (code != 0) throw new Error("Non-zero exit code: " + code);
  }
}

@@ -9,16 +9,65 @@
 build;%0A%0A
+import java.nio.file.DirectoryNotEmptyException;%0A
 import j
@@ -1769,16 +1769,26 @@
 elected)
+ %7B%0A       
  Files.d
@@ -1796,24 +1796,277 @@
 lete(path);%0A
+        // also delete non-empty %22.bach%22 directory%0A        if (path.endsWith(Path.of(%22.bach%22, %22workspace%22)))%0A          try %7B%0A            Files.deleteIfExists(path.getParent());%0A          %7D catch (DirectoryNotEmptyException ignore) %7B%0A          %7D%0A      %7D%0A
       System

Delete non-empty .bach directories on bootstrap