#!/bin/dash
# personal gnome-session script (for gnome-session version 2.32)
# by thomas hisch <thomas@opentech.at>

# if something with the gnome-session does go wrong look at .xsession-errors in your homedir

MYWM=$1
SHOW_DESKTOP=$2

SESSKEY="/desktop/gnome/session"
GSETTOOL="/usr/bin/gconftool-2"

# Load resources
if [ -f $HOME/.Xresourses ]; then
  xrdb -merge $HOME/.Xresources
fi
if [ -f $HOME/.Xdefaults ]; then
  xrdb -merge $HOME/.Xdefaults
fi

# TODO: when is it better to start apps here and when is it better to
# put the apps in .config/autostart and start it using a session
# manager (gnome-session)

# Set the background color
#/usr/bin/xsetroot -solid black
pgrep -u $USER urxvtd > /dev/null || urxvtd -o -f &

#The following line is now in gnome-session
#pgrep -u $USER emacs > /dev/null || emacs --daemon &

# Update the Gnome configuration to reflect WM and desktop appearance
# IMPORTANT: $MYWM should have a valid .desktop file in
# /usr/share/applications or in .local/share/applications (for other
# dirs see log of `gnome-session --debug`) otherwise gsm will complain
# about it

$GSETTOOL -t string  -s $SESSKEY/required_components/windowmanager ${MYWM}
$GSETTOOL -t boolean -s /apps/nautilus/preferences/show_desktop  ${SHOW_DESKTOP}

if [ "$MYWM" = "awesome" ]
then
    $GSETTOOL -s -t list --list-type string $SESSKEY/required_components_list "[windowmanager]"
    $GSETTOOL -s -t list --list-type string $SESSKEY/default_session "[gnome-settings-daemon]"
else
    $GSETTOOL -s -t list --list-type string $SESSKEY/required_components_list "[filemanager,panel,windowmanager]"
    $GSETTOOL -s -t list --list-type string $SESSKEY/default_session "[gnome-settings-daemon]"
fi

export WINDOW_MANAGER=/usr/bin/$1

#if something does not work as expected start gnome-session with --debug and look into .xsession-errors
#-a starts only the *.desktop files in the specified dir instead of the WM  (ex: -a /home/thomas/dumm)
#exec strace -fF -o /tmp/gnome-session-trace /usr/bin/gnome-session
exec /usr/bin/gnome-session
