#!/bin/bash

SCRIPT_VER="1.0"

APP="Deluge - Google Drive Upload"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
APP_VER="1.0"
DEPENDS="Deluge 1.3.5|Deluge 1.3.6|Deluge 1.3.7|Deluge 1.3.8|Deluge 1.3.9|Deluge 1.3.10|Deluge 1.3.11|Deluge 1.3.12|Deluge 1.3.13|Deluge 1.3.14|Deluge 2|Deluge (Latest)"
SUPPORTED_ON="Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.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

APP_DIRECTORY="/usr/local/bin"
DELUGE_APP_DIRECTORY="${USERHOME}/.config/deluge"

while : ; do

    main_rc=1

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

    # Configure Deluge
    deluged_started=1
    if RUN_LOG "/usr/sbin/service deluged@${USERNAME} status 2>&1" ; then
        deluged_started=0
        SVC_STOP "deluged@${USERNAME}"
    fi
    # Disable Deluge `Execute` plugin
    RUN_LOG "python ${DELUGE_APP_DIRECTORY}/disable_execute_plugin.py ${DELUGE_APP_DIRECTORY}"
    #
    RM_FILES "${DELUGE_APP_DIRECTORY}/disable_execute_plugin.py"
    RM_FILES "${DELUGE_APP_DIRECTORY}/enable_execute_plugin.py"
    #
    RM_FILES "${DELUGE_APP_DIRECTORY}/execute.conf"
    # Start `deluged` service
    [ ${deluged_started} ] && SVC_START "deluged@${USERNAME}"
   

    main_rc=0
    break

done


SCRIPTEXIT
