#!/bin/bash

SCRIPT_VER="1.1"

APP="VueTorrent UI"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="system"
APP_ENABLED="yes"
DEPENDS="qBittorrent WebUI"
STORED_PASSWORD_SERVICE="userdir"
#><#APP_CHECK_VER_CMD="[ -e "/opt/vuetorrent" ] && cat "/opt/vuetorrent/version.id" || echo """
#><#APP_UPGRADE_VER_CMD="curl -Ls --connect-timeout 10 --retry 3 -m 30 -o /dev/null -w "%{url_effective}" https://github.com/WDaan/VueTorrent/releases/latest | awk -F/ "{print \$NF}" | sed -e "s/^.*-v//" -e "s/^v//""
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04"
USE_APACHE2="yes"
DAEMON_PORT=5000
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

VALIDATE_USER

APP_DIRECTORY="/opt/vuetorrent"

while : ; do

    main_rc=1

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

    # -- Sanity checks
    if [ "${USERUID}" != "${FIRST_UID}" ] ; then
        ERROR "Cannot install this application for the user account '${USERNAME}'"
        break
    fi
    if [ ! -e "${USERHOME}/.config/qBittorrent/qBittorrent.conf" ] ; then
        ERROR "Please install 'qBittorrent WebUI' application first!"
        break
    fi

    if ! which crudini > /dev/null 2>&1 ; then
        ${PKG_INSTALL} crudini || break
    fi

    if [ ! -d "${APP_DIRECTORY}" ] ; then
        MKDIR "${APP_DIRECTORY}" || break
    fi

    _vue_ver="$( curl -Ls -m 30 -o /dev/null -w "%{url_effective}" \
                          https://github.com/WDaan/VueTorrent/releases/latest \
                     | awk -F/ "{print \$NF}" \
                     | sed -e "s/^.*-v//" -e "s/^v//" )"

    if [ -z "${_vue_ver}" ] ; then
        ERROR "Failed to retrieve ${APP} release URL !"
        break
    fi

    WGET "https://github.com/WDaan/VueTorrent/releases/download/v${_vue_ver}/vuetorrent.zip" || break
    [ -d "${APP_DIRECTORY}" ] && RM_DIRS "${APP_DIRECTORY}"
    UNZIP "${TMP_DIR}/vuetorrent.zip" "/opt/" || break

    echo "${_vue_ver}" > "${APP_DIRECTORY}/version.id"

    # -- Modify settings
    RUN_LOG "crudini --del '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
                           'Preferences' \
                           'WebUI\AlternativeUIEnabled' 2>&1"
    RUN_LOG "crudini --del '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
                           'Preferences' \
                           'WebUI\RootFolder' 2>&1"
    RUN_LOG "crudini --set '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
                           'Preferences' \
                           'WebUI\AlternativeUIEnabled' \
                           'true' 2>&1"
    RUN_LOG "crudini --set '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
                           'Preferences' \
                           'WebUI\RootFolder' \
                           '/opt/vuetorrent' 2>&1"
    RUN_LOG "sed -i -re 's/\s+=\s+/=/' '${USERHOME}/.config/qBittorrent/qBittorrent.conf' 2>&1"


    # -- Restart service 'qbittorrent@${USERNAME}'
    RUN_LOG "systemctl is-active qbittorrent@${USERNAME} 2>&1" \
        && SVC_RESTART "qbittorrent@${USERNAME}"


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 ] && [ ${main_rc} -ne 111 ] && [ ${main_rc} -ne 117 ] ; then
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
#    [ -d "${APP_DIRECTORY}" ] && RM_DIRS "${APP_DIRECTORY}"
#    if [ -e "${USERHOME}/.config/qBittorrent/qBittorrent.conf" ] ; then
#        RUN_LOG "crudini --del '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
#                               'Preferences' \
#                               'WebUI\AlternativeUIEnabled' 2>&1"
#        RUN_LOG "crudini --del '${USERHOME}/.config/qBittorrent/qBittorrent.conf' \
#                               'Preferences' \
#                               'WebUI\RootFolder' 2>&1"
#        RUN_LOG "sed -i -re 's/\s+=\s+/=/' '${USERHOME}/.config/qBittorrent/qBittorrent.conf' 2>&1"
#        RUN_LOG "systemctl is-active qbittorrent@${USERNAME} 2>&1" \
#            && SVC_RESTART "qbittorrent@${USERNAME}"
#    fi
fi

SCRIPTEXIT
