JAL-4160 JAL-3830 Improvements to the jalview.sh bash script especially when not...
[jalview.git] / utils / getdown / bin / jalview.sh
index 62da28c..371bfcd 100755 (executable)
@@ -36,18 +36,47 @@ if [ "$( uname -s )" = "Darwin" ]; then
   ISMACOS=1
 fi
 
+# check for headless mode
+HEADLESS=0
+GUI=0
+HELP=0
+DEBUG=0
+for ARG in "${@}"; do
+  case "${ARG}" in
+    "--headless")
+      HEADLESS=1
+      ;;
+    "--help")
+      HELP=1
+      ;;
+    --help-*)
+      HELP=1
+      ;;
+    "--gui")
+      GUI=1
+      ;;
+    "--debug")
+      DEBUG=1
+      ;;
+  esac
+  
+  if [ "${HELP}" = 1 ]; then
+    # --help takes precedence
+    HEADLESS=1
+    GUI=0
+  elif [ "${GUI}" = 1 ]; then
+    # --gui takes precedence over --headless
+    HEADLESS=0
+  fi
+done
+
 declare -a JVMARGS=()
 
 # set vars for being inside the macos App Bundle
 if [ "${ISMACOS}" = 1 ]; then
 # MACOS ONLY
   DIR="$(dirname "$(readlinkf "$0")")"
-  APP="${DIR%.app/Contents/*}".app
-  if [ "${APP}" = "${APP%.app}" ]; then
-    echo "Could not find Jalview.app" >&2
-    exit 2
-  fi
-  APPDIR="${APP}/Contents/Resources/app"
+  APPDIR="${DIR%/bin}"
   JAVA="${APPDIR}/jre/Contents/Home/bin/java"
   JVMARGS=( "${JVMARGS[@]}" "-Xdock:icon=${APPDIR}/resource/jalview_logo.png" )
 else
@@ -57,6 +86,11 @@ else
   JAVA="${APPDIR}/jre/bin/java"
 fi
 
+if [ "${HEADLESS}" = 1 ]; then
+  # this suppresses the Java icon appearing in the macOS Dock and maybe other things in other OSes
+  JVMARGS=( "${JVMARGS[@]}" "-Djava.awt.headless=true" )
+fi
+
 SYSJAVA=java
 GETDOWNTXT="${APPDIR}/getdown.txt"
 
@@ -65,7 +99,8 @@ CLASSPATH=""
 declare -a JARPATHS=()
 if [ -e "${GETDOWNTXT}" ]; then
   # always check grep and sed regexes on macos -- they're not the same
-  for JAR in $(grep -e '^code\s*=\s*' "${GETDOWNTXT}" | sed -e 's/^code\s*=\s*//;'); do
+for JAR in $(grep -e '^code[[:space:]]*=[[:space:]]*' "${GETDOWNTXT}" | while read -r line; do echo $line | sed -E -e 's/code[[:space:]]*=[[:space:]]*//;'; done);
+  do
     [ -n "${CLASSPATH}" ] && CLASSPATH="${CLASSPATH}:"
     CLASSPATH="${CLASSPATH}${APPDIR}/${JAR}"
     JARPATHS=( "${JARPATHS[@]}" "${APPDIR}/${JAR}" )
@@ -122,6 +157,7 @@ elif command -v resize 2>&1 >/dev/null; then
   COLUMNS=$(resize -u | grep COLUMNS= | sed -e 's/.*=//;s/;//') 2>/dev/null
 fi
 JVMARGS=( "${JVMARGS[@]}" "-DCONSOLEWIDTH=${COLUMNS}" )
+JVMARGS=( "${JVMARGS[@]}" "-Dgetdownappdir=${APPDIR}" )
 
 # Is there a bundled Java?  If not just try one in the PATH (do need .exe in WSL)
 if [ \! -e "${JAVA}" ]; then
@@ -129,4 +165,8 @@ if [ \! -e "${JAVA}" ]; then
   echo "Cannot find bundled java, using system ${JAVA} and hoping for the best!" >&2
 fi
 
+if [ "${DEBUG}" = 1 ]; then
+ echo Shell running: \""${JAVA}"\" \""${JVMARGS[@]}"\" -cp \""${CLASSPATH}"\" jalview.bin.Launcher "${ARGS[@]}"
+fi
+
 "${JAVA}" "${JVMARGS[@]}" -cp "${CLASSPATH}" jalview.bin.Launcher "${ARGS[@]}"