#!/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.
#
#

loginctl activate "$XDG_SESSION_ID"

# Set desktop background
xsetroot -solid white

rm -f /tmp/qubes-session-env

export QUBES_ENV_SOURCED=1

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

# Get VM type from XenStore
VMTYPE=`/usr/bin/qubesdb-read /qubes-vm-type`

UPDTYPE=`/usr/bin/qubesdb-read /qubes-vm-updateable`
[[ $UPDTYPE == 'True' ]] && UPDTYPE="UpdateableVM" || UPDTYPE="NonUpdateableVM"

# process /etc/xdg/autostart and friends (according to Desktop Application
# Autostart Specification)
/usr/bin/qubes-session-autostart QUBES X-QUBES "X-$VMTYPE" "X-$UPDTYPE"

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

# Save user environment
(
    umask 0077
    unset PWD TERM SHELL RUNLEVEL PATH SHLVL LOGNAME USER MEM
    export -p > /tmp/qubes-session-env.tmp
)

cd $HOME

if [ -x /usr/bin/qrexec-fork-server ]; then
    /usr/bin/qrexec-fork-server
fi

# Environment ready, make it available to processes
mv /tmp/qubes-session-env.tmp /tmp/qubes-session-env

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

# Wait some time to pick up zombie children
wait

# Now hang myself so Xorg may live :'(
# ____
# |/ |
# |  O
# | /|\
# | / \
# |
env sleep inf
