#!/bin/bash

SCRIPT_VER="2.0"

APP="rTorrent"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="system"
APP_ENABLED="yes"
#><#APP_CHECK_VER_CMD="${PKG_VER} rtorrent"
#><#APP_UPGRADE_VER_CMD="STANDARD_PACKAGE"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.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

VALIDATE_USER

RTORRENT_PPA="ppa:rapidseedbox/ppa"

while : ; do

    main_rc=1

    CHECK_APP ; _rc=$?
    if [ ${_rc} -ne 0 ] && ! DPKG_CHECK "rtorrent" ; then
        main_rc=${_rc}
        break
    fi

    APT_INSTALL "software-properties-common" || break
    if ! ADD_APT_PPA "${RTORRENT_PPA}" ; then
        WARN "Failed to add '${RTORRENT_PPA}'. Will continue with currently configured APT sources."
    fi

    APT_UPDATE || break
    APT_INSTALL "rtorrent" "screen" || break

    if [ ! -e "/etc/systemd/system/rtorrent@.service" ] \
       || ! grep -q -e '^ExecStart=.*\s*/usr/bin/rtorrent' "/etc/systemd/system/rtorrent@.service" > /dev/null 2>&1
    then
        cat <<-EOUNIT > "/etc/systemd/system/rtorrent@.service"
	[Unit]
	Description=rTorrent
	After=network.target

	[Service]
	Type=forking
	User=%i
	Group=%i
	KillMode=none
	ExecStartPre=-/bin/rm -f /var/www/session/%i/rtorrent.pid
	ExecStartPre=-/bin/rm -f /var/www/session/%i/rtorrent.lock
	ExecStart=/usr/bin/screen -d -m -fa -S rtorrent-%i /usr/bin/rtorrent
	ExecStartPost=/bin/sh -c "echo \$MAINPID > /var/www/session/%i/rtorrent.pid"
	ExecStop=/usr/bin/screen -S rtorrent-%i -X quit
	Restart=always

	[Install]
	WantedBy=multi-user.target
	EOUNIT

        [ -d "/etc/systemd/system/rtorrent@.service.d" ] \
            || MKDIR "/etc/systemd/system/rtorrent@.service.d" || break

        cat <<-EOFCONF > "/etc/systemd/system/rtorrent@.service.d/10-Nice.conf"
	[Service]
	Nice=19
	EOFCONF

        cat <<-EOFCONF > "/etc/systemd/system/rtorrent@.service.d/15-IOScheduling.conf"
	[Service]
	IOSchedulingClass=2
	IOSchedulingPriority=7
	EOFCONF

        cat <<-EOFCONF > "/etc/systemd/system/rtorrent@.service.d/17-LimitNOFILE.conf"
	[Service]
	LimitNOFILE=4000
	EOFCONF

        SYSTEMD_RELOAD || break
    fi

    while read -r _dir ; do
        [ -z "${_dir}" ] && continue
        _user="$( basename "${_dir}" )"
        systemctl is-active "rtorrent@${_user}" > /dev/null 2>&1 \
            && SVC_RESTART "rtorrent@${_user}"
    done < <( find /home -mindepth 1 -maxdepth 1 -type d | sort )

    main_rc=0
    break

done


# Cleanup if error
if [ "${main_rc}" -ne 0 ] && [ "${main_rc}" -ne 111 ] && [ "${main_rc}" -ne 117 ] ; then
    ERROR "'${APP}': Upgrade failed."
fi

SCRIPTEXIT
