#! /bin/sh

# Script to run Yate from the build directory, optionally under gdb or valgrind

yate="./yate"
set_conf="-c ./conf.d"
set_mods="-m ./modules"
set_share="-e ./share"
if [ "$1" = "--executable" ]; then
    shift
    yate="$1"
    shift
fi
if [ "$1" = "--there" ]; then
    shift
    cd `dirname "$0"`
fi
if [ "$1" = "--gdb" ]; then
    shift
    yate="gdb --args $yate"
fi
if [ "$1" = "--core" ]; then
    shift
    yate="gdb $yate core*"
    set_conf=""
    set_mods=""
fi
if [ "$1" = "--backtrace" ]; then
    shift
    yate="gdb -x tools/gdb-bt-all.txt $yate core*"
    set_conf=""
    set_mods=""
    exec </dev/null 2>&1
fi
if [ "$1" = "--valgrind" ]; then
    shift
    yate="valgrind $1 $yate"
    shift
fi
if [ "$1" = "--callgrind" ]; then
    shift
    export LD_BIND_NOW=yes
    yate="valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --zero-before=*TelEngine*Engine*internalStatisticsStart* $1 $yate -Du"
    shift
fi

for opt in $@; do
    case "$opt" in
	-c)
	    set_conf=
	    ;;
	-m)
	    set_mods=
	    ;;
	-e)
	    set_share=
	    ;;
    esac
done

LD_LIBRARY_PATH=.@H323_RUN@:$LD_LIBRARY_PATH exec $yate $set_conf $set_mods $set_share "$@"
