#!/bin/sh

contains()
{
    local str="$1" substr="$2"
    [ "$str" = "$substr" -o -z "${str##$substr:*}" -o -z "${str##*:$substr:*}" -o -z "${str%%*:$substr}" ]
}

if [ -z "$XDG_DATA_HOME" ]; then
    export XDG_DATA_HOME="$HOME/.local/share"
fi

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

if [ -z "$XDG_DATA_DIRS" ]; then
    XDG_DATA_DIRS="$XDG_DATA_HOME:/usr/local/share:/usr/share"
else
    if ! contains "$XDG_DATA_DIRS" "/usr/share"; then
        XDG_DATA_DIRS="$XDG_DATA_DIRS:/usr/share"
    fi
fi
export XDG_DATA_DIRS

if [ -z "$XDG_CONFIG_DIRS" ]; then
    export XDG_CONFIG_DIRS="/etc/xdg"
else
    if ! contains "$XDG_CONFIG_DIRS" '/etc/xdg'; then
        XDG_CONFIG_DIRS="$XDG_CONFIG_DIRS:/etc/xdg"
    fi
fi

if [ -z "$XDG_CACHE_HOME" ]; then
    export XDG_CACHE_HOME="$HOME/.cache"
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 Qt integration for OpenOffice.org, if available.
if [ -z "$SAL_USE_VCLPLUGIN" ]; then
    export SAL_USE_VCLPLUGIN=kde4
fi

# Launch DBus if needed
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
    if [ -z "$XDG_RUNTIME_DIR" ] || ! [ -S "$XDG_RUNTIME_DIR/bus" ] || ! [ -O "$XDG_RUNTIME_DIR/bus" ]; then
        eval "$(dbus-launch --sh-syntax --exit-with-session)" || echo "startlxqt: error executing dbus-launch" >&2
    fi
fi

# Copy default settings of openbox
if [ ! -e "$XDG_CONFIG_HOME/openbox/lxqt-rc.xml" ] ; then
    ob_config_copied=0
    for considered_file in 'lxde-rc.xml' 'rc.xml'; do
        if [ -e "$XDG_CONFIG_HOME/openbox/$considered_file" ]; then
            #copy existing configuration of openbox
            cp "$XDG_CONFIG_HOME/openbox/$considered_file" "$XDG_CONFIG_HOME/openbox/lxqt-rc.xml"
            message="Your existing configuration for openbox '$XDG_CONFIG_HOME/openbox/$considered_file' was used to
fill the LXQt's openbox configuration '$XDG_CONFIG_HOME/openbox/lxqt-rc.xml'.
If you want to use the predefined LXQt's openbox configuration, run:
  cp '/etc/xdg/openbox/lxqt-rc.xml' '$XDG_CONFIG_HOME/openbox'"
            echo "$message" >&2
            xmessage -center -title "LXQt Openbox configuration" "$message" &
            ob_config_copied=1
            break
        fi
    done
    if [ 0 -eq "$ob_config_copied" ]; then
        #copy predefined configuration
        mkdir -p "$XDG_CONFIG_HOME/openbox"
        #user/distribution can change the default configuration via LXQT_DEFAULT_OPENBOX_CONFIG
        [ -r "$LXQT_DEFAULT_OPENBOX_CONFIG" ] || LXQT_DEFAULT_OPENBOX_CONFIG='/etc/xdg/openbox/lxqt-rc.xml'
        cp "$LXQT_DEFAULT_OPENBOX_CONFIG" "$XDG_CONFIG_HOME/openbox"
    fi
fi

# Qt4 platform plugin
export QT_PLATFORM_PLUGIN=lxqt

# Qt5 platform plugin
export QT_QPA_PLATFORMTHEME=qt5ct

# use lxqt-applications.menu for main app menu
export XDG_MENU_PREFIX="lxqt-"

export XDG_CURRENT_DESKTOP="LXQt"

# Start the LXQt session
exec lxqt-session