#!/bin/sh

PRODUCT_NAME="Linaro Forge"

# Don't wipe LD_PRELOAD if script was launched under squish
[ -z "$SQUISH_PATH" ] && LD_PRELOAD=

# First find FORGE_TOOLS_PATH. Don't set this is in your environment - this script will
# find it.

# Follow symlinks to get the canonical path of this script (in case we're symlinked outside the install directory).
if type readlink >/dev/null 2>&1; then
	CANONICAL_EXE="`readlink -f "$0"`"
else
	CANONICAL_EXE="$0"
fi

# Set FORGE_TOOLS_PATH to the parent of this script's directory.
FORGE_TOOLS_PATH="$(cd "$(dirname "$CANONICAL_EXE")/.."; pwd)"
. "$FORGE_TOOLS_PATH/libexec/common"

FORGE_INTERNAL_ARGS_ACCUMULATOR=""
for var in "$@"; do
    FORGE_INTERNAL_ARGS_ACCUMULATOR="$FORGE_INTERNAL_ARGS_ACCUMULATOR$var"

    case "$var" in
        "-offline" | "--offline" | "-profile" | "--profile")
            FORGE_OFFLINE_LAUNCHER_USE=on
            ;;
        "-show-the-gui" | "--show-the-gui")
            FORGE_GUI=on
            ;;
        "--" | "---") #19544
            break
            ;;
    esac
done

# --export option in offline mode: make sure not to catch any arguments for mpirun
case $FORGE_INTERNAL_ARGS_ACCUMULATOR in
    --export*.json*.map | -export*.json*.map)
        FORGE_OFFLINE_LAUNCHER_USE=on
        ;;
esac

MAP_ARGS="--tool=profiler"
if [ -n "${FLUENT_MPI_PRE}" ]; then
    echo "MAP: Running in ANSYS Fluent compatibility mode."
    if [ -w /dev/tty -a -z "${FORGE_NO_DEV_TTY_FOR_FLUENT}" ]; then
        log=/dev/tty
    else
        log="map$(date +'%Y%m%d%H%M%S').log"
        echo "MAP: Logging to ${log}."
    fi
    exec >${log} 2>&1
fi
if [ -n "${FORGE_ENABLE_CORE_DUMP}" ]; then
    ulimit -c unlimited
fi
if [ "$FORGE_OFFLINE_LAUNCHER_USE" = "on" ] && [ "$FORGE_GUI" != "on" ]; then
    exec $SSH_AGENT "$FORGE_TOOLS_PATH/libexec/forge-offline" "$FORGE_TOOLS_PATH/libexec/forge.bin" $MAP_ARGS "$@"
else
    exec $SSH_AGENT "$FORGE_TOOLS_PATH/libexec/forge.bin" $MAP_ARGS "$@"
fi
