#!/bin/bash

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

#osmesa is separated, to try to prevent issues with wb_view
commandlibs="$directory/../libs_linux64:$directory/../libs_linux64/osmesa"

#matlab does stupid stuff with LD_LIBRARY_PATH in system() calls, leading to warnings about non-versioned libraries
#so, if we detect matlab in it, ignore the LD_LIBRARY_PATH to prevent errors/warnings
if [[ -z "$LD_LIBRARY_PATH" ]] || echo "$LD_LIBRARY_PATH" | grep -iq matlab
then
    export LD_LIBRARY_PATH="$commandlibs"
else
    export LD_LIBRARY_PATH="$commandlibs:$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"

#QT6 wants UTF-8 locale and will complain bitterly
if [[ "${LC_ALL:-}" != "" && "${LC_ALL:-}" != *.UTF-8 ||
        "${LANGUAGE:-}" != "" && "${LANGUAGE:-}" != *.UTF-8 ]]
then
        #matlab un-exports LC_ALL after setting everything else independently, so it can set LC_NUMERIC to C by itself
        #fix it all with the override
        export LC_ALL=C.UTF-8
        export LANGUAGE=C.UTF-8
fi

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