/*
 * Bach - Java Shell Builder
 * Copyright (C) 2019 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.Properties;

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

  private static final Path PROPERTIES = Path.of("src/project-info.java.properties");

  public static void main(String... args) throws Exception {
    if (args.length == 0) {
      var properties = new Properties();
      try (var reader = Files.newBufferedReader(PROPERTIES)) {
        properties.load(reader);
      }
      var version = properties.getProperty("version");
      if (version == null) throw new Error("Key 'version' not found in: " + PROPERTIES.toUri());
      System.out.println(version);
      return;
    }

    if (args.length > 1) {
      throw new Error("Exactly one argument, the new version, expected! args=" + List.of(args));
    }
    var version = args[0];
    var readmeMd = Path.of("README.md");
    var bachJava = Path.of("src/bach/Bach.java");
    var bachJsh = Path.of("src/bach/Bach.jsh");
    // var mergedBach = Path.of("src/bach/MergedBach.java");

    sed(PROPERTIES, "version=.+", "version=" + version);
    sed(readmeMd, "# Bach.java .+ -", "# Bach.java " + version + " -");
    sed(bachJava, "String VERSION = \".+\";", "String VERSION = \"" + version + "\";");
    sed(bachJsh, "raw/.+/src", "raw/" + (version.endsWith("-ea") ? "master" : version) + "/src");
    // mergedBach.toFile().setWritable(true);
    // sed(mergedBach, "String VERSION = \".+\";", "String VERSION = \"" + version + "\";");
    // mergedBach.toFile().setWritable(false);
  }

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

@@ -742,26 +742,29 @@
 va.util.
-Properties
+regex.Pattern
 ;%0A%0A/** S
@@ -889,26 +889,21 @@
 al Path 
-PROPERTIES
+BUILD
  = Path.
@@ -914,36 +914,23 @@
 src/
-project-info.java.properties
+bach/Build.java
 %22);%0A
@@ -999,171 +999,214 @@
     
-if (args.length == 0) %7B%0A      var properties = new Properties();%0A      try (var reader = Files.newBufferedReader(PROPERTIES)) %7B%0A        properties.load(reader
+var matcher = Pattern.compile(%22String VERSION = %5C%22(.+)%5C%22;%22).matcher(Files.readString(BUILD));%0A    if (!matcher.find()) %7B%0A      throw new IllegalArgumentException(%22Version constant not found in: %22 + BUILD
 );%0A
-  
     
@@ -1215,155 +1215,102 @@
     
-  
 var 
-version = properties.getProperty(%22version%22);%0A      if (version == null) throw new Error(%22Key 'version' not found in: %22 + PROPERTIES.toUri());
+current = matcher.group(1);%0A    // Only print current version?%0A    if (args.length == 0) %7B
 %0A   
@@ -1331,23 +1331,23 @@
 println(
-version
+current
 );%0A     
@@ -1361,16 +1361,38 @@
 ;%0A    %7D%0A
+    // Set new version
 %0A    if 
@@ -1549,44 +1549,124 @@
     
-var readmeMd = Path.of(%22README.md%22);
+if (version.equals(current)) %7B%0A      System.out.println(%22Same version already set: %22 + current);%0A      return;%0A    %7D
 %0A   
@@ -1825,74 +1825,58 @@
 %22);%0A
-%0A    sed(PROPERTIES, %22version=.+%22, %22version=%22 + version
+    var readmeMd = Path.of(%22README.md%22
 );%0A
+%0A
     sed(
 read
@@ -1875,51 +1875,63 @@
 sed(
-readmeMd, %22# Bach.java .+ -%22, %22# Bach.java 
+BUILD, %22String VERSION = %5C%22.+%5C%22;%22, %22String VERSION = %5C%22
 %22 + 
@@ -1941,18 +1941,19 @@
 sion + %22
- -
+%5C%22;
 %22);%0A    
@@ -2320,16 +2320,88 @@
 false);%0A
+    sed(readmeMd, %22# Bach.java .+ -%22, %22# Bach.java %22 + version + %22 -%22);%0A
   %7D%0A%0A  p

Update Version program