#!/bin/bash

SCRIPT_VER="1.1"

APP="ruTorrent - AutoDL Plugin"
APP_CATEGORY="Torrent Clients and Utilities"
APP_SCOPE="user"
APP_ENABLED="yes"
DEPENDS="ruTorrent (Latest)"
#><#APP_CHECK_VER_CMD="awk "\$1 == \"version:\" {print \$2}" /var/www/rutorrent/plugins/autodl-irssi/plugin.info"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
USE_APACHE2="yes"
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

RUTORRENT_DIRECTORY="/var/www/rutorrent"
APP_DIRECTORY="${RUTORRENT_DIRECTORY}/plugins/autodl-irssi"

while : ; do

    main_rc=1

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

    APT_INSTALL "git-core" || break

    oWD=$( pwd )
    cd "${APP_DIRECTORY}" || break
    if [ ! -d "./.git" ] ; then
        RUN_LOG "git init" || break
        RUN_LOG "git config --global user.email 'user@example.com'" || break
        RUN_LOG "git config --global user.name 'Seedbox Admin'" || break
        RUN_LOG "git add ." || break
        RUN_LOG "git commit -m'RSB: initial commit'" || break
        RUN_LOG "git clean -f" || break
        RUN_LOG "git remote add origin 'https://github.com/autodl-community/autodl-rutorrent.git'" || break
    fi
    RUN_LOG "git fetch --all" || break
    RUN_LOG "git reset --hard origin/master" || break
    RUN_LOG "git pull --no-edit origin master -f" || break
    cd "${oWD}"

    SVC_STATUS  "irssi@${USERNAME}" \
        && SVC_RESTART  "irssi@${USERNAME}"



    main_rc=0
    break

done



SCRIPTEXIT
