#!/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
pkglibdir=/usr/lib64/dcv
modulesdir=/usr/lib64/dcv/modules
programsdir=/usr/libexec/dcv
saslpluginsdir=/usr/lib64/dcv/sasl2
datadir=/usr/share
dcvserver_bin="${programsdir}/dcvserver"

export LD_LIBRARY_PATH="${pkglibdir}:${LD_LIBRARY_PATH}"
export XDG_DATA_DIRS="${datadir}:${XDG_DATA_DIRS}:/usr/share:/usr/local/share"
export GIO_EXTRA_MODULES="${pkglibdir}/gio/modules"
export DCV_MODULE_DIR="${modulesdir}"
export DCV_SASL_PLUGIN_DIR="${saslpluginsdir}"
export DCV_AGENT_DIR="${programsdir}"

case "$1" in
    --gdb)
        shift
        export G_DEBUG=fatal-criticals
        gdb -ex run --args "${dcvserver_bin}" "$@"
        _exit=$?
        ;;
    --strace)
        shift
        strace -o stdump -v -f "${dcvserver_bin}" "$@"
        _exit=$?
        ;;
    --valgrind)
        shift
        export G_DEBUG=resident-modules
        export G_SLICE=always-malloc
        timestamp=$(date +'%F_%H-%M-%S-%N')
        valgrind --tool=memcheck --leak-check=full --leak-resolution=high --num-callers=20 --log-file="/tmp/dcvserver_${timestamp}.vgdump" "${dcvserver_bin}" "$@"
        _exit=$?
        ;;
    --sanitizer)
        shift
        timestamp=$(date +'%F_%H-%M-%S-%N')
        export G_SLICE=always-malloc
        export ASAN_OPTIONS="log_path=/tmp/dcvserver_${timestamp}.asan:fast_unwind_on_malloc=0"
        "${dcvserver_bin}" "$@"
        _exit=$?
        ;;
    --shell)
        shift
        # Just a simple shell with the env already set
        export PATH="${bindir}:${PATH}"
        PS1="[DCV]\$ " bash -i
        _exit=$?
        ;;
    -d)
        shift
        "${dcvserver_bin}" "$@"
        _exit=$?
        ;;
    *)
        "${dcvserver_bin}" "$@" &
        _exit=$?
        ;;
esac

exit ${_exit}

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