#!/bin/bash

SCRIPT_VER="1.5"

APP="XRDP"
APP_CATEGORY="Remote Access"
APP_SCOPE="system"
APP_ENABLED="yes"
STORED_PASSWORD_SERVICE="user"
SUPPORTED_ON="CentOS7,CentOS8,CentOSStream8,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

XRDP_PORT="57389"

while : ; do

    main_rc=1

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

    case "${LSB_VENDOR}" in
      "CentOS"|"CentOSStream")
          PKGS="tigervnc-server"
          ;;
      "Debian"|"Ubuntu")
          case "${LSB_CODENAME}" in
            "focal"|"jammy")
                PKGS=""
                ;;
            *)
                PKGS="vnc4server"
                ;;
          esac
          ;;    
      *)
          ERROR "Unsupported vendor: ${LSB_VENDOR}"
          break
          ;;
    esac

    # Install 'xrdp' package
    ${PKG_INSTALL} "xrdp" ${PKGS} || break

    # Fix group membership
    case "${LSB_VENDOR}" in
      "Debian"|"Ubuntu")
#          INFO "DBG #1"
          if getent group "ssl-cert" > /dev/null 2>&1 ; then
#              INFO "DBG #2"
              RUN_LOG "usermod -aG ssl-cert xrdp 2>&1"
          fi
          ;;
    esac

    # Configure listening port
    _ini_file="/etc/xrdp/xrdp.ini"
    if crudini --get "${_ini_file}" Globals port > /dev/null 2>&1 ; then
        _port="$( crudini --get "${_ini_file}" Globals port )"
        _section="Globals"
    elif crudini --get "${_ini_file}" globals port > /dev/null 2>&1 ; then
        _port="$( crudini --get "${_ini_file}" globals port )"
        _section="globals"
    fi
    if [ -z "${_port}" ] ; then
        ERROR "Failed to determine xrdp listening port."
        break
    fi
    if [ "${_port}" != "${XRDP_PORT}" ] ; then
        if crudini --set --existing "${_ini_file}" "${_section}" port "${XRDP_PORT}" ; then
            INFO "Port changed to: ${XRDP_PORT}"
            if systemctl is-active "xrdp" > /dev/null 2>&1 ; then
                INFO "Restarting 'xrdp' service..."
                SVC_STOP "xrdp"
                killall -9 xrdp 2> /dev/null
                killall -9 xrdp-sesman 2> /dev/null
                rm -vf /var/run/xrdp/xrdp*.pid
            else
                INFO "'xrdp' service is not running."
            fi
        else
            ERROR "Failed to change the port to: ${XRDP_PORT}"
        fi
    fi

    # Configure firewall
    FW_OPEN_PORTS "${XRDP_PORT}/tcp"

    SVC_ENABLE "xrdp"
    SVC_START  "xrdp"


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    SVC_DISABLE "xrdp"
    SVC_STOP    "xrdp"
    ${PKG_REMOVE} "xrdp"
fi


SCRIPTEXIT
