#!/bin/bash

SCRIPT_VER="1.3"

APP="OpenVPN 2.5"
APP_CATEGORY="System Services"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="openvpn"
CONFLICTS="OpenVPN 2.4"
#><#APP_UPGRADE_VER_CMD="STANDARD_PACKAGE"
SUPPORTED_ON="Debian10,Ubuntu16.04,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 old (?) `openvpn` package
    if DPKG_CHECK "openvpn" ; then
        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"
        APT_AUTOREMOVE
    fi

    # -- Install new `openvpn` package
    if [ ! -d "/etc/apt/sources.list.d" ] ; then
        MKDIR "/etc/apt/sources.list.d" || break
    fi

    if [ "${LSB_VER}" = "Ubuntu22.04" ] ; then
        INFO "[${LSB_VER}]: We will use standard repositories"
    else
        echo "deb http://build.openvpn.net/debian/openvpn/release/2.5 ${LSB_CODENAME} main" \
                    > "/etc/apt/sources.list.d/openvpn25.list"
        if [ $? -ne 0 ] ; then
            ERROR "Error adding ${APP} repository!" ; break
        fi
        ADD_APT_KEY_URL "https://swupdate.openvpn.net/repos/repo-public.gpg" "openvp.gpg"
        if [ $? -ne 0 ] ; then
            ERROR "Error adding ${APP} repository key!" ; break
        fi
        APT_UPDATE || break
    fi

    APT_INSTALL "openvpn" "easy-rsa" || break

    # Workaround for `openvpn-plugin-auth-pam.so` relocation to the plugins directory
    if [ ! -e "/usr/lib/openvpn/openvpn-plugin-auth-pam.so" ] ; then
        case "${LSB_VER}" in
          "Ubuntu18.04"|"Ubuntu20.04")
              _src="/usr/lib/x86_64-linux-gnu/openvpn/plugins/openvpn-plugin-auth-pam.so"
              ;;
          *)
              _src="/usr/lib/openvpn/plugins/openvpn-plugin-auth-pam.so"
              ;;
        esac
        if [ ! -e "${_src}" ] ; then
            ERROR "Could not find plugin: '${_src}'"
            break
        else
            [ -e "/usr/lib/openvpn" ] || MKDIR "/usr/lib/openvpn"
            LN "${_src}" \
                    "/usr/lib/openvpn/openvpn-plugin-auth-pam.so" \
                || break
        fi
    fi

    # -- Configure 
    WGET "${SUPPORT}/openvpn/openvpn-rsb-config.tar.gz" || break
    TAR_ZXF "${TMP_DIR}/openvpn-rsb-config.tar.gz" "/etc/" || break
    CHOWN_DIR  root:root "/etc/openvpn" || break
    CHOWN_FILE root:root "/etc/pam.d/openvpn" || break

    # -- Enable/start services
    if grep -e '^LimitNPROC=' "/lib/systemd/system/openvpn@.service" >> "${LOGFILE}" 2>&1 ; then
        sed -e 's|^\(LimitNPROC=.*\)$|#\1|' -i "/lib/systemd/system/openvpn@.service" >> "${LOGFILE}" 2>&1
    fi
    SVC_ENABLE  "openvpn@rsb-server"
    SVC_RESTART   "openvpn@rsb-server"
    if [ -d "/etc/monit/conf.d" ] ; then
        cat <<-EOF > "/etc/monit/conf.d/openvpn@rsb-server"
		check process openvpn-rsb-server with pidfile /var/run/openvpn/rsb-server.pid
		  start program = "/usr/sbin/service openvpn@rsb-server restart"
		  stop  program = "/usr/sbin/service openvpn@rsb-server stop"
		  if 5 restarts with 5 cycles then timeout
		EOF
        MONIT_RELOAD
    fi


    main_rc=0
    break

done


SCRIPTEXIT
