package build;

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

class Bootstrap {
  public static void main(String... args) throws Exception {
    var module = "com.github.sormuras.bach";
    var version = Files.readString(Path.of("VERSION")) + "+BOOTSTRAP";

    var classes = Path.of(".bach/workspace/.bootstrap");
    run(
        "javac",
        "--module=" + module + ",build",
        "--module-version=" + version + "-" + Instant.now(),
        "--module-source-path=./*/main/java" + File.pathSeparator + ".bach",
        "-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,
        "-C",
        classes.resolve(module).toString(),
        ".");
  }

  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);
  }
}

@@ -955,16 +955,60 @@
  + jar,%0A
+        %22--main-class=%22 + module + %22.Main%22,%0A
         

Set main class attribute in bootstrap module