#!/bin/bash

SCRIPT_VER="1.4"

APP="Transmission Daemon (git)"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
CONFLICTS="Transmission Daemon"
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,Ubuntu24.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

    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

    GITHUB_RELEASE "transmission/transmission" "latest" || break
    _tr_git_tag="${GITHUB_RELEASE_TAG}"
    _tr_git_ver="$( sed -e 's/^.*-v//' -e 's/^v//' <<< "${GITHUB_RELEASE_VERSION}" )"
    if [ -z "${_tr_git_tag}" ] || [ -z "${_tr_git_ver}" ] ; then
        ERROR "Can't retrieve '${APP}' release info"
        break
    fi

    JOB_START "Building '${APP}'"

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

    pushd "${_src_dir}/build" > /dev/null 2>&1 || break
    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
    echo "${_tr_git_ver}" > "${_src_dir}/version.id"
    popd > /dev/null 2>&1 || break

    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
