#!/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"

#load the qt6ct theme onto itself, I guess
if [[ -v WORKBENCH_PLATFORMTHEME ]]
then
    export QT_QPA_PLATFORMTHEME="$WORKBENCH_PLATFORMTHEME"
else
    export QT_QPA_PLATFORMTHEME=qt6ct
fi

#try to populate the colors, etc selections in qt6ct
#hopefully this is the correct XDG incantation
#reject relative paths as if it weren't set
if [[ -v XDG_CONFIG_HOME && "$XDG_CONFIG_HOME" == /* ]]
then
    confdir="$XDG_CONFIG_HOME"/qt6ct
else
    confdir=~/.config/qt6ct
fi
if [[ ! -d "$confdir" ]]
then
    #we have absolutely nothing, copying some small files shouldn't hurt
    cp -r "$directory"/../libs_linux64/qt6ct "$confdir"
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/qt6ct "$@"
exit $?
