112c3e64f2ad7e94974ca076d64775a97b8dc704
[jalview.git] / utils / getdown / bin / jalview.sh
1 #!/usr/bin/env bash
2
3 declare -a ARGS=("${@}")
4 ARG1=$1
5
6 # this whole next part is because there's no readlink -f in Darwin
7 function readlinkf() {
8   FINDFILE="$1"
9   FILE="${FINDFILE}"
10   PREVFILE=""
11   C=0
12   MAX=100 # just in case we end up in a loop
13   FOUND=0
14   while [ "${C}" -lt "${MAX}" -a "${FILE}" != "${PREVFILE}" -a "${FOUND}" -ne 1 ]; do
15     PREVFILE="${FILE}"
16     FILE="$(readlink "${FILE}")"
17     if [ -z "${FILE}" ]; then
18       # the readlink is empty means we've arrived at the script, let's canonicalize with pwd
19       FILE="$(cd "$(dirname "${PREVFILE}")" &> /dev/null && pwd -P)"/"$(basename "${PREVFILE}")"
20       FOUND=1
21     elif [ "${FILE#/}" = "${FILE}" ]; then
22       # FILE is not an absolute path link, we need to add the relative path to the previous dir
23       FILE="$(dirname "${PREVFILE}")/${FILE}"
24     fi
25     C=$((C+1))
26   done
27   if [ "${FOUND}" -ne 1 ]; then
28     echo "Could not determine path to actual file '$(basename "${FINDFILE}")'" >&2
29     exit 1
30   fi
31   echo "${FILE}"
32 }
33
34 ISMACOS=0
35 if [ "$( uname -s )" = "Darwin" ]; then
36   ISMACOS=1
37 fi
38
39 declare -a JVMARGS=()
40
41 # set vars for being inside the macos App Bundle
42 if [ "${ISMACOS}" = 1 ]; then
43 # MACOS ONLY
44   DIR="$(dirname "$(readlinkf "$0")")"
45   APP="${DIR%.app/Contents/*}".app
46   if [ "${APP}" = "${APP%.app}" ]; then
47     echo "Could not find Jalview.app" >&2
48     exit 2
49   fi
50   APPDIR="${APP}/Contents/Resources/app"
51   JAVA="${APPDIR}/jre/Contents/Home/bin/java"
52   JVMARGS=( "${JVMARGS[@]}" "-Xdock:icon=${APPDIR}/resource/jalview_logo.png" )
53 else
54 # NOT MACOS
55   DIR="$(dirname "$(readlink -f "$0")")"
56   APPDIR="${DIR%/bin}"
57   JAVA="${APPDIR}/jre/bin/java"
58 fi
59
60 SYSJAVA=java
61 GETDOWNTXT="${APPDIR}/getdown.txt"
62
63 CLASSPATH=""
64 # save an array of JAR paths in case we're in WSL (see later)
65 declare -a JARPATHS=()
66 if [ -e "${GETDOWNTXT}" ]; then
67   # always check grep and sed regexes on macos -- they're not the same
68   for JAR in $(grep -e '^code\s*=\s*' "${GETDOWNTXT}" | sed -e 's/^code\s*=\s*//;'); do
69     [ -n "${CLASSPATH}" ] && CLASSPATH="${CLASSPATH}:"
70     CLASSPATH="${CLASSPATH}${APPDIR}/${JAR}"
71     JARPATHS=( "${JARPATHS[@]}" "${APPDIR}/${JAR}" )
72   done
73 else
74   echo "Cannot find getdown.txt" >&2
75   exit 3
76 fi
77
78 # WINDOWS ONLY (Cygwin or WSL)
79 # change paths for Cygwin or Windows Subsystem for Linux (WSL)
80 if [ "${ISMACOS}" != 1 ]; then # macos doesn't like uname -o, best to avoid
81   if [ "$(uname -o)" = "Cygwin" ]; then
82   # CYGWIN
83     echo "When using relative paths in args within Cygwin, please start with './' or '../'" >&2
84     CLASSPATH=$(cygpath -pw "${CLASSPATH}")
85     # now for some arg paths fun. only translating paths starting with './', '../', '/' or '~'
86     ARGS=()
87     for ARG in "${@}"; do
88       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
89         ARGS=( "${ARGS[@]}" "$(cygpath -aw "${ARG}")" )
90       else
91         ARGS=( "${ARGS[@]}" "${ARG}" )
92       fi
93     done
94   elif uname -r | grep Microsoft >/dev/null; then
95   # WSL
96     echo "When using relative paths in args within WSL, please start with './' or '../'" >&2
97     CLASSPATH=""
98     for JARPATH in "${JARPATHS[@]}"; do
99       [ -n "${CLASSPATH}" ] && CLASSPATH="${CLASSPATH};"
100       CLASSPATH="${CLASSPATH}$(wslpath -aw "${JARPATH}")"
101     done
102     ARGS=()
103     for ARG in "${@}"; do
104       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
105         # annoyingly wslpath does not work if the file doesn't exist!
106         ARGBASENAME="$(basename "${ARG}")"
107         ARGDIRNAME="$(dirname "${ARG}")"
108         ARGS=( "${ARGS[@]}" "$(wslpath -aw "${ARGDIRNAME}")\\${ARGBASENAME}" )
109       else
110         ARGS=( "${ARGS[@]}" "${ARG}" )
111       fi
112     done
113     JAVA="${JAVA}.exe"
114     SYSJAVA="java.exe"
115   fi
116 fi
117
118 # Is there a bundled Java?  If not just try one in the PATH (do need .exe in WSL)
119 if [ \! -e "${JAVA}" ]; then
120   JAVA=$SYSJAVA
121   echo "Cannot find bundled java, using system ${JAVA} and hoping for the best!" >&2
122 fi
123
124 # check to see if $1 is set and is not start of other cli args
125 OPEN=""
126 if [ -n "${ARG1}" -a "${ARG1}" = "${ARG1#-}" -a "${ARG1}" != "open" ]; then
127  # first argument exists and does not start with a "-" and is not "open"
128  OPEN="-open"
129 fi
130
131 # don't quote $OPEN (don't want it accidentally mistaken as an empty string arg!)
132 "${JAVA}" "${JVMARGS[@]}" -cp "${CLASSPATH}" jalview.bin.Launcher ${OPEN} "${ARGS[@]}"