#!/bin/bash

SCRIPT_VER="1.4"

APP="Resilio Sync"
APP_CATEGORY="Sync Solutions"
APP_SCOPE="user"
APP_ENABLED="yes"
SYS_PKG_NAME="resilio-sync"
#><#APP_UPGRADE_VER_CMD="STANDARD_PACKAGE"
SUPPORTED_ON="Ubuntu20.04,Ubuntu22.04,Ubuntu24.04"
LIBRARY="functions4.sh"
RESILIO_APT_REPO_URL="http://linux-packages.resilio.com/resilio-sync/deb"
RESILIO_APT_KEY_URL="https://linux-packages.resilio.com/resilio-sync/key.asc"

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"

while : ; do

    main_rc=1

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

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

    case "${LSB_VENDOR}" in
      "Ubuntu")
          APT_INSTALL "ca-certificates" "gnupg" || break
          ADD_APT_REPO "${RESILIO_APT_REPO_URL}" "resilio-sync" "non-free" \
              "${RESILIO_APT_KEY_URL}" "resilio-sync" || break
          APT_UPDATE || break
          APT_INSTALL "resilio-sync" || break
          ;;
    esac

    # Stop/disable system service - we do not need it :)
    if [ -f "/etc/monit/conf.d/resilio-sync" ] ; then
        RM_FILES "/etc/monit/conf.d/resilio-sync"
        MONIT_RELOAD
    fi
    SVC_STOP      "resilio-sync"
    SVC_DISABLE   "resilio-sync"
    if [ "${SVC_SYSTEM}" = "init" ] ; then
        RM_FILES "/etc/init.d/resilio-sync"
    fi

    # Stop/disable BTSync service - conflicts
    SVC_STOP      "btsync@${USERNAME}"
    SVC_DISABLE   "btsync@${USERNAME}"

    SVC_STATUS  "resilio-sync@${USERNAME}" \
        && SVC_RESTART   "resilio-sync@${USERNAME}"


    main_rc=0
    break

done


SCRIPTEXIT
