JAL-3830 bash script that works in both cygwin and WSL (Windows Subsystem for Linux)
authorBen Soares <b.soares@dundee.ac.uk>
Wed, 17 Mar 2021 00:22:00 +0000 (00:22 +0000)
committerBen Soares <b.soares@dundee.ac.uk>
Wed, 17 Mar 2021 00:22:00 +0000 (00:22 +0000)
utils/getdown/jalviewc-windows [new file with mode: 0755]

diff --git a/utils/getdown/jalviewc-windows b/utils/getdown/jalviewc-windows
new file mode 100755 (executable)
index 0000000..2f0f968
--- /dev/null
@@ -0,0 +1,74 @@
+#!/usr/bin/env bash
+
+ARG1=$1
+DIR="$(dirname "$(readlink -f "$0")")"
+
+# check to see if $1 is set and is not start of other cli set args
+OPEN=""
+if [ -n "${ARG1}" -a "${ARG1}" = "${ARG1#-}" -a "${ARG1}" != "open" ]; then
+ # first argument exists and does not start with a "-" and is not "open"
+ OPEN="-open"
+fi
+
+APPDIR="${DIR}"
+JAVA="${APPDIR}/jre/bin/java"
+GETDOWNTXT="${APPDIR}/getdown.txt"
+CLASSPATH=""
+if [ -e "${GETDOWNTXT}" ]; then
+  for JAR in $(grep -e '^code\s*=\s*' "${GETDOWNTXT}" | sed -e 's/^code\s*=\s*//;'); do
+    [ -n "${CLASSPATH}" ] && CLASSPATH="${CLASSPATH}:"
+    CLASSPATH="${CLASSPATH}${APPDIR}/${JAR}"
+  done
+else
+  echo "Cannot find getdown.txt" >&2
+  exit 3
+fi
+
+if [ \! -e "${JAVA}" ]; then
+  echo "Cannot find bundled java, using system and hoping for the best!" >&2
+  JAVA=java
+fi
+
+declare -a ARGS=("${@}")
+
+# change paths for Cygwin or Windows Subsystem for Linux (WSL)
+if [ "$(uname -o)" = "Cygwin" ]; then
+  echo "When using relative paths in args within Cygwin, please start with './' or '../'" >&2
+  CLASSPATH=$(cygpath -pw "${CLASSPATH}")
+  # now for some arg paths fun. only translating paths starting with './', '../' or '/'
+  ARGS=()
+  for ARG in "${@}"; do
+    if [ "${ARG}" != "${ARG#./}" -o "${ARG}" != "${ARG#/}" -o "${ARG}" != "${ARG#../}" ]; then
+      ARGS=( "${ARGS[@]}" "$(cygpath -aw "${ARG}")" )
+    else
+      ARGS=( "${ARGS[@]}" "${ARG}" )
+    fi
+  done
+  JAVA="${JAVA}.exe"
+elif uname -r | grep Microsoft >/dev/null; then
+  echo "When using relative paths in args within WSL. please start with './' or '../'" >&2
+  WCLASSPATH=""
+  OLD_IFS="${IFS}"
+  IFS=":"
+  for JAR in $CLASSPATH; do
+    [ -n "${WCLASSPATH}" ] && WCLASSPATH="${WCLASSPATH};"
+    WCLASSPATH="${WCLASSPATH}$(wslpath -aw "${JAR}")"
+  done
+  CLASSPATH="${WCLASSPATH}"
+  IFS="${OLD_IFS}"
+  ARGS=()
+  for ARG in "${@}"; do
+    if [ "${ARG}" != "${ARG#./}" -o "${ARG}" != "${ARG#/}" -o "${ARG}" != "${ARG#../}" ]; then
+      # annoyingly wslpath does not work if the file doesn't exist!
+      ARGBASENAME="$(basename "${ARG}")"
+      ARGDIRNAME="$(dirname "${ARG}")"
+      ARGS=( "${ARGS[@]}" "$(wslpath -aw "${ARGDIRNAME}")\\${ARGBASENAME}" )
+    else
+      ARGS=( "${ARGS[@]}" "${ARG}" )
+    fi
+  done
+  JAVA="${JAVA}.exe"
+fi
+
+# don't quote $OPEN
+"${JAVA}" -cp "${CLASSPATH}" jalview.bin.Launcher ${OPEN} "${ARGS[@]}"