#!/bin/bash

#set -x

SCRIPT_VER="1.15"

APP="ThinLinc"
APP_CATEGORY="Remote Access"
APP_SCOPE="system"
APP_ENABLED="yes"
SYS_PKG_NAME="thinlinc-webaccess"
STORED_PASSWORD_SERVICE="user"
SUPPORTED_ON="CentOS7,CentOS8,CentOSStream8,Debian9,Debian10,Ubuntu16.04,Ubuntu18.04,Ubuntu20.04,Ubuntu22.04"
USE_APACHE2="yes"
LIBRARY="functions4.sh"

#TL_VER="4.12.0"
#TL_BUILD="6517"
#TL_VER="4.12.1"
#TL_BUILD="6733"
#TL_VER="4.13.0"
#TL_BUILD="2253"
#TL_VER="4.14.0"
#TL_BUILD="2408"
##
## thinlinc-server_4.15.0-3358_amd64.deb
##
#TL_VER="4.15.0"
#TL_BUILD="3358"
#
# thinlinc-server_4.16.0-3491_amd64.deb
#
TL_VER="4.16.0"
TL_BUILD="3491"


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

case "${LSB_VENDOR}" in
  "CentOS"|"CentOSStream")
      TL_ARCH="x86_64"
      ;;
  "Debian"|"Ubuntu")
      TL_ARCH="amd64"
      ;;
esac

APP_DIRECTORY="/opt/thinlinc"

while : ; do

    main_rc=1

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

    case "${LSB_VENDOR}" in
      "Debian"|"Ubuntu")
          case "${LSB_VER}" in
            "Ubuntu22.04")
                APT_INSTALL "python3-apt" || break
                ;;
            *)
                APT_INSTALL "python-apt" || break
                ;;
          esac
          ;;
    esac

    case "${LSB_VER}" in
      "Ubuntu20.04"|"Ubuntu22.04")
          wget -O "/tmp/python-gtk2.deb" \
               "http://mirrors.kernel.org/ubuntu/pool/universe/p/pygtk/python-gtk2_2.24.0-6_amd64.deb"
          RUN_LOG "dpkg -i --force-confmiss --force-confnew --force-overwrite '/tmp/python-gtk2.deb'"
          RUN_LOG "apt-get -f -y install" || break
          RM_FILES "/tmp/python-gtk2.deb"
          ;;
    esac

    WGET "${SUPPORT}/thinlinc/tl-${TL_VER}-server.zip" || break
    UNZIP "${TMP_DIR}/tl-${TL_VER}-server.zip" "${TMP_DIR}/" || break

    TL_PKGDIR="${TMP_DIR}/tl-${TL_VER}-server/packages"

    _pkglist=""
#    for _component in \
#        "tlmisc" \
#        "tlmisc-libs" \
#        "tlmisc-libs32" \
#        "vnc-server" \
#        "rdesktop" \
#        "tladm" \
#        "tlprinter" \
#        "vsm" \
#        "webaccess" \
    for _component in \
        "server" \
        "tlmisc-libs" \
        "tlmisc-libs32" \
        "vnc-server" \
        "rdesktop" \
        "tladm" \
        "tlprinter" \
        "vsm" \
        "webaccess" \
    ; do
        case "${LSB_VENDOR}" in
          "CentOS"|"CentOSStream")
              _pfx="thinlinc-${_component}-${TL_VER}-${TL_BUILD}"
              [ -e "${TL_PKGDIR}/${_pfx}.${TL_ARCH}.rpm" ] \
                  && _pkglist="${_pkglist} ${TL_PKGDIR}/${_pfx}.${TL_ARCH}.rpm"
              [ -e "${TL_PKGDIR}/${_pfx}.noarch.rpm" ] \
                  && _pkglist="${_pkglist} ${TL_PKGDIR}/${_pfx}.noarch.rpm"
              ;;
          "Debian"|"Ubuntu")
              _pfx="thinlinc-${_component}_${TL_VER}-${TL_BUILD}"
              [ -e "${TL_PKGDIR}/${_pfx}_${TL_ARCH}.deb" ] \
                  && _pkglist="${_pkglist} ${TL_PKGDIR}/${_pfx}_${TL_ARCH}.deb"
              [ -e "${TL_PKGDIR}/${_pfx}_all.deb" ] \
                  && _pkglist="${_pkglist} ${TL_PKGDIR}/${_pfx}_all.deb"
              ;;
        esac
    done

    INFO "Installing: [${_pkglist}]"

    case "${LSB_VENDOR}" in
      "CentOS"|"CentOSStream")
          RUN_LOG "rpm -i ${_pkglist} 2>&1" \
              || break
          ;;
      "Debian"|"Ubuntu")
          RUN_LOG "dpkg -i --force-confmiss --force-confnew --force-overwrite ${_pkglist} 2>&1" \
              || break
          ;;
    esac

    cat <<-EOF > "${TMP_DIR}/ANSWER_FILE"
	install-gtk=yes
	install-pygtk=no
	email-address=root
	setup-selinux=no
	setup-web-integration=no
	setup-nearest=no
	install-python-ldap=no
	setup-firewall=no
	server-type=master
	setup-apparmor=no
	missing-answer=abort
	install-nfs=no
	setup-thinlocal=no
	install-sshd=no
	tlwebadm-password=ChangeMe
	accept-eula=yes
	migrate-conf=ignore
	install-required-libs=yes
	EOF

    RUN_LOG "${APP_DIRECTORY}/sbin/tl-setup -a ${TMP_DIR}/ANSWER_FILE" || break

    # The maximum number of sessions per user. 0 means no limit.
    RUN_LOG "sed -i -e 's/^max_sessions_per_user=.*$/max_sessions_per_user=0/' \
              ${APP_DIRECTORY}/etc/conf.d/vsmserver.hconf"

    # Disable/stop TL Web Admin service
    SYSTEMD_RELOAD
    SVC_DISABLE "tlwebadm"
    SVC_STOP    "tlwebadm"

    # Enable/start TL Web Access services
    SVC_ENABLE "tlwebaccess"
    SVC_STATUS "tlwebaccess" || SVC_START  "tlwebaccess"
    SVC_ENABLE "vsmagent"
    SVC_STATUS "vsmagent"    || SVC_START  "vsmagent"
    SVC_ENABLE "vsmserver"
    SVC_STATUS "vsmserver"   || SVC_START  "vsmserver"

    # Configure `monit`
    if [ -d "/etc/monit/conf.d" ] ; then
        cat <<-EOF > "/etc/monit/conf.d/tlwebaccess"
	check process tlwebaccess
	  matching "tlwebaccess"
	  start program = "/usr/sbin/service tlwebaccess restart"
	  stop  program = "/usr/sbin/service tlwebaccess stop"
	  if 20 restarts with 20 cycles then timeout

	check process vsmserver
	  matching "vsmserver"
	  start program = "/usr/sbin/service vsmserver restart"
	  stop  program = "/usr/sbin/service vsmserver stop"
	  if 20 restarts with 20 cycles then timeout

	check process vsmagent
	  matching "vsmagent"
	  start program = "/usr/sbin/service vsmagent restart"
	  stop  program = "/usr/sbin/service vsmagent stop"
	  if 20 restarts with 20 cycles then timeout
	EOF
        MONIT_RELOAD
    fi

    RUN_LOG "touch /var/run/reboot-required"


    main_rc=0
    break

done


# Cleanup if error
if [ ${main_rc} -ne 0 -a ${main_rc} -ne 111 -a ${main_rc} -ne 117 ] ; then
    ERROR "'${APP}': Installation failed."
    # Cleanup tasks here
    _pkglist=""
#    for _component in \
#        "webaccess" \
#        "tladm" \
#        "vnc-server" \
#        "rdesktop" \
#        "tlprinter" \
#        "vsm" \
#        "tlmisc-libs" \
#        "tlmisc-libs32" \
#        "tlmisc" \
    for _component in \
        "server" \
    ; do
            _pkglist="${_pkglist} thinlinc-${_component}"
    done
    case "${LSB_VENDOR}" in
      "CentOS"|"CentOSStream")
          YUM_REMOVE ${_pkglist} || break
          ;;
      "Debian"|"Ubuntu")
          RUN_LOG "dpkg --purge --force-all ${_pkglist} 2>&1"
          ;;
    esac
    #
    RM_DIRS "${APP_DIRECTORY}"
fi

SCRIPTEXIT
