#!/bin/bash
#
# The Qubes OS Project, http://www.qubes-os.org
#
# Copyright (C) 2011  Tomasz Sterna <tomek@xiaoka.com>
#
# 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 program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
#
#

# Switch user to 'user'
[[ ! -f /etc/this-is-dvm && "$USER" != "user" ]] && exec su user -c $0

loginctl activate "$XDG_SESSION_ID"

if [ -x /usr/sbin/xenstore-read ]; then
        XENSTORE_READ="/usr/sbin/xenstore-read"
else
        XENSTORE_READ="/usr/bin/xenstore-read"
fi

# Set desktop background
xsetroot -solid white

# Save user environment
umask 0077
rm -f /tmp/qubes-session-env

if [ ! -f /etc/this-is-dvm ]; then

env | grep -v "^\(PWD\|TERM\|SHELL\|RUNLEVEL\|PATH\|SHLVL\|LOGNAME\|USER\|MEM\|_\)=" > /tmp/qubes-session-env

# Process XDG Autostart files
shopt -s nullglob
set -a

# Get VM type from XenStore
VMTYPE=`$XENSTORE_READ qubes-vm-type`

UPDTYPE=`$XENSTORE_READ qubes-vm-updateable`
[[ $UPDTYPE == 'True' ]] && UPDTYPE="UpdateableVM" || UPDTYPE="NonUpdateableVM"

# Gnome keyring is special case, as its env needed by nm-applet
if [ -x /usr/bin/gnome-keyring-daemon ]; then
    eval `/usr/bin/gnome-keyring-daemon --start | tee -a /tmp/qubes-session-env`
fi

for D in /etc/xdg/autostart/*.desktop; do
	MATCH="\(QUBES\|$VMTYPE\|$UPDTYPE\)"
	[[ -n "$XDG_CONFIG_DIRS" ]] && [[ -d "$XDG_CONFIG_DIRS/autostart" ]] && USER_AUTOSTART="$XDG_CONFIG_DIRS/autostart" || USER_AUTOSTART="$HOME/.config/autostart"
	[[ -r "$USER_AUTOSTART/${D##*/}" ]] && D="$D $USER_AUTOSTART/${D##*/}"
	# Skip Terminal, Hidden, NotShowIn=QUBES and ! OnlyShowIn=QUBES
	[[ -n "`sed -n "/^Terminal.*[tT1]/p;/^Hidden.*[tT1]/p;/^X-GNOME-Autostart-enabled.*[fF0]/p;/^NotShowIn.*=.*$MATCH/p;/^OnlyShowIn/{s/.*$MATCH.*//;p}" $D`" ]] && continue
	# Extract Exec= line
	EXEC="`sed -n '/^Exec[ \t=]/s/^[^=]\+=[ \t]*//p' $D | tail -n1`"
	[[ -z "$EXEC" ]] && continue
	if grep -qiL ^Terminal=false $D; then
		$EXEC &
	else
		# Append variables to qubes-session-env
		$EXEC | grep '^[^ ]*=[^ ]*$' >> /tmp/qubes-session-env &
	fi
done

unset EXEC MATCH UPDTYPE D USER_AUTOSTART

else # ! DispVM
	# DispVM do not load system xinitrc, so manually load selected services

	# Set up i18n environment
	if [ -r /etc/profile.d/lang.sh ]; then
		. /etc/profile.d/lang.sh
	fi

	[ -r $HOME/.profile ] && . $HOME/.profile

	if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
		eval `dbus-launch --sh-syntax --exit-with-session`
	fi

	[ -x /usr/bin/xhost ] && [ -x /usr/bin/id ] &&
		xhost +si:localuser:`id -un` >& /dev/null
	# start pulseaudio in DispVM (normally done from /etc/xdg/autostart)
	/usr/bin/start-pulseaudio-with-vchan

	# and save environment
	env | grep -v "^\(PWD\|TERM\|SHELL\|RUNLEVEL\|PATH\|SHLVL\|LOGNAME\|USER\|MEM\|_\)=" > /tmp/qubes-session-env
fi # DispVM

if [ -f /etc/X11/xinit/xinitrc.d/qubes-keymap.sh ]; then
	. /etc/X11/xinit/xinitrc.d/qubes-keymap.sh
fi
if [ -f /etc/X11/Xsession.d/90qubes-keymap ]; then
	. /etc/X11/Xsession.d/90qubes-keymap
fi

# Wake waiting for environment processes
[ -s /tmp/qubes-session-waiter ] && kill -KILL `cat /tmp/qubes-session-waiter`

# Set desktop background
xsetroot -solid white

# Wait some time to pick up zombie children
wait

# Now hang myself so Xorg may live :'(
# ____
# |/ |
# |  O
# | /|\
# | / \
# |
kill -STOP $$
