#!/bin/bash

NVIDIA_DRIVER=410.79
VIRTUAL_GL=2.6.1
VDISPLAY_RESOLUSION=1920x1080

set -e

sudo yum update -y
sudo DEBIAN_FRONTEND=noninteractive yum upgrade -yq

# Prepare for NVidia drivers install
sudo yum install -y gcc make pkg-config xserver-xorg-dev linux-headers-$(uname -r) xterm
# xterm is needed for xinit

# Install Lubuntu/Xubuntu/anything
sudo yum -y groupinstall "X Window System"
sudo yum -y install lightdm

# Installing NVidia driver
wget -O NVIDIA-Linux-x86_64-${NVIDIA_DRIVER}.run --no-check-certificate http://us.download.nvidia.com/tesla/${NVIDIA_DRIVER}/NVIDIA-Linux-x86_64-${NVIDIA_DRIVER}.run
chmod +x NVIDIA-Linux-x86_64-${NVIDIA_DRIVER}.run
sudo ./NVIDIA-Linux-x86_64-${NVIDIA_DRIVER}.run --no-questions --accept-license --no-precompiled-interface --ui=none --glvnd-egl-config-path=/usr/share/glvnd/egl_vendor.d
echo ""
echo "************************************************************************************************"
echo "*                                                                                              *"
echo "* May be you see this warning above:                                                           *"
echo "*  - WARNING: Unable to find a suitable destination to install 32-bit compatibility libraries. *"
echo "* This is OK.                                                                                  *"
echo "*                                                                                              *"
echo "************************************************************************************************"
echo ""
rm NVIDIA-Linux-x86_64-${NVIDIA_DRIVER}.run


# Preparation for virtualgl like in https://virtualgl.org/Documentation/HeadlessNV
sudo nvidia-xconfig -a --use-display-device=None --virtual=${VDISPLAY_RESOLUSION}

echo ""
echo "********************************************************************************"
echo "*                                                                              *"
echo "* May be you see this warning above:                                           *"
echo "*  - WARNING: Unable to locate/open X configuration file.                      *"
echo "* This is OK.                                                                  *"
echo "*                                                                              *"
echo "********************************************************************************"
echo ""

# Fix /etc/X11/xorg.conf:
# 1. Add line with BusID in section Device (taken from output of lspci | egrep -h "VGA|3D controller")
# For EC2 g3 and p3 also:
# 2. Delete whole section ServerLayout (comment it with # symbol)
# 3. Delete whole section Screen (comment it with # symbol)
sudo /usr/bin/python2.7 fix_xorg_conf.py /etc/X11/xorg.conf

# Install VirtualGL
wget -O virtualgl.rpm https://downloads.sourceforge.net/project/virtualgl/${VIRTUAL_GL}/VirtualGL-${VIRTUAL_GL}.x86_64.rpm
sudo yum -y install virtualgl.rpm
rm virtualgl.rpm

# Configure VirtualGL
sudo service lightdm stop
sudo systemctl enable lightdm
sudo /opt/VirtualGL/bin/vglserver_config -config +s +f -t

echo ""
echo "********************************************************************************"
echo "*                                                                              *"
echo "* May be you see these lines above:                                            *"
echo "*  - rmmod: ERROR: Module nvidia is in use by: nvidia_modeset                  *"
echo "*  - IMPORTANT NOTE: Your system uses modprobe.d to set device permissions.    *"
echo "* This is OK - just means that reboot required.                                *"
echo "*                                                                              *"
echo "********************************************************************************"
echo ""


# Add Global Environment Variables
sudo sed -i '$ a export DISPLAY=:0.0' /etc/profile


echo ""
echo "******************************************************************"
echo "*                                                                *"
echo "* Rebooting for changes to take effect!                          *"
echo "* Don't forget to execute below command after reboot.            *"
echo "* xauth merge /etc/opt/VirtualGL/vgl_xauth_key                   *"
echo "*                                                                *"
echo "******************************************************************"
echo ""

sudo reboot
