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

// default package

import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.regex.Pattern;

/** Set various version values to the one of this program's first argument. */
public class Version {

  public static void main(String... args) throws Exception {
    if (args.length > 1) throw new Error("Only new version expected: " + List.of(args));

    var bach = Path.of("src/de.sormuras.bach/main/java/de/sormuras/bach/Bach.java");
    var pattern = Pattern.compile("Version VERSION = Version.parse\\(\"(.+)\"\\);");
    var matcher = pattern.matcher(Files.readString(bach));
    if (!matcher.find()) throw new Error("Version constant not found in: " + bach);

    var current = matcher.group(1);
    // Only print current version?
    if (args.length == 0) {
      System.out.println(current);
      return;
    }
    // Set new version
    var version = args[0];
    var masterOrVersion = version.endsWith("-ea") ? "master" : version;

    sed(bach, pattern.pattern(), "Version VERSION = Version.parse(\"" + version + "\");");

    var readmeMd = Path.of("README.md");
    var pomXml = Path.of("src/de.sormuras.bach/main/maven/pom.xml");
    var bachBootJsh = Path.of("src/bach/bach-boot.jsh");
    var bachBuildJsh = Path.of("src/bach/bach-build.jsh");

    sed(readmeMd, "# Bach.java .+ -", "# Bach.java " + version + " -");
    sed(pomXml, "<version>.+</version>", "<version>" + version + "</version>");
    sed(bachBootJsh, "\"version\", \".+\"", "\"version\", \"" + masterOrVersion + '"');
    sed(bachBuildJsh, "\"version\", \".+\"", "\"version\", \"" + masterOrVersion + '"');
  }

  private static void sed(Path path, String regex, String replacement) throws Exception {
    Files.writeString(path, Files.readString(path).replaceAll(regex, replacement));
  }
}

@@ -1330,44 +1330,8 @@
 );%0A%0A
-    var current = matcher.group(1);%0A
     
@@ -1414,23 +1414,32 @@
 println(
-current
+matcher.group(1)
 );%0A     
@@ -1511,22 +1511,24 @@
     var 
-master
+snapshot
 OrVersio
@@ -1564,16 +1564,25 @@
  %22master
+-SNAPSHOT
 %22 : vers
@@ -1587,16 +1587,84 @@
 rsion;%0A%0A
+    System.out.println(%22Set version of Bach.java to: %22 + version);%0A%0A
     sed(
@@ -2182,38 +2182,40 @@
 ersion%5C%22, %5C%22%22 + 
-master
+snapshot
 OrVersion + '%22')
@@ -2281,22 +2281,24 @@
 , %5C%22%22 + 
-master
+snapshot
 OrVersio

Fix snapshot version string