#!/bin/bash

SCRIPT_VER="1.7"

APP="Jackett"
APP_CATEGORY="Torrent Clients and Utilities"
APP_ENABLED="yes"
#><#APP_CHECK_VER_CMD="[ -e "/opt/jackett/version.id" ] && cat "/opt/jackett/version.id" || if [ -e /usr/bin/mono -a -e /opt/jackett/JackettConsole.exe ] ; then /usr/bin/mono /opt/jackett/JackettConsole.exe --version | awk "{print \$NF}" | head -1 ; elif [ -e /opt/jackett/jackett.deps.json ] ; then grep "\"Jackett.Common\":" /opt/jackett/jackett.deps.json | head -1 | awk "{print \$NF}" | tr -d "\"," ; fi"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
USE_APACHE2="yes"
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/Jackett"
APP_HOME="/opt/jackett"

while : ; do

    main_rc=1 

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

    systemctl is-enabled "jackett@root" > /dev/null 2>&1 \
        && SVC_DISABLE "jackett@root"
    systemctl is-active "jackett@root" > /dev/null 2>&1 \
        && SVC_STOP    "jackett@root"

    SVC_DISABLE "jackett@${USERNAME}"
    SVC_STOP    "jackett@${USERNAME}"

    RM_DIRS     "${APP_DIRECTORY}"

    # It was last user?
    ret="$( find /home -type d -a -iwholename '*/.config/Jackett' )"
    if [ -z "${ret}" ] ; then
        RM_FILES "/etc/systemd/system/jackett@.service"
        SYSTEMD_RELOAD
        RM_DIRS "${APP_HOME}"
    fi


    main_rc=0 
    break

done


SCRIPTEXIT
