JAL-3830 JAL-3421 Conda launch script for 2.11.3 and new CLI with OS compatibility...
[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 ARG in "${@}"; do
70   case "${ARG}" in
71     "--headless")
72       HEADLESS=1
73       ;;
74     "--help")
75       HELP=1
76       ;;
77     --help-*)
78       HELP=1
79       ;;
80     "--gui")
81       GUI=1
82       ;;
83     "--debug")
84       DEBUG=1
85       ;;
86   esac
87   
88   if [ "${HELP}" = 1 ]; then
89     # --help takes precedence
90     HEADLESS=1
91     GUI=0
92   elif [ "${GUI}" = 1 ]; then
93     # --gui takes precedence over --headless
94     HEADLESS=0
95   fi
96 done
97
98 declare -a JVMARGS=()
99
100 # set vars for being inside the macos App Bundle
101 if [ "${ISMACOS}" = 1 ]; then
102 # MACOS ONLY
103   DIR="$(dirname "$(readlinkf "$0")")"
104   JVMARGS=( "${JVMARGS[@]}" "-Xdock:icon=${DIR}/jalview_logo.png" )
105 else
106 # NOT MACOS
107   DIR="$(dirname "$(readlink -f "$0")")"
108 fi
109
110 if [ "${HEADLESS}" = 1 ]; then
111   # this suppresses the Java icon appearing in the macOS Dock and maybe other things in other OSes
112   JVMARGS=( "${JVMARGS[@]}" "-Djava.awt.headless=true" )
113 fi
114
115 JAVA=java
116
117 # decide which jalview jar to launch - either 'j11' or 'j1.8'
118 if [[ "$JALVIEW_JRE" != "j11" && "$JALVIEW_JRE" != "j1.8" ]]; then
119   JALVIEW_JRE="j11"
120   # if java 8 is installed we pick the j1.8 build
121   if [[ $( "${JAVA}" -version 2>&1 | grep '"1.8' ) != "" ]]; then
122     JALVIEW_JRE="j1.8"
123   fi
124 fi
125
126 JARPATH="${DIR}/jalview-all-${JALVIEW_JRE}.jar"
127
128 # check if memory maximum is set and if so forward to java-based jalview call
129 if [ \! -z "$JALVIEW_MAXMEM" ]; then
130   JVMARGS=( "${JVMARGS[@]}" "-Xmx${JALVIEW_MAXMEM}" )
131 fi
132
133 # WINDOWS ONLY (Cygwin or WSL)
134 # change paths for Cygwin or Windows Subsystem for Linux (WSL)
135 if [ "${ISMACOS}" != 1 ]; then # older macos doesn't like uname -o, best to avoid
136   if [ "$(uname -o)" = "Cygwin" ]; then
137   # CYGWIN
138     JARPATH="$(cygpath -pw "${JARPATH}")"
139     # now for some arg paths fun. only translating paths starting with './', '../', '/' or '~'
140     ARGS=()
141     for ARG in "${@}"; do
142       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
143         ARGS=( "${ARGS[@]}" "$(cygpath -aw "${ARG}")" )
144       else
145         ARGS=( "${ARGS[@]}" "${ARG}" )
146       fi
147     done
148   elif uname -r | grep -i microsoft | grep -i wsl >/dev/null; then
149   # WSL
150     JARPATH="$(wslpath -aw "${JARPATH}")"
151     ARGS=()
152     for ARG in "${@}"; do
153       if [ "${ARG}" != "${ARG#@(/|./|../|~)}" ]; then
154         # annoyingly wslpath does not work if the file doesn't exist!
155         ARGBASENAME="$(basename "${ARG}")"
156         ARGDIRNAME="$(dirname "${ARG}")"
157         ARGS=( "${ARGS[@]}" "$(wslpath -aw "${ARGDIRNAME}")\\${ARGBASENAME}" )
158       else
159         ARGS=( "${ARGS[@]}" "${ARG}" )
160       fi
161     done
162     JAVA="${JAVA}.exe"
163   fi
164 fi
165
166 # get console width -- three ways to try, just in case
167 if command -v tput 2>&1 >/dev/null; then
168   COLUMNS=$(tput cols) 2>/dev/null
169 elif command -v stty 2>&1 >/dev/null; then
170   COLUMNS=$(stty size | cut -d" " -f2) 2>/dev/null
171 elif command -v resize 2>&1 >/dev/null; then
172   COLUMNS=$(resize -u | grep COLUMNS= | sed -e 's/.*=//;s/;//') 2>/dev/null
173 fi
174 JVMARGS=( "${JVMARGS[@]}" "-DCONSOLEWIDTH=${COLUMNS}" )
175
176 if [ "${DEBUG}" = 1 ]; then
177  echo Shell running: "${JAVA}" "${JVMARGS[@]}" -jar \""${JARPATH}"\" jalview.bin.Launcher "${ARGS[@]}"
178 fi
179
180 "${JAVA}" "${JVMARGS[@]}" -jar "${JARPATH}" jalview.bin.Launcher "${ARGS[@]}"