#!/usr/bin/bash

################################################################################
# Copyright (C) 2020-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/libexec
datadir=/usr/share
dcv_bin="${libexecdir}/dcv/dcvagentlauncher"

export LD_LIBRARY_PATH="${libdir}/dcv:${LD_LIBRARY_PATH}"
export GIO_EXTRA_MODULES="${libdir}/dcv/gio/modules"
export GST_PLUGIN_SCANNER="${libexecdir}/dcv/gst-plugin-scanner"
export GST_PLUGIN_SYSTEM_PATH="${libdir}/dcv/gstreamer-1.0"

case "$1" in
    --gdb)
        shift
        export G_DEBUG=fatal-criticals
        gdb -ex run --args "${dcv_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/dcvagentlauncher_$USER_${timestamp}.vgdump" "${dcv_bin}" "$@" &
        _exit=$?
        ;;
    --sanitizer)
        shift
        timestamp=$(date +'%F_%H-%M-%S-%N')
        export G_SLICE=always-malloc
        export ASAN_OPTIONS="log_path=/tmp/dcvagentlauncher_${timestamp}.asan:fast_unwind_on_malloc=0"
        "${dcv_bin}" "$@" &
        _exit=$?
        ;;
    -d)
        shift
        "${dcv_bin}" "$@"
        _exit=$?
        ;;
    *)
        "${dcv_bin}" "$@" &
        _exit=$?
        ;;
esac

exit ${_exit}

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