#!/bin/bash

SCRIPT_VER="1.6"

APP="X2Go Server"
APP_CATEGORY="Remote Access"
APP_ENABLED="yes"
SYS_PKG_NAME="x2goserver"
SUPPORTED_ON="CentOS7,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04,Ubuntu24.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

while : ; do

    main_rc=1

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

    if ls -A /etc/apt/sources.list.d/x2go-ubuntu* > /dev/null 2>&1 ; then
        RUN_LOG "apt-mark unhold x2go* 2>&1"
    fi

    ${PKG_REMOVE} \
          "x2goserver" \
          "x2goserver-x2goagent" \
          "x2goserver-xsession"

    case "${LSB_VENDOR}" in
      "Debian"|"Devuan")
          RM_FILES "/etc/apt/sources.list.d/x2go.list"
          ;;
      "Ubuntu")
          DEL_APT_REPO "ppa:x2go/ppa"
          DEL_APT_REPO "ppa:x2go/stable"
          ;;
    esac

    ${PKG_UPDATE}

    # 'monit' configuration
    if [ -f "/etc/monit/conf.d/x2goserver" ] ; then
        RM_FILES "/etc/monit/conf.d/x2goserver"
        MONIT_RELOAD
    fi

    # disable/stop service
    SVC_DISABLE "x2goserver"
    SVC_STOP    "x2goserver"
    if [ -f "/etc/systemd/system/x2goserver.service" ] ; then
        RM_FILES "/etc/systemd/system/x2goserver.service"
        SYSTEMD_RELOAD
    fi


    main_rc=0
    break

done


SCRIPTEXIT
