/*
 * Bach - Java Shell Builder
 * Copyright (C) 2020 Christian Stein
 *
 * Licensed 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
 *
 *     https://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.
 */

package build;

import java.io.File;
import java.lang.module.ModuleDescriptor;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.Instant;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.spi.ToolProvider;

public class Bootstrap {
  public static void main(String... args) throws Exception {
    var destination = Path.of(".bach/workspace/classes/bootstrap");
    var pattern = DateTimeFormatter.ofPattern("yyyy.MM.dd.HHmm").withZone(ZoneId.of("UTC"));
    var version = ModuleDescriptor.Version.parse(pattern.format(Instant.now()));

    run(
        "javac",
        "-d",
        "" + destination,
        "--module=build,de.sormuras.bach",
        "--module-source-path=.bach/src" + File.pathSeparator + "src/*/main/java",
        "--module-version=" + version + "-BOOTSTRAP",
        "-encoding",
        "UTF-8",
        "-W" + "error",
        "-X" + "lint");

    var java = ProcessHandle.current().info().command().orElse("java");
    start(
        java,
        "-ea",
        "-D" + "user.language=en",
        "-D" + "java.util.logging.config.file=src/logging.properties",
        "-D" + "ebug",
        "--enable-preview",
        "--module-path=" + destination,
        "--module=build/build.Build");

    var modules = Path.of(".bach/workspace/modules");
    if (Files.notExists(modules)) {
      System.err.println("Modules directory not found: " + modules);
      return;
    }

    start(
        java,
        "--module-path",
        modules.toString(),
        "--module",
        "de.sormuras.bach",
        "help");
  }

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

  static void start(String... command) throws Exception {
    System.out.println(">> " + String.join(" ", command));
    var process = new ProcessBuilder(command).inheritIO().start();
    int code = process.waitFor();
    if (code != 0) throw new Error("Non-zero exit code: " + code);
  }
}

@@ -1216,24 +1216,48 @@
 t.now()));%0A%0A
+    step(%22Bootstrap%22);%0A%0A
     run(%0A   
@@ -1253,16 +1253,16 @@
    run(%0A
-
         
@@ -1573,24 +1573,44 @@
 + %22lint%22);%0A%0A
+    step(%22Build%22);%0A%0A
     var java
@@ -2136,34 +2136,66 @@
   st
-art(%0A        java,%0A       
+ep(%22Smoke-test module de.sormuras.bach%22);%0A%0A    start(java,
  %22--
@@ -2207,24 +2207,16 @@
 e-path%22,
-%0A       
  modules
@@ -2227,24 +2227,16 @@
 tring(),
-%0A       
  %22--modu
@@ -2239,24 +2239,16 @@
 module%22,
-%0A       
  %22de.sor
@@ -2263,23 +2263,240 @@
 ch%22,
-%0A        %22help%22
+ %22help%22);%0A%0A    step(%22Boostrap finished.%22);%0A  %7D%0A%0A  static void step(String caption) %7B%0A    System.out.println();%0A    System.out.println(%22#%22);%0A    System.out.println(%22# %22 + caption);%0A    System.out.println(%22#%22);%0A    System.out.println(
 );%0A 

Print step information while boot-strapping