#!/bin/sh

# DDTPATH is an alias of FORGE_TOOLS_PATH for backwards compatibility
export DDTPATH="${FORGE_TOOLS_PATH}"

# We set this environment variable because otherwise forge may try to load Qt with the native Wayland
# compositor where forge currently has many issues, see comments in ticket FORPRDEV-10184.
export QT_QPA_PLATFORM="xcb"

# On native Wayland platforms the code will try and launch with Wayland, which we do not currently support
# This code prevents this from happening, falling back to XWayland, see comments in ticket FORPRDEV-21295.
export XDG_SESSION_TYPE=""

[ -n "$PRODUCT_NAME" ] || PRODUCT_NAME="Linaro Forge"

[ -n "$FORGE_WORKING_DIRECTORY" ] && cd "$FORGE_WORKING_DIRECTORY"

# Architecture check
TOOLS_RUN_ARCH=`uname -m`
TOOLS_BUILD_ARCH="x86_64"

if [ $TOOLS_RUN_ARCH != $TOOLS_BUILD_ARCH ] ; then
    echo "$0: Your machine uses the $TOOLS_RUN_ARCH architecture but this $PRODUCT_NAME installation is for the $TOOLS_BUILD_ARCH architecture."
    exit 1
fi

# Check ssh-agent setup
TMPDIR_PATH="${TMPDIR:-/tmp}"
TMPDIR_WRITEABLE=
if [ -d "${TMPDIR_PATH}" ] && [ -w "${TMPDIR_PATH}" ]; then
	TMPDIR_WRITEABLE=1
fi

SSH_AGENT=
if [ -n "${TMPDIR_WRITEABLE}" ] && [ -z "${SSH_AUTH_SOCK}" ] && type ssh-agent >/dev/null 2>&1 && [ ! -u `which ssh-agent` ] && [ ! -g `which ssh-agent` ]; then
        SSH_AGENT=ssh-agent
fi

# Set SSH askpass program
if [ -z "${SSH_ASKPASS}" ]; then
        SSH_ASKPASS="$FORGE_TOOLS_PATH/libexec/qt-ssh-askpass"
        export SSH_ASKPASS
fi

# Set path
PATH="$FORGE_TOOLS_PATH/bin${PATH:+:$PATH}"
PATH="$FORGE_TOOLS_PATH/libexec${PATH:+:$PATH}"
export PATH

# Set library paths
for lib_path in lib/64
do
	LD_LIBRARY_PATH="$FORGE_TOOLS_PATH/${lib_path}${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}"
done
export LD_LIBRARY_PATH

PYTHONPATH="$FORGE_TOOLS_PATH/map/wrapper${PYTHONPATH:+:$PYTHONPATH}"
export PYTHONPATH

# Source allinearc files
SITE_RC=$FORGE_TOOLS_PATH/allinearc
if [ -r "$SITE_RC" ]; then
    . "$SITE_RC"
fi
USER_RC=${FORGE_CONFIG_DIR:-$HOME/.allinea}/allinearc
if [ -r "$USER_RC" ]; then
    . "$USER_RC"
fi

# perf-reports requires execmod on SELinux - force using the interpreter if prohibited
if [ -x "$(command -v getsebool)" ] ; then
    if getsebool selinuxuser_execmod 2>&1 | grep -q off ; then
        export QV4_FORCE_INTERPRETER=1
    fi
fi
