#!/bin/bash

################################################################################
# Copyright (C) 2015-2026 NICE s.r.l.
# All Rights Reserved
#
# This software is the confidential and proprietary information
# of NICE s.r.l. ("Confidential Information").
# You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of
# the license agreement you entered into with NICE.
################################################################################

prefix=/usr
exec_prefix=/usr
bindir=/usr/bin
libdir=/usr/lib64
libexecdir=/usr/lib
datadir=/usr/share
dcvsessionlauncher_bin="${libexecdir}/dcv/dcvsessionlauncher"

export LD_LIBRARY_PATH="${libdir}/dcv:${LD_LIBRARY_PATH}"
export XDG_DATA_DIRS="${datadir}:${XDG_DATA_DIRS}:/usr/share:/usr/local/share"
export GIO_EXTRA_MODULES="${libdir}/dcv/gio/modules"
export DCV_AGENT_DIR="${libexecdir}/dcv"

case "$1" in
    --gdb)
        shift
        export G_DEBUG=fatal-criticals
        gdb -ex run --args "${dcvsessionlauncher_bin}" "$@"
        _exit=$?
        ;;
    --valgrind)
        shift
        export G_SLICE=always-malloc
        export G_DEBUG=resident-modules
        timestamp=$(date +'%F_%H-%M-%S-%N')
        valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --log-file="/tmp/dcvsessionlauncher_${timestamp}.vgdump" "${dcvsessionlauncher_bin}" "$@"
        _exit=$?
        ;;
    --sanitizer)
        shift
        timestamp=$(date +'%F_%H-%M-%S-%N')
        export G_SLICE=always-malloc
        export ASAN_OPTIONS="log_path=/tmp/dcvsessionlauncher_${timestamp}.asan:fast_unwind_on_malloc=0"
        "${dcvsessionlauncher_bin}" "$@"
        _exit=$?
        ;;
    -d)
        shift
        "${dcvsessionlauncher_bin}" "$@"
        _exit=$?
        ;;
    *)
        "${dcvsessionlauncher_bin}" "$@" &
        _exit=$?
        ;;
esac

exit ${_exit}

# --------------------------------------------------------------------------- #
# ex: set ts=4 sw=4 et syntax=sh :
# --------------------------------------------------------------------------- #
