package org.pharmgkb.pharmcat.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.jar.Attributes;
import java.util.jar.Manifest;
import javax.annotation.Nonnull;
import org.apache.commons.lang3.StringUtils;
import org.pharmgkb.common.io.util.CliHelper;
import org.pharmgkb.pharmcat.PharmCAT;


/**
 * CLI utility methods.
 *
 * @author Mark Woon
 */
public class CliUtils {


  /**
   * Looks for the pharmcat.properties file in the user's home directory.
   *
   * @throws IllegalStateException if properties file cannot be found
   */
  public static @Nonnull Path getPropsFile(@Nonnull CliHelper cliHelper, @Nonnull String propArgKey) {

    if (cliHelper.hasOption(propArgKey)) {
      return cliHelper.getValidFile(propArgKey, true);
    }

    String home = StringUtils.stripToNull(System.getenv("HOME"));
    if (home == null) {
      home = System.getProperty("user.home");
    }
    if (home == null) {
      throw new IllegalStateException("Cannot determine home directory.  Please specify property file.");
    }
    Path propsFile = Paths.get(home).resolve("pharmcat.properties");
    System.out.println("Looking for properties file in: " + propsFile);
    if (!Files.exists(propsFile)) {
      throw new IllegalStateException("Cannot find " + propsFile);
    }
    if (!Files.isRegularFile(propsFile)) {
      throw new IllegalStateException("Not a file: " + propsFile);
    }
    return propsFile;
  }

  /**
   * Gets the currently tagged version based on the Jar manifest or a generic "development" version if not run from a
   * Jar.
   * @return a String of the PharmCAT version
   * @throws IOException can occur from reading the manifest
   */
  public static String getVersion() throws IOException {
    Class clazz = PharmCAT.class;
    String className = clazz.getSimpleName() + ".class";
    String classPath = clazz.getResource(className).toString();
    if (!classPath.startsWith("jar")) {
      // Class not from JAR
      return "development";
    }
    String manifestPath = classPath.substring(0, classPath.lastIndexOf("!") + 1) +
        "/META-INF/MANIFEST.MF";
    try (InputStream input = new URL(manifestPath).openStream()) {
      Manifest manifest = new Manifest(input);
      Attributes attr = manifest.getMainAttributes();
      return attr.getValue("Implementation-Version");
    }
  }
}

@@ -86,16 +86,45 @@
 Stream;%0A
+import java.io.StringWriter;%0A
 import j
@@ -316,16 +316,54 @@
 onnull;%0A
+import org.apache.commons.io.IOUtils;%0A
 import o
@@ -1679,16 +1679,43 @@
 manifest
+, the current git repo tag,
  or a ge
@@ -1720,16 +1720,22 @@
 generic 
+%0A   * 
 %22develop
@@ -1752,34 +1752,57 @@
 ion 
-if not run from a%0A   * Jar
+as a fallback when neither of those are available
 .%0A  
@@ -2160,24 +2160,24 @@
 h(%22jar%22)) %7B%0A
-
       // Cla
@@ -2192,16 +2192,439 @@
 rom JAR%0A
+      try (StringWriter writer = new StringWriter()) %7B%0A        IOUtils.copy(%0A            Runtime.getRuntime().exec(%22git describe%22, new String%5B%5D%7B %22--tags%22 %7D)%0A                .getInputStream(), %0A            writer);%0A        String gitVersion = writer.toString();%0A        if (StringUtils.isNotBlank(gitVersion)) return gitVersion;%0A      %7D catch (Exception e) %7B%0A        System.err.println(%22Error reading git version%22);%0A      %7D%0A
       re

Improve version detection