#!/bin/bash

SCRIPT_VER="1.5"

APP="Resilio Sync"
APP_CATEGORY="Sync Solutions"
APP_ENABLED="yes"
SYS_PKG_NAME="resilio-sync"
SUPPORTED_ON="CentOS7,CentOS8,CentOSStream8,Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
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/resilio-sync"
STORAGE_DIRECTORY="${USERHOME}/resilio-sync-storage"
LOG_DIRECTORY="/var/log/resilio-sync/${USERNAME}"
RSLSYNC_PORT="$(( USERUID + 8888 - FIRST_UID ))"

while : ; do

    main_rc=1

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


    # Disable/stop user service
    if [ -f "/etc/monit/conf.d/resilio-sync@${USERNAME}" ] ; then
        [ -e "/etc/monit/conf.d/resilio-sync@${USERNAME}" ] \
            && RM_FILES "/etc/monit/conf.d/resilio-sync@${USERNAME}"
        MONIT_RELOAD
    fi
    SVC_STOP    "resilio-sync@${USERNAME}"
    SVC_DISABLE "resilio-sync@${USERNAME}"
    if [ "${SVC_SYSTEM}" = "init" ] ; then
        [ -e "/etc/init.d/resilio-sync@${USERNAME}" ] \
            && RM_FILES "/etc/init.d/resilio-sync@${USERNAME}"
    fi

    [ -e "/etc/logrotate.d/resilio-sync-${USERNAME}" ] \
        && RM_FILES "/etc/logrotate.d/resilio-sync-${USERNAME}"

    [ -e "${APP_DIRECTORY}" ] \
        && RM_DIRS "${APP_DIRECTORY}"
    [ -e "${LOG_DIRECTORY}" ] \
        && RM_DIRS "${LOG_DIRECTORY}"

    if [ -e "/etc/apache2/userconf-enabled/resilio-${USERNAME}.conf" ] ; then
        RM_FILES "/etc/apache2/userconf-enabled/resilio-${USERNAME}.conf"
        SVC_RELOAD "apache2"
    fi

    # Configure firewall
    FW_REMOVE_PORTS "${RSLSYNC_PORT}/tcp"

    # It was last user in Resilio Sync? - Will delete Resilio Sync from machine....
    ret="$( find /home -type d -a -iwholename '*/.config/resilio-sync' )"
    if [ -z "${ret[@]}" ] ; then
         ${PKG_REMOVE} "resilio-sync"
         if [ -e "/etc/yum.repos.d/resilio-sync.repo" ] ; then
             RM_FILES "/etc/yum.repos.d/resilio-sync.repo"
         fi
         if [ -e "/etc/apt/sources.list.d/resilio-sync.list" ] ; then
             RM_FILES "/etc/apt/sources.list.d/resilio-sync.list"
             APT_UPDATE
         fi
         if [ -e "/etc/systemd/system/resilio-sync@.service" ] ; then
             RM_FILES "/etc/systemd/system/resilio-sync@.service"
         fi
         if [ -d "/etc/systemd/system/resilio-sync@.service.d" ] ; then
             RM_DIRS "/etc/systemd/system/resilio-sync@.service.d"
         fi
         if [ -e "/etc/systemd/system/resilio-sync.slice" ] ; then
             RM_FILES "/etc/systemd/system/resilio-sync.slice"
         fi
         SYSTEMD_RELOAD
         [ -e "/var/log/resilio-sync" ] \
             && RM_DIRS "/var/log/resilio-sync"
         id rslsync > /dev/null 2>&1 \
             && RUN_LOG "userdel -f -r rslsync 2>&1"
    fi


    main_rc=0
    break

done


SCRIPTEXIT
