JAL-629 move getdown update to earlier stage
[jalview.git] / src / jalview / bin / Jalview.java
index 99ae815..5f1f228 100755 (executable)
@@ -26,7 +26,9 @@ import java.io.File;
 import java.io.FileOutputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
+import java.io.OutputStream;
 import java.io.OutputStreamWriter;
+import java.io.PrintStream;
 import java.io.PrintWriter;
 import java.net.MalformedURLException;
 import java.net.URI;
@@ -63,6 +65,7 @@ import com.threerings.getdown.util.LaunchUtil;
 import groovy.lang.Binding;
 import groovy.util.GroovyScriptEngine;
 import jalview.bin.ArgParser.Arg;
+import jalview.bin.ArgParser.BootstrapArgs;
 import jalview.ext.so.SequenceOntology;
 import jalview.gui.AlignFrame;
 import jalview.gui.Desktop;
@@ -123,6 +126,8 @@ public class Jalview
 
   private Desktop desktop;
 
+  protected Commands cmds;
+
   public static AlignFrame currentAlignFrame;
 
   static
@@ -281,8 +286,49 @@ public class Jalview
       System.setSecurityManager(null);
     }
 
+    // Move any new getdown-launcher-new.jar into place over old
+    // getdown-launcher.jar
+    String appdirString = System.getProperty("getdownappdir");
+    if (appdirString != null && appdirString.length() > 0)
+    {
+      final File appdir = new File(appdirString);
+      new Thread()
+      {
+        @Override
+        public void run()
+        {
+          LaunchUtil.upgradeGetdown(
+                  new File(appdir, "getdown-launcher-old.jar"),
+                  new File(appdir, "getdown-launcher.jar"),
+                  new File(appdir, "getdown-launcher-new.jar"));
+        }
+      }.start();
+    }
+
     // get args needed before proper ArgParser
-    Map<String, String> bootstrapArgs = ArgParser.bootstrapArgs(args);
+    BootstrapArgs bootstrapArgs = BootstrapArgs.getBootstrapArgs(args);
+
+    if (!Platform.isJS())
+    {
+      // are we being --quiet ?
+      if (bootstrapArgs.contains(Arg.QUIET))
+      {
+        OutputStream devNull = new OutputStream()
+        {
+          @Override
+          public void write(int b)
+          {
+            // DO NOTHING
+          }
+        };
+        System.setOut(new PrintStream(devNull));
+        // redirecting stderr not working
+        if (bootstrapArgs.getList(Arg.QUIET).size() > 1)
+        {
+          System.setErr(new PrintStream(devNull));
+        }
+      }
+    }
 
     System.out
             .println("Java version: " + System.getProperty("java.version"));
@@ -314,7 +360,7 @@ public class Jalview
 
     // get bootstrap properties (mainly for the logger level)
     Properties bootstrapProperties = Cache
-            .bootstrapProperties(bootstrapArgs.get("props"));
+            .bootstrapProperties(bootstrapArgs.get(Arg.PROPS));
 
     // report Jalview version
     Cache.loadBuildProperties(true);
@@ -322,11 +368,14 @@ public class Jalview
     // old ArgsParser
     ArgsParser aparser = new ArgsParser(args);
 
+    // old
     boolean headless = false;
+    // new
+    boolean headlessArg = false;
 
     try
     {
-      String logLevel = bootstrapArgs.containsKey("debug") ? "DEBUG" : null;
+      String logLevel = bootstrapArgs.contains(Arg.DEBUG) ? "DEBUG" : null;
       if (logLevel == null && !(bootstrapProperties == null))
       {
         logLevel = bootstrapProperties.getProperty(Cache.JALVIEWLOGLEVEL);
@@ -335,9 +384,9 @@ public class Jalview
     } catch (NoClassDefFoundError error)
     {
       error.printStackTrace();
-      System.out.println("\nEssential logging libraries not found."
-              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview");
-      System.exit(0);
+      String message = "\nEssential logging libraries not found."
+              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview";
+      Jalview.exit(message, 0);
     }
 
     // register SIGTERM listener
@@ -363,8 +412,8 @@ public class Jalview
       }
     });
 
-    String usrPropsFile = bootstrapArgs.containsKey("props")
-            ? bootstrapArgs.get("props")
+    String usrPropsFile = bootstrapArgs.contains(Arg.PROPS)
+            ? bootstrapArgs.get(Arg.PROPS)
             : aparser.getValue("props");
     Cache.loadProperties(usrPropsFile);
     if (usrPropsFile != null)
@@ -374,22 +423,16 @@ public class Jalview
     }
 
     // new ArgParser
-    ArgParser argparser = new ArgParser(args);
-
-    if (argparser.isSet(Arg.HEADLESS))
-      headless = argparser.getBool(Arg.HEADLESS);
-    boolean commandsSuccess = Commands.processArgs(argparser, headless);
-    if (commandsSuccess)
+    ArgParser argparser;
+    // --argfile=... -- OVERRIDES ALL NON-BOOTSTRAP ARGS
+    if (bootstrapArgs.contains(Arg.ARGFILE))
     {
-      Console.info("Successfully completed commands");
-      if (headless)
-        System.exit(0);
+      argparser = ArgParser
+              .parseArgFiles(bootstrapArgs.getList(Arg.ARGFILE));
     }
     else
     {
-      Console.warn("Error when running commands");
-      if (headless)
-        System.exit(1);
+      argparser = new ArgParser(args);
     }
 
     if (!Platform.isJS())
@@ -399,20 +442,24 @@ public class Jalview
      * @j2sIgnore
      */
     {
-      if (argparser.isSet(Arg.HEADLESS))
+      if (aparser.contains("help") || aparser.contains("h")
+              || argparser.getBool(Arg.HELP))
       {
-        headless = argparser.getBool(Arg.HEADLESS);
+        showUsage();
+        Jalview.exit(null, 0);
       }
 
-      if (aparser.contains("help") || aparser.contains("h"))
+      if (bootstrapArgs.contains(Arg.HEADLESS))
       {
-        showUsage();
-        System.exit(0);
+        System.setProperty("java.awt.headless", "true");
+        // new
+        headlessArg = argparser.getBool(Arg.HEADLESS);
       }
-      if (headless || aparser.contains("nodisplay")
-              || aparser.contains("nogui") || aparser.contains("headless"))
+      if (aparser.contains("nodisplay") || aparser.contains("nogui")
+              || aparser.contains("headless"))
       {
         System.setProperty("java.awt.headless", "true");
+        // old
         headless = true;
       }
       // anything else!
@@ -473,13 +520,14 @@ public class Jalview
     NoClassDefFoundError error)
     {
       error.printStackTrace();
-      System.out.println("\nEssential logging libraries not found."
-              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview");
-      System.exit(0);
+      String message = "\nEssential logging libraries not found."
+              + "\nUse: java -classpath \"$PATH_TO_LIB$/*:$PATH_TO_CLASSES$\" jalview.bin.Jalview";
+      Jalview.exit(message, 0);
     }
     desktop = null;
 
-    setLookAndFeel();
+    if (!(headless || headlessArg))
+      setLookAndFeel();
 
     /*
      * configure 'full' SO model if preferences say to, else use the default (full SO)
@@ -491,7 +539,7 @@ public class Jalview
       SequenceOntologyFactory.setInstance(new SequenceOntology());
     }
 
-    if (!headless)
+    if (!(headless || headlessArg))
     {
       Desktop.nosplash = aparser.contains("nosplash");
       desktop = new Desktop();
@@ -613,6 +661,25 @@ public class Jalview
         }
       }
     }
+    // Run Commands from cli
+    cmds = new Commands(argparser, headlessArg);
+    boolean commandsSuccess = cmds.argsWereParsed();
+    if (commandsSuccess)
+    {
+      if (headlessArg)
+      {
+        Jalview.exit("Successfully completed commands in headless mode", 0);
+      }
+      Console.info("Successfully completed commands");
+    }
+    else
+    {
+      if (headlessArg)
+      {
+        Jalview.exit("Error when running Commands in headless mode", 1);
+      }
+      Console.warn("Error when running commands");
+    }
 
     // Check if JVM and compile version might cause problems and log if it
     // might.
@@ -624,25 +691,6 @@ public class Jalview
               + LaunchUtils.getJavaCompileVersion() + ".");
     }
 
-    // Move any new getdown-launcher-new.jar into place over old
-    // getdown-launcher.jar
-    String appdirString = System.getProperty("getdownappdir");
-    if (appdirString != null && appdirString.length() > 0)
-    {
-      final File appdir = new File(appdirString);
-      new Thread()
-      {
-        @Override
-        public void run()
-        {
-          LaunchUtil.upgradeGetdown(
-                  new File(appdir, "getdown-launcher-old.jar"),
-                  new File(appdir, "getdown-launcher.jar"),
-                  new File(appdir, "getdown-launcher-new.jar"));
-        }
-      }.start();
-    }
-
     String file = null, data = null;
 
     FileFormatI format = null;
@@ -659,8 +707,7 @@ public class Jalview
 
     if (file == null && desktop == null)
     {
-      System.out.println("No files to open!");
-      System.exit(1);
+      Jalview.exit("No files to open!", 1);
     }
 
     long progress = -1;
@@ -687,11 +734,12 @@ public class Jalview
         {
           if (!(new File(file)).exists())
           {
-            System.out.println("Can't find " + file);
             if (headless)
             {
-              System.exit(1);
+              Jalview.exit(
+                      "Can't find file '" + file + "' in headless mode", 1);
             }
+            Console.warn("Can't find file'" + file + "'");
           }
         }
       }
@@ -726,7 +774,7 @@ public class Jalview
           if (cs != null)
           {
             System.out.println(
-                    "CMD [-color " + data + "] executed successfully!");
+                    "CMD [-colour " + data + "] executed successfully!");
           }
           af.changeColour(cs);
         }
@@ -929,7 +977,10 @@ public class Jalview
     // ////////////////////
 
     if (!Platform.isJS() && !headless && file == null
-            && Cache.getDefault("SHOW_STARTUP_FILE", true))
+            && Cache.getDefault("SHOW_STARTUP_FILE", true)
+            && !cmds.commandArgsProvided())
+    // don't open the startup file if command line args have been processed
+    // (&& !Commands.commandArgsProvided())
     /**
      * Java only
      * 
@@ -1528,7 +1579,7 @@ public class Jalview
   public void quit()
   {
     // System.exit will run the shutdownHook first
-    System.exit(0);
+    Jalview.exit("Quitting now. Bye!", 0);
   }
 
   public static AlignFrame getCurrentAlignFrame()
@@ -1540,4 +1591,22 @@ public class Jalview
   {
     Jalview.currentAlignFrame = currentAlignFrame;
   }
+
+  protected Commands getCommands()
+  {
+    return cmds;
+  }
+
+  public static void exit(String message, int exitcode)
+  {
+    System.err.println("####### EXITING HERE!");
+    Console.debug("Using Jalview.exit");
+    if (message != null)
+      if (exitcode == 0)
+        Console.info(message);
+      else
+        Console.error(message);
+    if (exitcode > -1)
+      System.exit(exitcode);
+  }
 }