#!/bin/bash

SCRIPT_VER="1.2"

APP="Oracle Java JRE 8"
APP_CATEGORY="Utilities"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="oracle-java8-jre"
SUPPORTED_ON="Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
LIBRARY="functions4.sh"

if [ "${SUPPORT-}" = '' ]; then
    SUPPORT="https://installers.rapidseedbox.com/scripts/whmcs_client_installers/support"
fi

LOGFILE="/var/log/whmcs_client_installers/${0}.log"
[ ! -d "/var/log/whmcs_client_installers" ] && mkdir -p "/var/log/whmcs_client_installers"

wget -4 -nc -q --no-check-certificate --timeout=20 --tries=5 ${SUPPORT}/${LIBRARY} >> "${LOGFILE}" 2>&1
if [ ! -f "./${LIBRARY}" ] ; then
    echo "55"
    echo "Error downloading \"${SUPPORT}/${LIBRARY}\"" >> "${LOGFILE}"
    exit 55
fi

# shellcheck source=support/functions4.sh
. "./${LIBRARY}"
SCRIPTENTRY


while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] ; then
        main_rc=${_rc}
        break
    fi

    #
    # JDK download links:
    #
    # https://gist.github.com/hgomez/9650687
    # https://gist.github.com/wavezhang/ba8425f24a968ec9b2a8619d7c2d86a6
    #
    # Build instructions:
    #
    # https://meandubuntu.ru/2019/06/oracle-java8-jdk/
    #
    # -----------------------------------------
    # wget --no-cookies --no-check-certificate \
    #      --header "Cookie: oraclelicense=accept-securebackup-cookie" \
    #       https://javadl.oracle.com/webapps/download/GetFile/1.8.0_281-b09/89d678f2be164786b292527658ca1605/linux-i586/jdk-8u281-linux-x64.tar.gz
    #

#    DEB_FILE="oracle-java8-jre_8u231_amd64.deb"
    DEB_FILE="oracle-java8-jre_8u281_amd64.deb"

    WGET "http://ansible.rapidseedbox.com/packages/${DEB_FILE}" || break

    APT_INSTALL "java-common" || break

    DPKG_INSTALL "${TMP_DIR}/${DEB_FILE}" || break

    RUN_LOG "update-alternatives --set java /usr/lib/jvm/oracle-java8-jre-amd64/bin/java"
    RUN_LOG "update-alternatives --set javaws /usr/lib/jvm/oracle-java8-jre-amd64/bin/javaws"



    main_rc=0
    break

done


SCRIPTEXIT
