#!/bin/bash

# We need bash here, because of the *-loader.sh scripts containing bashisms!

## NAME
##   vdrleaktest - Runs the VDR debugging binary with Valgrind.
## 
## SYNOPSIS
##   vdrleaktest [-h] [vdr-options]
## 
## DESCRIPTION
##   vdrleaktest will stop any running VDR daemon and start the debugging
##   version of VDR with Valgrind. No plugins will be loaded automatically.
##   In order to test VDR plugins with vdrleaktest, you need to pass the
##   appropriate VDR arguments (e.g. -P plugin-name).
## 
## OPTIONS
##   -h This help
## 
##   All other options will be passed down to VDR
## 
## SEE ALSO
##   vdr(1), valgrind(1)
## 
## AUTHOR
##   This manual page was written by Tobias Grimm <tg@e-tobi.net>
## 
### txt2man -s 1 -t VDRLEAKTEST -v "Start VDR with Valgrind"

usage()
{
    local HELPCOMMENTPATTERN="^## "
    cat "$0" | grep "$HELPCOMMENTPATTERN" | sed "s/$HELPCOMMENTPATTERN//"
}


while getopts h opt
do
    case $opt in
        h)
            usage
            exit
            ;;
    esac
done

#
# Check for Valgrind
#

if [ ! -x /usr/bin/valgrind ] ; then
    echo "ERROR: Please install valgrind first!" >&2
    exit 1
fi

#
# c't-VDR Memory Leak Tester
#

. /usr/lib/vdr/config-loader.sh
. /usr/lib/vdr/commands-loader.sh

mergecommands "commands"
mergecommands "reccmds"

/etc/init.d/vdr stop

LANG=C LD_LIBRARY_PATH="/usr/lib/debug${LD_LIBRARY_PATH:+:$LD_LIBRARY_PATH}" \
   valgrind --tool=memcheck --leak-check=yes --num-callers=20 \
   --suppressions=/usr/share/vdr/valgrind.supp \
   /usr/bin/vdr-dbg -v $VIDEO_DIR -c $CFG_DIR -L $PLUGIN_DIR  -r $REC_CMD \
   -E $EPG_FILE -g /tmp $OPTIONS --port $SVDRP_PORT --lirc \
   "$@"

/etc/init.d/vdr restart
