#!/bin/bash

function checkPkg {
  # verify that xephyr is installed, if not install
  dpkg --status $1 &> /dev/null
  if [ "$?" -ne "0" ]; then
  	echo "Required package $1 not installed."
  	echo "Attempting to install..."
        # verify running as root
  	if [ "$UID" -ne "0" ]; then
  		echo "[ERROR] Can't install $1 package. No root priveleges"
  		exit 1
  	fi
  	apt-get install $1 
  	if [ "$?" -ne "0" ]; then
  		echo "[ERROR] Failed to run: apt-get install $1"
  		exit 1
  	else
  		echo "Successfully installed $1"
  	fi
  fi;
}

checkPkg "xserver-xephyr"
checkPkg "xinit"

echo "Setting DISPLAY=:0"
export DISPLAY=:0
echo "Starting dbus"
/etc/init.d/dbus start

echo "Starting UI in Xephyr"
xinit /etc/X11/xinit/xinitrc -- /usr/bin/Xephyr :2 -host-cursor -screen 1024x600x32 -dpi 96 -ac

if [ "$?" -ne "0" ]; then
	echo ""
	echo "It appears that the UI or Xephyr did not start"
	echo "Note 1: Make sure that dbus is started successfully"
	echo "Note 2: Make sure you have enabled control access on DISPLAY :0"
	echo " Example (run on workstation terminal):  xhost +SI:localuser:root"
	echo ""
fi


