Merge branch 'releases/Release_2_11_3_Branch'
[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 ARG1=$1
30
31 # this function is because there's no readlink -f in Darwin/macOS
32 function readlinkf() {
33   FINDFILE="$1"
34   FILE="${FINDFILE}"
35   PREVFILE=""
36   C=0
37   MAX=100 # just in case we end up in a loop
38   FOUND=0
39   while [ "${C}" -lt "${MAX}" -a "${FILE}" != "${PREVFILE}" -a "${FOUND}" -ne 1 ]; do
40     PREVFILE="${FILE}"
41     FILE="$(readlink "${FILE}")"
42     if [ -z "${FILE}" ]; then
43       # the readlink is empty means we've arrived at the script, let's canonicalize with pwd
44       FILE="$(cd "$(dirname "${PREVFILE}")" &> /dev/null && pwd -P)"/"$(basename "${PREVFILE}")"
45       FOUND=1
46     elif [ "${FILE#/}" = "${FILE}" ]; then
47       # FILE is not an absolute path link, we need to add the relative path to the previous dir
48       FILE="$(dirname "${PREVFILE}")/${FILE}"
49     fi
50     C=$((C+1))
51   done
52   if [ "${FOUND}" -ne 1 ]; then
53     echo "Could not determine path to actual file '$(basename "${FINDFILE}")'" >&2
54     exit 1
55   fi
56   echo "${FILE}"
57 }
58
59 ISMACOS=0
60 if [ "$( uname -s )" = "Darwin" ]; then
61   ISMACOS=1
62 fi
63
64 # check for headless mode
65 HEADLESS=0
66 GUI=0
67 HELP=0
68 DEBUG=0
69 for RAWARG in "${@}"; do
70   ARG="${RAWARG%%=*}"
71   case "${ARG}" in
72     --headless|--output|--image|--structureimage)
73       HEADLESS=1
74       ;;
75     --help|--help-*|--version)
76       HELP=1
77       ;;
78     --gui)
79       GUI=1
80       ;;
81     --debug)
82       DEBUG=1
83       ;;
84   esac
85   
86   if [ "${HELP}" = 1 ]; then
87     # --help takes precedence
88     HEADLESS=1
89     GUI=0
90   elif [ "${GUI}" = 1 ]; then
91     # --gui takes precedence over --headless
92     HEADLESS=0
93   fi
94 done
95
96 declare -a JVMARGS=()
97
98 # set vars for being inside the macos App Bundle
99 if [ "${ISMACOS}" = 1 ]; then
100 # MACOS ONLY
101   DIR="$(dirname "$(readlinkf "$0")")"
102   JVMARGS=( "${JVMARGS[@]}" "-Xdock:icon=${DIR}/jalview_logo.png" )
103 else
104 # NOT MACOS
105   DIR="$(dirname "$(readlink -f "$0")")"
106 fi
107
108 if [ "${HEADLESS}" = 1 ]; then
109   # this suppresses the Java icon appearing in the macOS Dock and maybe other things in other OSes
110   JVMARGS=( "${JVMARGS[@]}" "-Djava.awt.headless=true" )
111 fi
112
113 JAVA=java
114
115 # decide which jalview jar to launch - either 'j11' or 'j1.8'
116 if [[ "$JALVIEW_JRE" != "j11" && "$JALVIEW_JRE" != "j1.8" ]]; then
117   JALVIEW_JRE="j11"
118   # if java 8 is installed we pick the j1.8 build
119   if [[ $( "${JAVA}" -version 2>&1 | grep '"1.8' ) != "" ]]; then
120     JALVIEW_JRE="j1.8"
121   fi
122 fi
123
124 JARPATH="${DIR}/jalview-all-${JALVIEW_JRE}.jar"
125
126 # check if memory maximum is set and if so forward to java-based jalview call
127 if [ \! -z "$JALVIEW_MAXMEM" ]; then
128   JVMARGS=( "${JVMARGS[@]}" "-Xmx${JALVIEW_MAXMEM}" )
129 fi
130
131 # WINDOWS ONLY (Cygwin or WSL)
132 # change paths for Cygwin or Windows Subsystem for Linux (WSL)
133 if [ "${ISMACOS}" != 1 ]; then # older macos doesn't like uname -o, best to avoid
134   if [ "$(uname -o)" = "Cygwin" ]; then
135   # CYGWIN
136     JARPATH="$(cygpath -pw "${JARPATH}")"
137     # now for some arg paths fun. only translating paths starting with './', '../', '/' or '~'
138     ARGS=()
139     for ARG in "${@}"; do
140       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
141         ARGS=( "${ARGS[@]}" "$(cygpath -aw "${ARG}")" )
142       else
143         ARGS=( "${ARGS[@]}" "${ARG}" )
144       fi
145     done
146   elif uname -r | grep -i microsoft | grep -i wsl >/dev/null; then
147   # WSL
148     JARPATH="$(wslpath -aw "${JARPATH}")"
149     ARGS=()
150     for ARG in "${@}"; do
151       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
152         # annoyingly wslpath does not work if the file doesn't exist!
153         ARGBASENAME="$(basename "${ARG}")"
154         ARGDIRNAME="$(dirname "${ARG}")"
155         ARGS=( "${ARGS[@]}" "$(wslpath -aw "${ARGDIRNAME}")\\${ARGBASENAME}" )
156       else
157         ARGS=( "${ARGS[@]}" "${ARG}" )
158       fi
159     done
160     JAVA="${JAVA}.exe"
161   fi
162 fi
163
164 # get console width -- three ways to try, just in case
165 if command -v tput 2>&1 >/dev/null; then
166   COLUMNS=$(tput cols) 2>/dev/null
167 elif command -v stty 2>&1 >/dev/null; then
168   COLUMNS=$(stty size | cut -d" " -f2) 2>/dev/null
169 elif command -v resize 2>&1 >/dev/null; then
170   COLUMNS=$(resize -u | grep COLUMNS= | sed -e 's/.*=//;s/;//') 2>/dev/null
171 fi
172 JVMARGS=( "${JVMARGS[@]}" "-DCONSOLEWIDTH=${COLUMNS}" )
173
174 if [ "${DEBUG}" = 1 ]; then
175  echo Shell running: "${JAVA}" "${JVMARGS[@]}" -jar \""${JARPATH}"\" "${ARGS[@]}"
176 fi
177
178 "${JAVA}" "${JVMARGS[@]}" -jar "${JARPATH}" "${ARGS[@]}"