#!/bin/bash

SCRIPT_VER="1.1"

APP="OpenVPN 2.6"
APP_CATEGORY="System Services"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="openvpn"
CONFLICTS="OpenVPN 2.4|OpenVPN 2.5"
SUPPORTED_ON="Ubuntu18.04,Ubuntu20.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

    # -- Remove `openvpn` package
    if [ -e "/etc/monit/conf.d/openvpn@rsb-server" ] ; then
        RM_FILES "/etc/monit/conf.d/openvpn@rsb-server"
        MONIT_RELOAD
    fi

    SVC_STOP       "openvpn@rsb-server"
    SVC_DISABLE    "openvpn@rsb-server"

    APT_REMOVE "openvpn"

    RM_DIRS  "/etc/openvpn"
    RM_FILES "/etc/pam.d/openvpn"

    if [ -e "/etc/systemd/system/openvpn@.service" ] ; then
        RM_FILES "/etc/systemd/system/openvpn@.service"
        SYSTEMD_RELOAD
    fi

    RM_FILES "/etc/apt/sources.list.d/openvpn26.list"
    APT_UPDATE


    main_rc=0
    break

done


SCRIPTEXIT
