#!/bin/bash

SCRIPT_VER="1.1"

APP="Simple Torrent"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="userdir"
#><#APP_CHECK_VER_CMD="if [ -x "/usr/local/bin/simple-torrent" ] && [ -d "${APP_DIRECTORY}" ] ; then simple-torrent --version 2>&1 ; else echo "" ; fi"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04"
USE_APACHE2="yes"
DAEMON_PORT=53540
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/simple-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

    ST_PORT="$(( USERUID - FIRST_UID + 53540 ))"
    ST_INCOMING_PORT="$(( USERUID - FIRST_UID + 50017 ))"

    WGET "${SUPPORT}/simple-torrent/simple-torrent" || break
    MV "${TMP_DIR}/simple-torrent" "/usr/local/bin/simple-torrent" || break
    CHMOD_FILE 0755 "/usr/local/bin/simple-torrent" || break

    # Application configuation
    cat <<-EOF > "${APP_DIRECTORY}/simple-torrent.json"
	{
	  "allowruntimeconfigure": true,
	  "autostart": true,
	  "disableutp": false,
	  "donecmd": "",
	  "downloaddirectory": "${USERHOME}/Downloads",
	  "enableseeding": true,
	  "enableupload": true,
	  "incomingport": ${ST_INCOMING_PORT},
	  "maxconcurrenttask": 0,
	  "nodefaultportforwarding": true,
	  "obfspreferred": true,
	  "obfsrequirepreferred": false,
	  "proxyurl": "",
	  "seedratio": 0,
	  "seedtime": "0",
	  "watchdirectory": "${USERHOME}/Watch"
	}
	EOF

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

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

	[Service]
	Type=simple
	WorkingDirectory=/home/%I
	User=%I
	Group=%I
	ExecStart=/usr/local/bin/simple-torrent \\
	              --listen 127.0.0.1:${ST_PORT} \\
	              --config-path /home/%I/.config/simple-torrent/simple-torrent.json \\
	              --title "Simple Torrent"
	Restart=always
	StandardOutput=null

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

    SVC_ENABLE "simple-torrent@${USERNAME}" || break
    SVC_START  "simple-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 "simple-torrent@${USERNAME}"
    SVC_STOP    "simple-torrent@${USERNAME}"
    [ -d "${APP_DIRECTORY}" ] && RM_DIRS "${APP_DIRECTORY}"
    RM_FILES    "/etc/systemd/system/simple-torrent@.service" \
                "/usr/local/bin/simple-torrent"
    SYSTEMD_RELOAD
fi

SCRIPTEXIT
