#!/bin/bash

SCRIPT_VER="1.2"

APP="Transmission 4 Daemon"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="transmission"
#><#APP_UPGRADE_VER_CMD="curl -Ls --connect-timeout 10 --retry 3 -m 30 -o /dev/null -w "%{url_effective}" https://github.com/transmission/transmission/releases/latest | awk -F/ "{print \$NF}" | sed -e "s/^.*-v//" -e "s/^v//""
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04"
USE_APACHE2="yes"
DAEMON_PORT=9101
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

TR4_VERSION="4.0.0"

APP_DIRECTORY="${USERHOME}/.config/transmission4-daemon"

while : ; do

    main_rc=1

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

    [ -d "${USERHOME}" ] || break

    SVC_STOP "transmission-daemon"
    SVC_DISABLE "transmission-daemon"
    SVC_MASK "transmission-daemon"
    if [ -e "/etc/systemd/system/multi-user.target.wants/transmission-daemon.service" ] ; then
        RM_FILES "/etc/systemd/system/multi-user.target.wants/transmission-daemon.service"
    fi
    SYSTEMD_RELOAD

    _src_dir="/usr/local/src/transmission4"
    if [ ! -d "${_src_dir}" ] ; then
        ERROR "[${_src_dir}]: Directory not found"
        break
    fi
    if [ ! -d "${_src_dir}/.git" ] ; then
        ERROR "[${_src_dir}/.git]: Directory not found"
        break
    fi

    APT_INSTALL \
        "cmake" \
        "libcurl4-openssl-dev" \
        "libssl-dev" \
      || break

    _tr4_ver="$( curl -Ls -m 30 -o /dev/null -w "%{url_effective}" \
                      https://github.com/transmission/transmission/releases/latest 2> /dev/null \
                   | awk -F/ "{print \$NF}" )"
    if [ -z "${_tr4_ver}" ] ; then
        ERROR "Can't retrieve new '${APP}' version info"
        break
    fi

    JOB_START "Building '${APP}'"

    pushd "${_src_dir}" > /dev/null 2>&1
    RUN_LOG "git fetch --all 2>&1"
    RUN_LOG "git checkout tags/${_tr4_ver} 2>&1"
    RUN_LOG "git fetch 2>&1"
    popd > /dev/null 2>&1

    pushd "${_src_dir}/build" > /dev/null 2>&1
    RUN_LOG "make clean 2>&1" || break
    RUN_LOG "git submodule foreach --recursive git clean -xfd 2>&1" || break
#    RUN_LOG "git pull --rebase --prune 2>&1" || break
    RUN_LOG "git submodule update --recursive 2>&1" || break
    # -- Use -DCMAKE_BUILD_TYPE=RelWithDebInfo to build optimized binary.
    RUN_LOG "cmake -DCMAKE_BUILD_TYPE=RelWithDebInfo .. 2>&1" || break
    RUN_LOG "make 2>&1" || break
    RUN_LOG "make install 2>&1" || break
    popd > /dev/null 2>&1

    JOB_END "Building '${APP}'"

    systemctl is-active "transmission4-daemon@${USERNAME}" > /dev/null 2>&1 \
        && SVC_RESTART  "transmission4-daemon@${USERNAME}"
 

    main_rc=0
    break

done


SCRIPTEXIT
