#!/bin/bash

SCRIPT_VER="1.1"

APP="NZBHydra 2"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="system"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="userdir"
#><#APP_CHECK_VER_CMD="[ -e "/opt/nzbhydra2/version.id" ] && cat "/opt/nzbhydra2/version.id" || echo """
#><#APP_UPGRADE_VER_CMD="curl -Ls -m 30 -o /dev/null -w "%{url_effective}" https://github.com/theotherp/nzbhydra2/releases/latest | awk -F/ "{print \$NF}" | tr -d "v""
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04"
MEMORY_K_MIN="4000000"
USE_APACHE2="yes"
DAEMON_PORT="5076"
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

APP_DIRECTORY="${USERHOME}/.config/nzbhydra"
APP_HOME="/opt/nzbhydra2"

while : ; do

    main_rc=1 

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

    # -- Stop running service instance
    systemctl is-active "nzbhydra2@${USERNAME}" > /dev/null \
        && SVC_STOP "nzbhydra2@${USERNAME}"

    # -- Upgrade NZBHydra 2
    [ -d "${APP_HOME}" ] \
        && RUN_LOG "mv -f '${APP_HOME}' '${APP_HOME}.BAK' 2>&1"
    while true ;  do
        MKDIR "${APP_HOME}" || break
        INFO "Installing ${APP} into ${APP_HOME}"
        _ver="$( curl -Ls -m 30 -o /dev/null -w '%{url_effective}' \
                          https://github.com/theotherp/nzbhydra2/releases/latest \
                     | awk -F '/' '{print $NF}' | tr -d 'v' )"
        if [ -z "${_ver}" ] ; then
            ERROR "Failed to retrieve ${APP} release tarball URL !"
            break
        else
            APP_URL="https://github.com/theotherp/nzbhydra2/releases/download/v${_ver}/nzbhydra2-${_ver}-amd64-linux.zip"
            INFO "${APP} release tarball URL: ${APP_URL}"
        fi
        if [ -z "${APP_URL}" ] ; then
            ERROR "Failed to parse ${APP} release tarball URL !"
            break
        else
            APP_ARCHIVE=$( echo "${APP_URL}" | awk -F "/" '{print $NF}' )
            INFO "${APP} release tarball: ${APP_ARCHIVE}"
        fi
        WGET "${APP_URL}" || break
        UNZIP "${TMP_DIR}/${APP_ARCHIVE}" "${APP_HOME}/" || break
        echo "${_ver}" > "${APP_HOME}/version.id"
        CHOWN_DIR root:root "${APP_HOME}" || break
        CHMOD_FILE 0755 "${APP_HOME}/nzbhydra2" || break
        CHMOD_FILE 0755 "${APP_HOME}/core" || break
        # --
        # -- Remove backup and quit inner loop
        # --
        [ -d "${APP_HOME}.BAK" ] \
            && RM_DIRS "${APP_HOME}.BAK"
        break
    done

    # -- Restore previous version if upgrade failed
    if [ -d "${APP_HOME}.BAK" ] ; then
        ERROR "Upgrade failed: Restoring previous version"
        RUN_LOG "mv -f '${APP_HOME}.BAK' '${APP_HOME}' 2>&1"
    fi

    # -- Restart user's nzbhydra2 service instance
    systemctl is-enabled "nzbhydra2@${USERNAME}" > /dev/null \
        && SVC_RESTART "nzbhydra2@${USERNAME}"


    main_rc=0 
    break

done


SCRIPTEXIT
