#!/bin/bash
# update all packages
sudo apt update && sudo apt upgrade -y
# install XRDP for RDP access and UFW as system firewall
sudo apt install xfce4 xfce4-goodies -y
sudo apt install xrdp xorg dbus-x11 x11-xserver-utils curl -y
sudo apt-get install xfonts-base --reinstall
sudo adduser xrdp ssl-cert
# Reboot is suggested
sudo systemctl restart xrdp

sudo apt install ufw -y
sed -i -e 's/IPV6=yes/IPV6=no/g' /etc/default/ufw
sudo systemctl status xrdp
sudo systemctl restart ufw

#Allow ports
sudo ufw allow 3389
sudo ufw allow 22,80,443/tcp

# RDP prompt fix:
sudo mkdir /etc/polkit-1/localauthority/50-local.d/
sudo cat >/etc/polkit-1/localauthority/50-local.d/45-allow-colord.pkla <<EOL
[Allow Colord all Users]
Identity=unix-user:*
Action=org.freedesktop.color-manager.create-device;org.freedesktop.color-manager.create-profile;org.freedesktop.color-m>ResultAny=no
ResultInactive=no
ResultActive=yes
EOL
cat /etc/polkit-1/localauthority/50-local.d/45-allow-colord.pkla

# Allow persistant RDP session for user:
file="/etc/xrdp/startwm.sh"
text_to_add="# workaround for local and remote session
unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR
. \$HOME/.profile

"
tmp_file=$(mktemp)
awk -v text="$text_to_add" '/test -x \/etc\/X11\/Xsession && / {print text} 1' "$file" > "$tmp_file"
mv "$tmp_file" "$file"


# Disable the terminal beeping
echo Disabling beeping in terminal
# echo >> blacklist pcspkr /etc/modprobe.d/blacklist.conf
# sudo rmmod pcspkr
OLD_LINE_irc="# set bell-style none"
NEW_LINE_irc="set bell-style none"
sudo sed -i "s/$OLD_LINE_irc/$NEW_LINE_irc/" /etc/inputrc

# Disable sleep and suspend
sudo systemctl stop sleep.target suspend.target hibernate.target hybrid-sleep.target
sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

