#!/bin/bash

SCRIPT_VER="1.2"

APP="Cloud Torrent"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="userdir"
SYS_PKG_NAME="cloud-torrent"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
USE_APACHE2="yes"
DAEMON_PORT=53530
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="${USERHOME}/.config/cloud-torrent"

while : ; do

    main_rc=1

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


    if [ "${USERUID}" != "${FIRST_UID}" ] ; then
        ERROR "Cannot install this application for the user account '${USERNAME}'"
        break
    fi

    MKDIR "${APP_DIRECTORY}" || break

    CT_PORT="$(( USERUID - FIRST_UID + 53530 ))"
    CT_INCOMING_PORT="$(( USERUID - FIRST_UID + 50007 ))"

    url="$(GITHUB_RELEASE "jpillora/cloud-torrent" "latest" "amd64" "deb")"
    WGET "$url" "${TMP_DIR}/cloud-torrent.deb" || break
    DPKG_INSTALL "${TMP_DIR}/cloud-torrent.deb" || break

    # Application configuation
    cat <<-EOF > "${APP_DIRECTORY}/cloud-torrent.json"
	{
	  "AutoStart": true,
	  "DisableEncryption": true,
	  "DownloadDirectory": "${USERHOME}/Downloads",
	  "EnableUpload": true,
	  "EnableSeeding": true,
	  "IncomingPort": ${CT_INCOMING_PORT}
	}
	EOF

    CHOWN_DIR "${USERNAME}":"${USERNAME}" "${APP_DIRECTORY}" || break

    # SystemD service unit
    if [ ! -e "/etc/systemd/system/cloud-torrent@.service" ] ; then
        cat <<-EOF > "/etc/systemd/system/cloud-torrent@.service"
	[Unit]
	Description=Cloud Torrent
	After=network.target

	[Service]
	Type=simple
	WorkingDirectory=/home/%I
	User=%I
	Group=%I
	ExecStart=/usr/bin/cloud-torrent \\
	              --host 127.0.0.1 \\
	              --port ${CT_PORT} \\
	              --config-path /home/%I/.config/cloud-torrent/cloud-torrent.json \\
	              --title "Cloud Torrent" --log
	Restart=always
	StandardOutput=null

	[Install]
	WantedBy=multi-user.target
	EOF
        SYSTEMD_RELOAD
    fi

    SVC_ENABLE "cloud-torrent@${USERNAME}" || break
    SVC_START  "cloud-torrent@${USERNAME}" || break


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
    SVC_DISABLE "cloud-torrent@${USERNAME}"
    SVC_STOP    "cloud-torrent@${USERNAME}"
    [ -d "${APP_DIRECTORY}" ] && RM_DIRS "${APP_DIRECTORY}"
    RM_FILES    "/etc/systemd/system/cloud-torrent@.service" \
                "/usr/local/bin/cloud-torrent"
    SYSTEMD_RELOAD
fi

SCRIPTEXIT
