JAL-4344 careful application of -Djava.awt.headless=true to avoid Jmol problem. Using...
[jalview.git] / utils / conda / jalview.sh
1 #!/usr/bin/env bash
2
3 ###############################
4 # Wrapper for Jalview
5 #
6 # 2023-08-16 Jalview 2.11.3.0 has new command line arguments
7 # Old command line arguments are currently detected and actioned
8 # but are no longer supported and will be removed at a later date.
9 #
10 # See
11 #   Jalview -> Help -> Documentation -> Command Line -> introduction and reference
12 # or
13 #   https://www.jalview.org/help/html/features/clarguments.html
14 # for details of the new command line arguments.
15 #
16 # Note, in order to run commandline-only calls use
17 #   --headless
18 #
19 # By default, this wrapper executes java -version to determine the JRE version
20 # Set JALVIEW_JRE=j1.8 or JALVIEW_JRE=j11 to skip the version check.
21 #
22 # By default, this wrapper does NOT restrict the memory consumption of Jalview.
23 # Set eg. JALVIEW_MAXMEM=1g to set the maximal memory of Jalview's VM
24 #
25 # This script is maintained in the Jalview repository in utils/conda/jalview.sh
26 ###############################
27
28 declare -a ARGS=("${@}")
29
30 # this function is because there's no readlink -f in Darwin/macOS
31 function readlinkf() {
32   FINDFILE="$1"
33   FILE="${FINDFILE}"
34   PREVFILE=""
35   C=0
36   MAX=100 # just in case we end up in a loop
37   FOUND=0
38   while [ "${C}" -lt "${MAX}" -a "${FILE}" != "${PREVFILE}" -a "${FOUND}" -ne 1 ]; do
39     PREVFILE="${FILE}"
40     FILE="$(readlink "${FILE}")"
41     if [ -z "${FILE}" ]; then
42       # the readlink is empty means we've arrived at the script, let's canonicalize with pwd
43       FILE="$(cd "$(dirname "${PREVFILE}")" &> /dev/null && pwd -P)"/"$(basename "${PREVFILE}")"
44       FOUND=1
45     elif [ "${FILE#/}" = "${FILE}" ]; then
46       # FILE is not an absolute path link, we need to add the relative path to the previous dir
47       FILE="$(dirname "${PREVFILE}")/${FILE}"
48     fi
49     C=$((C+1))
50   done
51   if [ "${FOUND}" -ne 1 ]; then
52     echo "Could not determine path to actual file '$(basename "${FINDFILE}")'" >&2
53     exit 1
54   fi
55   echo "${FILE}"
56 }
57
58 ISMACOS=0
59 if [ "$( uname -s )" = "Darwin" ]; then
60   ISMACOS=1
61 fi
62
63 # check for headless mode
64 HEADLESS=0
65 GUI=0
66 HELP=0
67 DEBUG=0
68 for RAWARG in "${@}"; do
69   ARG="${RAWARG%%=*}"
70   case "${ARG}" in
71     --headless|--output|--image|--structureimage)
72       HEADLESS=1
73       ;;
74     --help|--help-*|--version|-h)
75       HELP=1
76       ;;
77     --gui)
78       GUI=1
79       ;;
80     --debug)
81       DEBUG=1
82       ;;
83   esac
84   
85   if [ "${HELP}" = 1 ]; then
86     # --help takes precedence
87     HEADLESS=1
88     GUI=0
89   elif [ "${GUI}" = 1 ]; then
90     # --gui takes precedence over --headless
91     HEADLESS=0
92   fi
93 done
94
95 declare -a JVMARGS=()
96
97 # set vars for being inside the macos App Bundle
98 if [ "${ISMACOS}" = 1 ]; then
99 # MACOS ONLY
100   DIR="$(dirname "$(readlinkf "$0")")"
101   if [ -e "${DIR}/jalview_logo.png" ]; then
102     JVMARGS=( "${JVMARGS[@]}" "-Xdock:icon=${DIR}/jalview_logo.png" )
103   fi
104 else
105 # NOT MACOS
106   DIR="$(dirname "$(readlink -f "$0")")"
107 fi
108
109 if [ "${HEADLESS}" = 1 ]; then
110   # not setting java.awt.headless in java invocation of running jalview due to problem with Jmol
111   if [ "${HELP}" = 1 ]; then
112     JVMARGS=( "${JVMARGS[@]}" "-Djava.awt.headless=true" )
113   fi
114   # this suppresses the Java icon appearing in the macOS Dock
115   if [ "${ISMACOS}" = 1 ]; then
116     JVMARGS=( "${JVMARGS[@]}" "-Dapple.awt.UIElement=true" )
117   fi
118 fi
119
120 JAVA=java
121
122 # decide which jalview jar to launch - either 'j11' or 'j1.8'
123 if [[ "$JALVIEW_JRE" != "j11" && "$JALVIEW_JRE" != "j1.8" ]]; then
124   JALVIEW_JRE="j11"
125   # if java 8 is installed we pick the j1.8 build
126   if [[ $( "${JAVA}" -version 2>&1 | grep '"1.8' ) != "" ]]; then
127     JALVIEW_JRE="j1.8"
128   fi
129 fi
130
131 JARPATH="${DIR}/jalview-all-${JALVIEW_JRE}.jar"
132
133 # check if memory maximum is set and if so forward to java-based jalview call
134 if [ \! -z "$JALVIEW_MAXMEM" ]; then
135   JVMARGS=( "${JVMARGS[@]}" "-Xmx${JALVIEW_MAXMEM}" )
136 fi
137
138 # WINDOWS ONLY (Cygwin or WSL)
139 # change paths for Cygwin or Windows Subsystem for Linux (WSL)
140 if [ "${ISMACOS}" != 1 ]; then # older macos doesn't like uname -o, best to avoid
141   if [ "$(uname -o)" = "Cygwin" ]; then
142   # CYGWIN
143     JARPATH="$(cygpath -pw "${JARPATH}")"
144     # now for some arg paths fun. only translating paths starting with './', '../', '/' or '~'
145     ARGS=()
146     for ARG in "${@}"; do
147       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
148         ARGS=( "${ARGS[@]}" "$(cygpath -aw "${ARG}")" )
149       else
150         ARGS=( "${ARGS[@]}" "${ARG}" )
151       fi
152     done
153   elif uname -r | grep -i microsoft | grep -i wsl >/dev/null; then
154   # WSL
155     JARPATH="$(wslpath -aw "${JARPATH}")"
156     ARGS=()
157     for ARG in "${@}"; do
158       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
159         # annoyingly wslpath does not work if the file doesn't exist!
160         ARGBASENAME="$(basename "${ARG}")"
161         ARGDIRNAME="$(dirname "${ARG}")"
162         ARGS=( "${ARGS[@]}" "$(wslpath -aw "${ARGDIRNAME}")\\${ARGBASENAME}" )
163       else
164         ARGS=( "${ARGS[@]}" "${ARG}" )
165       fi
166     done
167     JAVA="${JAVA}.exe"
168   fi
169 fi
170
171 # get console width -- three ways to try, just in case
172 if command -v tput 2>&1 >/dev/null; then
173   COLUMNS=$(tput cols) 2>/dev/null
174 elif command -v stty 2>&1 >/dev/null; then
175   COLUMNS=$(stty size | cut -d" " -f2) 2>/dev/null
176 elif command -v resize 2>&1 >/dev/null; then
177   COLUMNS=$(resize -u | grep COLUMNS= | sed -e 's/.*=//;s/;//') 2>/dev/null
178 fi
179 JVMARGS=( "${JVMARGS[@]}" "-DCONSOLEWIDTH=${COLUMNS}" )
180
181 function quotearray() {
182   QUOTEDVALS=""
183   for VAL in "${@}"; do
184     if [ \! "$QUOTEDVALS" = "" ]; then
185       QUOTEDVALS="${QUOTEDVALS} "
186     fi
187     QUOTEDVALS="${QUOTEDVALS}\"${VAL}\""
188   done
189   echo $QUOTEDVALS
190 }
191
192 JVMARGSSTR=$(quotearray "${JVMARGS[@]}")
193 ARGSSTR=$(quotearray "${ARGS[@]}")
194
195 if [ "${DEBUG}" = 1 ]; then
196  echo Shell running: \""${JAVA}"\" ${JVMARGSSTR} -jar \""${JARPATH}"\" ${ARGSSTR}
197 fi
198
199 "${JAVA}" "${JVMARGS[@]}" -jar "${JARPATH}" "${ARGS[@]}"