JAL-3394 A little streamlining and fixing of helper script
[jalview.git] / utils / download_jres.sh
1 #!/usr/bin/env bash
2
3 # see https://api.adoptopenjdk.net/swagger-ui/#/Binary/get_v3_binary_latest__feature_version___release_type___os___arch___image_type___jvm_impl___heap_size___vendor_
4
5
6 ### bs 2020-01-22
7 ### This is a script to download and update the JREs used in the windows, mac (and maybe linux) installations, and update channel
8 ### It creates a structure with
9 ### ./jre-VERSION-OS-ARCH/jre/...
10 ### as used by getdown
11 ### and
12 ### ./tgz/jre-VERSION-OS-ARCH.tgz
13 ### which is an archive of the _contents_ of ./jre-VERSION-OS-ARCH/jre/ and used by install4j for the installer
14
15 BASE=https://api.adoptopenjdk.net/v3/binary/latest
16 RELEASE_TYPE=ga
17 JVM_IMPL=hotspot
18 HEAP_SIZE=normal
19 VENDOR=adoptopenjdk
20 IMAGE_TYPE=jre
21
22 STRIP_MAC_APP_BUNDLING=false
23
24 RM=/bin/rm
25
26 # unzip-strip from https://superuser.com/questions/518347/equivalent-to-tars-strip-components-1-in-unzip
27 unzip-strip() (
28   local zip=$1
29   local dest=${2:-.}
30   local temp=$(mktemp -d) && unzip -qq -d "$temp" "$zip" && mkdir -p "$dest" &&
31   shopt -s dotglob && local f=("$temp"/*) &&
32   if (( ${#f[@]} == 1 )) && [[ -d "${f[0]}" ]] ; then
33     mv "$temp"/*/* "$dest"
34   else
35     mv "$temp"/* "$dest"
36   fi && rmdir "$temp"/* "$temp"
37 )
38
39 for FEATURE_VERSION in 8 11; do
40   for OS in linux mac windows; do
41     for ARCH in x64 x32 arm; do
42       #for ARCH in aarch64 arm x32 x64; do
43       NAME="${IMAGE_TYPE}-${FEATURE_VERSION}-${OS}-${ARCH}"
44       TARFILE="${NAME}.tgz"
45       echo "* Downloading ${TARFILE}"
46       URL="${BASE}/${FEATURE_VERSION}/${RELEASE_TYPE}/${OS}/${ARCH}/${IMAGE_TYPE}/${JVM_IMPL}/${HEAP_SIZE}/${VENDOR}"
47       wget -q -O "${TARFILE}" "${URL}"
48       if [ "$?" != 0 ]; then
49         echo "- No ${IMAGE_TYPE}-${FEATURE_VERSION} download for ${OS}-${ARCH} '${URL}'"
50         $RM -f "${TARFILE}"
51         continue;
52       fi
53       echo "Unpacking ${TARFILE}"
54       JREDIR="${NAME}/${IMAGE_TYPE}"
55       [ x$NAME != x -a -e "${JREDIR}" ] && $RM -rf "${JREDIR}"
56       mkdir -p "${JREDIR}"
57       if [ x$OS = xwindows ]; then
58         echo "using unzip"
59         unzip-strip "${TARFILE}" "${JREDIR}"
60         RET=$?
61       else
62         echo "using tar"
63         if [ x$OS = xmac -a x$STRIP_MAC_APP_BUNDLING = xtrue ]; then
64           tar --strip-components=3 -C "${JREDIR}" -zxf "${TARFILE}" "*/Contents/Home"
65           RET=$?
66         else
67           tar --strip-components=1 -C "${JREDIR}" -zxf "${TARFILE}"
68           RET=$?
69         fi
70       fi
71       if [ "$RET" != 0 ]; then
72         echo "Error unpacking ${TARFILE}"
73         exit 1
74       fi
75       $RM "${TARFILE}"
76       echo "Creating .tar.gz for install4j bundling and updates"
77       TGZDIR=tgz
78       mkdir -p "${TGZDIR}"
79       tar -C "${JREDIR}" -zcf "${TGZDIR}/${NAME}.tar.gz" .
80     done
81   done
82 done
83