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

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

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

# 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"

# 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.tmp`
fi

################################################################################
# XDG Base Directory Specification
# http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html
#
# $XDG_CONFIG_HOME defines the base directory relative to which user specific
# configuration files should be stored. If $XDG_CONFIG_HOME is either not set
# or empty, a default equal to $HOME/.config should be used.
#
# $XDG_CONFIG_DIRS defines the preference-ordered set of base directories to
# search for configuration files in addition to the $XDG_CONFIG_HOME base 
# directory. The directories in $XDG_CONFIG_DIRS should be seperated with a 
# colon ':'.
#
# If $XDG_CONFIG_DIRS is either not set or empty, a value equal to /etc/xdg 
# should be used.
#
# The order of base directories denotes their importance; the first directory 
# listed is the most important. When the same information is defined in multiple
# places the information defined relative to the more important base directory
# takes precedent. The base directory defined by $XDG_DATA_HOME is considered 
# more important than any of the base directories defined by $XDG_DATA_DIRS. The
# base directory defined by $XDG_CONFIG_HOME is considered more important than
# any of the base directories defined by $XDG_CONFIG_DIRS. 
################################################################################

export XDG_CONFIG_DIRS="${XDG_CONFIG_DIRS:-/etc/xdg}"
XDG_CONFIG_QUBES="/usr/share/qubes/xdg"
[[ "${XDG_CONFIG_DIRS}" =~ ${XDG_CONFIG_QUBES} ]] || {
	XDG_CONFIG_DIRS="${XDG_CONFIG_QUBES}:${XDG_CONFIG_DIRS}"
}
export XDG_CONFIG_HOME="${XDG_CONFIG_HOME:-"$HOME/.config"}"
XDG_CONFIG_ALL="${XDG_CONFIG_HOME}:${XDG_CONFIG_DIRS}"

declare -A CACHE
for D in $(find ${XDG_CONFIG_ALL//:/\/autostart }/autostart -name "*.desktop" -print 2> /dev/null); do
	# Skip if already in cache since higher priority directory already executed it
	[[ ${CACHE[${D##*/}]+_} ]] && continue
	
	# Add $D to cache and follow though to execute
	CACHE[${D##*/}]=1

	MATCH="\(QUBES\|$VMTYPE\|$UPDTYPE\)"

	# 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.tmp &
	fi
done

unset EXEC MATCH UPDTYPE D XDG_CONFIG_QUBES XDG_CONFIG_ALL CACHE

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

# Set desktop background
xsetroot -solid white

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
# | /|\
# | / \
# |
while sleep 365d; do true; done
