JAL-3830 Delete wrapper scripts and getdown *v files when uninstalling
[jalview.git] / utils / getdown / jalviewc-macos
1 #!/usr/bin/env bash
2
3 ARG1="$1"
4
5 # this whole next part is because there's no readlink -f in Darwin
6 function readlinkf() {
7   FINDFILE="$1"
8   FILE="${FINDFILE}"
9   PREVFILE=""
10   C=0
11   MAX=100 # just in case we end up in a loop
12   FOUND=0
13   while [ "${C}" -lt "${MAX}" -a "${FILE}" != "${PREVFILE}" -a "${FOUND}" -ne 1 ]; do
14     PREVFILE="${FILE}"
15     FILE="$(readlink "${FILE}")"
16     if [ -z "${FILE}" ]; then
17       # the readlink is empty means we've arrived at the script, let's canonicalize with pwd
18       FILE="$(cd "$(dirname "${PREVFILE}")" &> /dev/null && pwd -P)"/"$(basename "${PREVFILE}")"
19       FOUND=1
20     elif [ "${FILE#/}" = "${FILE}" ]; then
21       # FILE is not an absolute path link, we need to add the relative path to the previous dir
22       FILE="$(dirname "${PREVFILE}")/${FILE}"
23     fi
24     C=$((C+1))
25   done
26   if [ "${FOUND}" -ne 1 ]; then
27     echo "Could not determine path to actual file '$(basename "${FINDFILE}")'" >&2
28     exit 1
29   fi
30   echo "${FILE}"
31 }
32
33 DIR="$(dirname "$(readlinkf "$0")")"
34 APP="${DIR%.app/Contents/*}".app
35
36 if [ "${APP}" = "${APP%.app}" ]; then
37   echo "Could not find Jalview.app dir" >&2
38   exit 2
39 fi
40
41 # check to see if $1 is set and is not start of other cli set args
42 OPEN=""
43 if [ -n "${ARG1}" -a "${ARG1}" = "${ARG1#-}" -a "${ARG1}" != "open" ]; then
44  # first argument exists and does not start with a "-" and is not "open"
45  OPEN="-open"
46 fi
47
48 APPDIR="${APP}/Contents/Resources/app"
49 JAVA="${APPDIR}/jre/Contents/Home/bin/java"
50 GETDOWNTXT="${APPDIR}/getdown.txt"
51 CLASSPATH=""
52 if [ -e "${GETDOWNTXT}" ]; then
53   # always check grep and sed regexes on macOS -- they're not the same
54   for JAR in $(grep -e '^code\s*=\s*' "${GETDOWNTXT}" | sed -e 's/^code\s*=\s*//;'); do
55     [ -n "${CLASSPATH}" ] && CLASSPATH="${CLASSPATH}:"
56     CLASSPATH="${CLASSPATH}${APPDIR}/${JAR}"
57   done
58 else
59   echo "Cannot find getdown.txt" >&2
60   exit 3
61 fi
62
63 if [ \! -e "${JAVA}" ]; then
64   echo "Cannot find bundled java, using system and hoping for the best!" >&2
65   JAVA=java
66 fi
67
68 # don't quote $OPEN
69 "${JAVA}" -Xdock:icon="${APPDIR}"/resource/jalview_logo.png -cp "${CLASSPATH}" jalview.bin.Launcher ${OPEN} "$@"