#!/bin/bash

### Setup VNC on an Oracle Linux instance

echo "Installing Graphical Desktop and TigerVNC"
sudo dnf groupinstall -y "Server with GUI"
sudo systemctl set-default graphical
if ! grep '^WaylandEnable=false' /etc/gdm/custom.conf; then sudo sed -i '/^\[daemon\]/a WaylandEnable=false' /etc/gdm/custom.conf; fi
sudo dnf install -y tigervnc-server tigervnc-server-module

echo "Setting up access for the $(whoami) user. You will now be prompted for a VNC Password, which you should verify. This password should be different from your usual user password!"
vncpasswd
echo ":1=$(whoami)"| sudo tee -a /etc/tigervnc/vncserver.users > /dev/null
printf 'session=gnome\ngeometry=1280x1024' | sudo tee -a /etc/tigervnc/vncserver-config-defaults > /dev/null

# These steps are not required for vnc server persistence
#sudo mkdir -p /etc/systemd/system/vncserver@.service.d/
#printf '[Service]\nRestart=always' | sudo tee /etc/systemd/system/vncserver@.service.d/10-restart.conf > /dev/null

echo "Restarting the service"
sudo systemctl daemon-reload
sudo systemctl enable --now vncserver@:1.service

echo "VNC should now be set up and running. On a remote host you should set up an SSH tunnel to access port 5901 on this host and then configure your VNC client to access port 5901 on your localhost."
echo "For example: ssh -L 5901:localhost:5901 $(whoami)@this.host.example.com"

