package com.conveyal.r5;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * This collects the Maven and Git version information from a properties file written during the Maven build.
 * We use this to allow the backend to report the exact version of the code it is running via an API. This is useful
 * in automated deployment, testing, and debugging situations. Note that when building the code in an IDE, the version
 * information may not be supplied to this class. It may only be provided in a command line Maven build.
 */
public class SoftwareVersion {

    private static final Logger LOG = LoggerFactory.getLogger(SoftwareVersion.class);
    private static final String VERSION_PROPERTIES_FILE = "version.properties";
    private static final String UNKNOWN = "UNKNOWN";

    // This could potentially be made into a Component so it's non-static
    public static SoftwareVersion instance = new SoftwareVersion();

    private final Properties properties = new Properties();

    // Version number should be very similar to a git describe, with possible addition of .dirty
    public final String version;
    public final String commit;
    public final String branch;

    protected SoftwareVersion () {
        try (InputStream is = getClass().getResourceAsStream(VERSION_PROPERTIES_FILE)) {
            properties.load(is);
        } catch (IOException | NullPointerException e) {
            LOG.error("Error loading version and commit information: {}", e.toString());
        }
        version = getPropertyOrUnknown("version");
        commit = getPropertyOrUnknown("commit");
        branch = getPropertyOrUnknown("branch");
    }

    /**
     * Return the value for the given key in the supplied Properties. If the key is not present or if the value appears
     * to be an uninterpolated Maven variable, return a fallback value. See R5 issue #190.
     */
    private String getPropertyOrUnknown(String key) {
        String result = properties.getProperty(key);
        if (result == null || result.startsWith("${")) {
            return UNKNOWN;
        }
        return result;
    }

}

@@ -1013,19 +1013,23 @@
 Version(
+%22r5%22
 );%0A
-
 %0A    pri
@@ -1277,16 +1277,163 @@
 ranch;%0A%0A
+    // Which software product is this a version of? Provides a scope or context for the version and commit strings.%0A    public final String name;%0A%0A
     prot
@@ -1455,16 +1455,34 @@
 ersion (
+String productName
 ) %7B%0A    
@@ -1851,24 +1851,24 @@
 (%22commit%22);%0A
-
         bran
@@ -1900,24 +1900,52 @@
 (%22branch%22);%0A
+        name = productName;%0A
     %7D%0A%0A    /

add software product name to SoftwareVersion