#!/bin/bash
#
# This file is part of Hawaii Shell.
#
# Copyright (C) 2013 Pier Luigi Fiorini <pierluigi.fiorini@gmail.com>
#
# Author(s):
#    Pier Luigi Fiorini
#
# $BEGIN_LICENSE:GPL2+$
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
# $END_LICENSE$
#

uxtype=$1

test -n "$uxtype" || {
    #echo "Unable to determine which UX to run" 1>&2
    #exit 1
    uxtype="desktop"
}

executable=/usr/libexec/hawaii-shell-client
build_type=RelWithDebInfo

# Make sure our library path is set
_libdir="/usr/lib64"
if test -n ""; then
    _libdir="$_libdir:"
fi
if test -n "$LD_LIBRARY_PATH"; then
    LD_LIBRARY_PATH="$_libdir:$LD_LIBRARY_PATH"
else
    LD_LIBRARY_PATH="$_libdir"
fi
export LD_LIBRARY_PATH

# Make sure we find Hawaii executables
_bindir="/usr/bin"
if test -n ""; then
    _bindir="$_bindir:"
fi
if test -n "$PATH"; then
    PATH="$_bindir:$PATH"
else
    PATH="$_bindir:/usr/local/bin:/bin:/usr/bin:/usr/local/sbin:/sbin:/usr/sbin"
fi
export PATH

# Set the path for Qt plugins provided by us
if test -n "$QT_PLUGIN_PATH"; then
    QT_PLUGIN_PATH="$QT_PLUGIN_PATH:/usr/lib64/hawaii/plugins"
else
    QT_PLUGIN_PATH="/usr/lib64/hawaii/plugins"
fi
export QT_PLUGIN_PATH

# Set the path for QML 2 imports provided by us
if test -n "$QML2_IMPORT_PATH"; then
    QML2_IMPORT_PATH="$QML2_IMPORT_PATH:/usr/lib64/hawaii/qml"
else
    QML2_IMPORT_PATH="/usr/lib64/hawaii/qml"
fi
export QML2_IMPORT_PATH

# Make sure data directories are set
if test -n "$XDG_DATA_DIRS"; then
    XDG_DATA_DIRS="/usr/share:$XDG_DATA_DIRS"
else
    XDG_DATA_DIRS="/usr/share:/usr/share:/usr/local/share"
fi
export XDG_DATA_DIRS

# Set desktop environment name for Qt platform themes detection,
# XDG scripts and OnlyShowIn= implementation
export XDG_CURRENT_DESKTOP=Hawaii

# QtQuick Controls style
export QT_QUICK_CONTROLS_STYLE=Aluminium

# Cursor theme
if test -n "$XCURSOR_PATH"; then
    XCURSOR_PATH="$XCURSOR_PATH:/usr/share/icons"
else
    XCURSOR_PATH="~/.icons:/usr/share/icons:/usr/share/pixmaps:~/.cursors:/usr/share/cursors/xorg-x11:/usr/share/icons"
fi
export XCURSOR_PATH
export XCURSOR_THEME=hawaii
export XCURSOR_SIZE=16

# Force wayland backend for both Qt and GTK+
export QT_QPA_PLATFORM=wayland
export GDK_BACKEND=wayland

# Launch a session bus instance
if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && type dbus-launch >/dev/null; then
    eval `dbus-launch --auto-syntax --exit-with-session`
fi

# Check whether D-Bus is OK
if ! qdbus >/dev/null; then
    echo "Could not start D-Bus session bus" 1>&2
    exit 1
fi

# Start the right shell
args=""
if test -n "$HAWAII_SHELL_DEBUG_PORT"; then
    args="-qmljsdebugger=port:${HAWAII_SHELL_DEBUG_PORT},block -d"
fi
if [ "$build_type" = "Debug" ]; then
    exec $executable $args
else
    exec catchsegv $executable $args
fi
