#!/bin/bash

#
# Point to the actual application executable
#
realpath=`readlink -f "$0"`
directory=`dirname "$realpath"`

if [ -z "$LD_LIBRARY_PATH" ]; then
	export LD_LIBRARY_PATH="$directory/../libs_linux64"
else
	export LD_LIBRARY_PATH="$directory/../libs_linux64:$LD_LIBRARY_PATH"
fi

#20.04 to 22.04 has a hard break in ssl versions
#so check if wb_command runs, and if not, add it
if ! "$directory"/../exe_linux64/wb_command -completion &> /dev/null
then
    export LD_LIBRARY_PATH="$LD_LIBRARY_PATH:$directory/../libs_linux64/focal-ssl3"
fi

export QT_PLUGIN_PATH="$directory/../libs_linux64/plugins"

#allow simple override of the theming detection
if [[ -v WORKBENCH_PLATFORMTHEME ]]
then
    export QT_QPA_PLATFORMTHEME="$WORKBENCH_PLATFORMTHEME"
else
    #don't use the qt6ct plugin unless the user has a qt6ct config folder, in case qt6ct is responsible for some crashes
    if [[ -v XDG_CONFIG_HOME && "$XDG_CONFIG_HOME" == /* ]]
    then
        confdir="$XDG_CONFIG_HOME"/qt6ct
    else
        confdir=~/.config/qt6ct
    fi
    #support dark mode (if user has configured qt6ct)
    #dbus session management sets this on login, so it will probably never be unset
    #but that refers to something in the system qt, not our bundled qt
    #so, blow it away when we see a qt6ct config dir
    if [[ -d "$confdir" ]]
    then
        export QT_QPA_PLATFORMTHEME=qt6ct
    fi
fi

#QT6 wants UTF-8 locale and will complain bitterly
if [[ "${LC_ALL:-}" != "" && "${LC_ALL:-}" != *.UTF-8 ]]
then
    LC_ALL=C.UTF-8
fi

"$directory"/../exe_linux64/wb_view "$@"
exit $?
