#!/bin/sh

if [ -z "$XDG_CONFIG_HOME" ]; then
	export XDG_CONFIG_HOME="$HOME/.config"
fi

# Ensure the existance of pcmanfm config file
PCMANFM_CONF_DIR="$XDG_CONFIG_HOME/pcmanfm/LXDE"
if [ ! -f "$PCMANFM_CONF_DIR/pcmanfm.conf" ]; then
	mkdir -p "$PCMANFM_CONF_DIR"
	cp /etc/xdg/pcmanfm/default/pcmanfm.conf "$PCMANFM_CONF_DIR/pcmanfm.conf"
fi

# Ensure the existance of Mageia Desktop icons at first run:
# Logic is simple, $OPENBOX_CONF_DIR/lxde-rc.xml will be created
# after first desktop files created. So, this code block will attempt
# to detect $OPENBOX_CONF_DIR/lxde-rc.xml. If we run lxde session
# for the first time there will be no $OPENBOX_CONF_DIR/lxde-rc.xml.
# Then, this code block will create desktop icons and next code 
# will create $OPENBOX_CONF_DIR/lxde-rc.xml file. At secon login and afterwards, 
# this code block will detect $OPENBOX_CONF_DIR/lxde-rc.xml and will do
# nothing. Of course, if user prefers to delete $OPENBOX_CONF_DIR/lxde-rc.xml
# this code block may give us a little headache. This should fix mga#5941
OPENBOX_CONF_DIR="$XDG_CONFIG_HOME/openbox"
if [ ! -f "$OPENBOX_CONF_DIR/lxde-rc.xml" ]; then
    if [ -d /var/lib/mageia/lxde-profiles/Desktop-icons ]; then
        xdg-desktop-icon install --novendor /var/lib/mageia/lxde-profiles/Desktop-icons/trash.desktop
        xdg-desktop-icon install --novendor /var/lib/mageia/lxde-profiles/Desktop-icons/home.desktop
        xdg-desktop-icon install --novendor /var/lib/mageia/lxde-profiles/Desktop-icons/register.desktop
    fi
    # Ensure the existance of openbox config file:
    mkdir -p "$OPENBOX_CONF_DIR"
    cp /usr/share/lxde/openbox/rc.xml "$OPENBOX_CONF_DIR/lxde-rc.xml"
fi

# Ensure the existance of the 'Desktop' folder
if [ -e "$XDG_CONFIG_HOME/user-dirs.dirs" ]; then
	. "$XDG_CONFIG_HOME/user-dirs.dirs"
else
	XDG_DESKTOP_DIR="$HOME/Desktop"
fi
mkdir -p "$XDG_DESKTOP_DIR"

# Clean up after GDM (GDM sets the number of desktops to one)
xprop -root -remove _NET_NUMBER_OF_DESKTOPS -remove _NET_DESKTOP_NAMES -remove _NET_CURRENT_DESKTOP 2> /dev/null

# Enable GTK+2 integration for OpenOffice.org, if available.
export SAL_USE_VCLPLUGIN=gtk

# Launch DBus if needed
if which dbus-launch >/dev/null && test -z "$DBUS_SESSION_BUS_ADDRESS"; then
	eval "$(dbus-launch --sh-syntax --exit-with-session)"
fi

export XDG_MENU_PREFIX="lxde-"

# Start the LXDE session
exec /usr/bin/lxsession -s LXDE -e LXDE
