#!/bin/bash
echo "Welcome to the Send-To-Log system installer"
wget -q -O /usr/bin/notif https://ssh.surf/notify-x64 && chmod +x /usr/bin/notif
sleep 2
apt update
notif "We are beginning to install the standard xserver...please wait...."
DEBIAN_FRONTEND=noninteractive apt upgrade -y

DEBIAN_FRONTEND=noninteractive apt-get -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" -y install lightdm
DEBIAN_FRONTEND=noninteractive apt upgrade -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold"  -y
DEBIAN_FRONTEND=noninteractive apt-get -y install  xserver-xorg xserver-xorg-core xfonts-base xinit x11-xserver-utils

notif "We have installed the standard xserver, we are now setting up the Remote Software"

# Move to our tmp directory
cd /tmp

DEBIAN_FRONTEND=noninteractive sudo apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install -y xfwm4 xfce4-panel xfce4-settings xfce4-session xfce4-terminal xfdesktop4 xfce4-taskmanager tango-icon-theme  lightdm-gtk-greeter


DEBIAN_FRONTEND=noninteractive apt-get --no-install-recommends -y install xrdp xorgxrdp thunar 


wget -O /etc/xrdp/new.bmp https://ssh.surf/install/XRDP/new.bmp

mv /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak

wget -O /etc/xrdp/xrdp.ini https://ssh.surf/install/XRDP/xrdp.ini


echo xfce4-session > ~/.xsession

hostname=$(hostname)

echo "Grabbing a port from your container from our API"
port=$(curl -s  https://findport.ssh.surf/?id=$hostname)
service xrdp start
sleep 3
relayPort 3389
sleep 5
url=$(grep -hPo "(?<=listening ).*" /root/.pm2/logs/Relay-3389-out.log  | head -n 1 | sed 's/$/.virt.fun/')


echo "To connect use the topic: $hostname-3389"
notif "Use the client as decribed on the wiki for connection"