#!/bin/bash

# Copyright (C) 2011-2017 Klaus Ade Johnstad <klaus@linuxavdelingen.no>
# Copyright (C) 2017 Mike Gabriel <mike.gabriel@das-netzwerkteam.de>

# This program is free software; you can redistribute it
# and/or modify it under the terms of the GNU General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later
# version.
#
# This program is distributed in the hope that it will be
# useful, but WITHOUT ANY WARRANTY; without even the implied
# warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
# PURPOSE.  See the GNU General Public License for more
# details.
#
# You should have received a copy of the GNU General Public
# License along with this package; if not, write to the Free
# Software Foundation, Inc., 51 Franklin St, Fifth Floor,
# Boston, MA  02110-1301 USA

AUTOLOAD_DISPLAY=":20"
DESKTOP_SESSION_MANAGER="x-session-manager"

test -f /etc/default/desktop-autoloader && . /etc/default/desktop-autoloader

###
### input sanitizing...
###
if ! echo "${AUTOLOAD_DISPLAY}" | grep -qE "^.*:[0-9]+(|\.[0-9]+)"; then
	echo "Inappropriate setting for \$AUTOLOAD_DISPLAY variable: ${AUTOLOAD_DISPLAY}"
	exit 1
fi
if ! type -p "${DESKTOP_SESSION_MANAGER}" 1>/dev/null; then
	echo "Cannot find session manager \$DESKTOP_SESSION_MANAGER: ${DESKTOP_SESSION_MANAGER}"
	exit 1
fi

# launch an invisible X-Server
Xvfb "${AUTOLOAD_DISPLAY}" &

# launch a session inside this Xserver
export DISPLAY="${AUTOLOAD_DISPLAY}"
STARTUP="${DESKTOP_SESSION_MANAGER}" dbus-run-session /etc/X11/Xsession
