#!/bin/sh

export XDG_DATA_DIRS="/usr/local/share:/usr/share/raspi-ui-overrides:/usr/share:/usr/share/gdm:/var/lib/menu-xdg"

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-pi"
if [ ! -f "$PCMANFM_CONF_DIR/pcmanfm.conf" ]; then
  mkdir -p "$PCMANFM_CONF_DIR"
  cp /etc/xdg/pcmanfm/LXDE-pi/pcmanfm.conf "$PCMANFM_CONF_DIR/pcmanfm.conf"
fi
if [ ! -f "$PCMANFM_CONF_DIR/desktop-items-0.conf" ]; then
  mkdir -p "$PCMANFM_CONF_DIR"
  cp /etc/xdg/pcmanfm/LXDE-pi/desktop-items-0.conf "$PCMANFM_CONF_DIR/desktop-items-0.conf"
fi

# Ensure the existance of openbox config file
OPENBOX_CONF_DIR="$XDG_CONFIG_HOME/openbox"
if [ ! -f "$OPENBOX_CONF_DIR/lxde-pi-rc.xml" ]; then
  mkdir -p "$OPENBOX_CONF_DIR"
  cp /etc/xdg/openbox/lxde-pi-rc.xml "$OPENBOX_CONF_DIR/lxde-pi-rc.xml"
fi

# Copy PiX theme into home directory
THEME_DIR="$HOME/.themes"
if [ ! -d "$THEME_DIR/PiX" ]; then
  mkdir -p "$THEME_DIR"
  cp /usr/share/themes/PiX $HOME/.themes/ -a
fi

# Copy gtk.css and settings.ini (required for GTK3 themes to work correctly)
if [ ! -f "$XDG_CONFIG_HOME/gtk-3.0/gtk.css" ]; then
  mkdir -p "$XDG_CONFIG_HOME/gtk-3.0"
  cp /usr/share/raspi-ui-overrides/gtk.css "$XDG_CONFIG_HOME/gtk-3.0/"
fi
if [ ! -f "$XDG_CONFIG_HOME/gtk-3.0/settings.ini" ]; then
  mkdir -p "$XDG_CONFIG_HOME/gtk-3.0"
  cp /usr/share/raspi-ui-overrides/settings.ini "$XDG_CONFIG_HOME/gtk-3.0/"
fi

# Ensure Qt themes are consistent with GTK themes
if [ ! -f "$XDG_CONFIG_HOME/Trolltech.conf" ]; then
  cp /usr/share/raspi-ui-overrides/Trolltech.conf "$XDG_CONFIG_HOME/"
fi

# Ensure the existance of lxpanel config files
LXPANEL_CONF_DIR="$XDG_CONFIG_HOME/lxpanel"
if [ ! -e "$LXPANEL_CONF_DIR/LXDE-pi" ]; then
  mkdir -p "$LXPANEL_CONF_DIR"
  cp /etc/xdg/lxpanel/launchtaskbar.cfg "$LXPANEL_CONF_DIR"
  cp -r /etc/xdg/lxpanel/profile/LXDE-pi "$LXPANEL_CONF_DIR"
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-pi-"

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