#!/usr/bin/sh

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

if [ -z "$XDG_DATA_HOME" ]; then
	export XDG_DATA_HOME="$HOME/.local/share"
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 second 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/org.lxde.home.desktop
		xdg-desktop-icon install --novendor /usr/share/dist/desktop-files/default/register.desktop
	fi
	# Ensure the existance of openbox config file:
	mkdir -p "$OPENBOX_CONF_DIR"
	cp /etc/xdg/openbox/LXDE/rc.xml "$OPENBOX_CONF_DIR/lxde-rc.xml"
fi

# Ensure the existance of lxpanel config file
LXPANEL_CONF_DIR="$XDG_CONFIG_HOME/lxpanel/LXDE"
if [ ! -f "$LXPANEL_CONF_DIR/config" ]; then
	mkdir -p "$LXPANEL_CONF_DIR"
	cp /etc/xdg/lxpanel/LXDE/config "$LXPANEL_CONF_DIR/config"
fi
if [ ! -f "$LXPANEL_CONF_DIR/panels/panel" ]; then
	mkdir -p "$LXPANEL_CONF_DIR/panels"
	cp /etc/xdg/lxpanel/LXDE/panels/panel "$LXPANEL_CONF_DIR/panels/panel"
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"

# Ensure the existance of the user applications folder
USER_APPS_DIR="$XDG_DATA_HOME/applications"
mkdir -p "$USER_APPS_DIR"

# add the following line to hide desktop files in the desktop that belong to other DE
hide-desktop-file.sh

# 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
	if test -z "$XDG_RUNTIME_DIR" -o ! -S "$XDG_RUNTIME_DIR/bus" -o ! -O "$XDG_RUNTIME_DIR/bus"; then
		eval "$(dbus-launch --sh-syntax --exit-with-session)"
	fi
fi

export XDG_MENU_PREFIX=""
export XDG_CURRENT_DESKTOP="LXDE"

# Provide a solution for bug mga#14622
setxkbmap

## Migration from previous versions of Mageia
#if [ ! -f "$XDG_CONFIG_HOME/.mga_lxde_migration" ]; then
#	touch "$XDG_CONFIG_HOME/.mga_lxde_migration"
#	# 1. Solution for the problem of empty menu
#	if [ -f "$XDG_CONFIG_HOME/lxsession/LXDE/desktop.conf" ]; then
#		wrong=$(grep -- "^menu_prefix=lxde-$" "$XDG_CONFIG_HOME/lxsession/LXDE/desktop.conf")
#		if [ -n "$wrong" ]; then
#			sed -i "s/^\(menu_prefix=\)lxde-$/\1/" "$XDG_CONFIG_HOME/lxsession/LXDE/desktop.conf"
#		fi
#	fi
#	# 2. Prevent having two trash icons on the desktop (mga#14210)
#	if [ -f "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf" ]; then
#		wrong=$(grep "^show_trash=0$" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf")
#		wrong2=""
#		if [ -f "$XDG_DESKTOP_DIR/trash.desktop" ]; then
#			wrong2=$(grep "^OnlyShowIn=LXDE\;$" "$XDG_DESKTOP_DIR/trash.desktop")
#		fi
#		if [ -n "$wrong" -a \( -f "$XDG_DESKTOP_DIR/org.lxde.trash.desktop" -o -n "$wrong2" \) ]; then
#			sed -i "s/^\(show_trash=\)0$/\11/" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf"
#			if [ -f "$XDG_DESKTOP_DIR/org.lxde.trash.desktop" ]; then
#				rm -f "$XDG_DESKTOP_DIR/org.lxde.trash.desktop"
#				wrong=$(grep "^\[org.lxde.trash.desktop]$" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf")
#				if [ -n "$wrong" ]; then
#					sed -i "s/^\[org.lxde.trash.desktop]$/\[trash:\/\/\/]/" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf"
#				fi
#			fi
#			if [ -n "$wrong2" ]; then
#				rm -f "$XDG_DESKTOP_DIR/trash.desktop"
#				wrong=$(grep "^\[trash.desktop]$" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf")
#				if [ -n "$wrong" ]; then
#					sed -i "s/^\[trash.desktop]$/\[trash:\/\/\/]/" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf"
#				fi
#			fi
#		fi
#	fi
#	# 3. Make sure the "Home" icon on the desktop is the good one
#	if [ -f "$XDG_DESKTOP_DIR/home.desktop" ]; then
#		wrong=$(grep "^OnlyShowIn=LXDE\;$" "$XDG_DESKTOP_DIR/home.desktop")
#		if [ -n "$wrong" ]; then
#			rm -f "$XDG_DESKTOP_DIR/home.desktop"
#			xdg-desktop-icon install --novendor /var/lib/mageia/lxde-profiles/Desktop-icons/org.lxde.home.desktop
#			wrong=$(grep "^\[home.desktop]$" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf")
#			if [ -n "$wrong" ]; then
#				sed -i "s/^\[\(home.desktop]\)$/\[org.lxde.\1/" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf"
#			fi
#		fi
#	fi
#	# 4. Use the new icon for the menu (mga#18579)
#	if [ -f "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel" ]; then
#		wrong=$(grep "/usr/share/icons/mageia.png" "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel")
#		if [ -n "$wrong" ]; then
#			sed -i "s/\(\/usr\/share\/icons\/\)mageia.png/\1hicolor\/scalable\/apps\/mageia-menu.svg/" "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel"
#		fi
#	fi
#	# 5. Set the default font size to 11 on desktop (mga#17905)
#	if [ -f "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf" ]; then
#		wrong=$(grep "^desktop_font=Sans 12$" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf")
#		if [ -n "$wrong" ]; then
#			sed -i "s/^\(desktop_font=Sans 1\)2$/\11/" "$XDG_CONFIG_HOME/pcmanfm/LXDE/desktop-items-0.conf"
#		fi
#	fi
#	# 6. The plugin volumealsa is replaced by volume
#	if [ -f "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel" ]; then
#		wrong=$(grep "/usr/share/icons/mageia.png" "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel")
#		if [ -n "$wrong" ]; then
#			sed -i "s/\(type=volume\)alsa$/\1/" "$XDG_CONFIG_HOME/lxpanel/LXDE/panels/panel"
#		fi
#	fi
#fi
#
# Start the LXDE session
exec /usr/bin/lxsession -s LXDE -e LXDE
