#!/bin/bash
#
# install-smsupport-deb.sh
# Installs GNOME with a dummy X display and Studio Manager Remote Support package
# Tested on Ubuntu (headless / server)
#

set -e

echo "=== Updating system packages ==="
sudo apt update -y

echo "=== Configuring and installing GNOME components ==="
sudo DEBIAN_FRONTEND=noninteractive dpkg-reconfigure gdm3 || true
sudo apt install -y gdm3 gnome-shell gnome-session xorg xwayland
sudo apt install -y xserver-xorg-video-dummy

echo "=== Creating dummy X display configuration ==="
sudo mkdir -p /etc/X11/xorg.conf.d
sudo tee /etc/X11/xorg.conf.d/10-dummy.conf >/dev/null <<'EOF'
Section "Device"
    Identifier "DummyDevice"
    Driver "dummy"
    VideoRam 256000
EndSection
Section "Monitor"
    Identifier "DummyMonitor"
    HorizSync 28.0-80.0
    VertRefresh 48.0-75.0
    Modeline "1920x1080" 148.50 1920 2008 2052 2200 1080 1084 1089 1125 +hsync +vsync
EndSection
Section "Screen"
    Identifier "DummyScreen"
    Device "DummyDevice"
    Monitor "DummyMonitor"
    DefaultDepth 24
    SubSection "Display"
        Depth 24
        Modes "1920x1080"
    EndSubSection
EndSection
EOF

#echo "=== Restarting GDM service ==="
#sudo systemctl restart gdm3
echo "Disabling sleep and suspend"

sudo systemctl mask sleep.target suspend.target hibernate.target hybrid-sleep.target

echo "=== Installing Studio Manager Remote Support package ==="
cd /tmp
wget -O studio-manager-deb-remote-support-x86_64.deb \
  http://go.al.sg/deb/studio-manager-deb-remote-support-x86_64.deb

sudo dpkg -i studio-manager-deb-remote-support-x86_64.deb || true
sudo apt install -f -y

echo "=== Enabling Studio Manager Remote Support service ==="
sudo systemctl enable studio-manager-deb-remote-support

echo "=== Installation complete. ==="

