#!/bin/bash

# xrdp-0.9.18-3

# define func
function LINE {
  STTY_SIZE=$(stty size)
  STTY_COLUMNS=$(echo $STTY_SIZE | cut -f2 -d" ")
  yes = 2>/dev/null | sed $STTY_COLUMNS'q' | tr -d '\n'
}

function pad {
  echo -e " $(date +%H%M) \e[1;32m${1}\e[0;39m"
}

function rht_usb_locale {
    # Failed to set locale, defaulting to C
    echo "export LC_ALL=en_US.UTF-8" >> /etc/profile
    source /etc/profile
}

function configure_repo {
    pad $(hostname -s) "Configuring tuna repo"
    RV=$(grep -o [0-9] /etc/redhat-release | head -n 1)
    if [ -f /etc/redhat-release ]; then
        curl https://vmcc.xyz:8443/redhat/centos-$RV-for-x86_64.repo \
            -o /etc/yum.repos.d/centos-$RV-for-x86_64.repo >&4 2>&1
    else
        echo Your RHEL must in 8 or 9
        exit 1
    fi
}

function install_pkgs {
    # xrdp xorgxrdp xorg-x11-server-Xorg xorg-x11-server-common
    pad "Installing xrdp xorgxrdp"
    yum -y install xrdp xorgxrdp >&4 2>&1
}

function configure_file {
    pad "Configuring default Xorg in xrdp.ini"
    sed -i.bk '/\[Xorg/,/^\[Xvnc/s|#||' /etc/xrdp/xrdp.ini
}

function configure_service {
    pad "Starting xrdp service"
    systemctl enable --now xrdp &>/dev/null
}

function configure_firewall {
    pad $(hostname -s) "Setting firewall"
    firewall-cmd --permanent --add-port=3389/tcp >&4 2>&1
    firewall-cmd --reload >&4 2>&1
}

# Main area
if ! [ $(id -u) = 0 ]; then
    echo -e "\nPlease use root\n"
fi
LOGFILE=/tmp/xrdp-setup.log
exec 4>${LOGFILE}
echo $(date "+%b %d %T") $(hostname -s) ================= >&4 2>&1
echo -e "
Please wait a moment, about \033[1;37m3\033[0m minutes. 
If you see more details, please open a new terminal, and type 
    \033[1;32mtail -f ${LOGFILE}\033[0;39m"
LINE
rht_usb_locale
configure_repo
install_pkgs
configure_file
configure_service
configure_firewall
pad WARNING 同一用户，不能"远程"和"本地"同时登陆
echo $(date "+%b %d %T") $(hostname -s) ================= >&4 2>&1