#!/usr/bin/env bash

# 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_


### bs 2020-01-22
### This is a script to download and update the JREs used in the windows, mac (and maybe linux) installations, and update channel
### It creates a structure with
### ./jre-VERSION-OS-ARCH/jre/...
### as used by getdown
### and
### ./tgz/jre-VERSION-OS-ARCH.tgz
### which is an archive of the _contents_ of ./jre-VERSION-OS-ARCH/jre/ and used by install4j for the installer

BASE=https://api.adoptopenjdk.net/v3/binary/latest
RELEASE_TYPE=ga
JVM_IMPL=hotspot
HEAP_SIZE=normal
VENDOR=adoptopenjdk
IMAGE_TYPE=jdk

RM=/bin/rm

# unzip-strip from https://superuser.com/questions/518347/equivalent-to-tars-strip-components-1-in-unzip
unzip-strip() (
  local zip=$1
  local dest=${2:-.}
  local temp=$(mktemp -d) && unzip -qq -d "$temp" "$zip" && mkdir -p "$dest" &&
  shopt -s dotglob && local f=("$temp"/*) &&
  if (( ${#f[@]} == 1 )) && [[ -d "${f[0]}" ]] ; then
    mv "$temp"/*/* "$dest"
  else
    mv "$temp"/* "$dest"
  fi && rmdir "$temp"/* "$temp"
)

for FEATURE_VERSION in 8 11 12 13; do
  for OS in mac; do
  #for OS in linux mac windows; do
    for ARCH in x64; do
    #for ARCH in aarch64 arm x32 x64; do
      NAME="${IMAGE_TYPE}-${FEATURE_VERSION}-${OS}-${ARCH}"
      TARFILE="${NAME}.tgz"
      echo "* Downloading ${TARFILE}"
      URL="${BASE}/${FEATURE_VERSION}/${RELEASE_TYPE}/${OS}/${ARCH}/${IMAGE_TYPE}/${JVM_IMPL}/${HEAP_SIZE}/${VENDOR}"
      wget -q -O "${TARFILE}" "${URL}"
      if [ "$?" != 0 ]; then
        echo "- No ${IMAGE_TYPE}-${FEATURE_VERSION} download for ${OS}-${ARCH} '${URL}'"
        $RM -f "${TARFILE}"
        continue;
      fi
      echo "Unpacking ${TARFILE}"
      JREDIR="${NAME}/${IMAGE_TYPE}"
      [ x$NAME != x -a -e "${JREDIR}" ] && $RM -rf "${JREDIR}"
      mkdir -p "${JREDIR}"
      if [ x$OS = xwindows ]; then
        echo "using unzip"
        unzip-strip "${TARFILE}" "${JREDIR}"
        RET=$?
      else
        echo "using tar"
        tar --strip-components=1 -C "${JREDIR}" -zxf "${TARFILE}"
        RET=$?
      fi
      if [ "$RET" != 0 ]; then
        echo "Error unpacking ${TARFILE}"
        exit 1
      fi
      $RM "${TARFILE}"
    done
  done
done

