#!/bin/bash
# unattended vdr-sxfe random channel switching stress test
# version 0.2, 14.09.2009, written by Dirk Brenken <dirk@brenken.org>
# syntax: start-stop-daemon --start --quiet --exec /<path>/sxfe-test.sh --background
#

# path to svdrp- and kill-utility
SVDRP="/opt/vdr/svdrpsend.pl"
KILL="/usr/bin/pkill -x"

# path to vdr-sxfe & settings for remote frontend
XINE_PRG="/usr/bin/vdr-sxfe \
--video=vdpau \
--aspect=default \
--fullscreen \
--buffers=1000 \
--verbose \
--nokbd \
--audio=alsa:hdmi \
--post=tvtime:method=use_vo_driver \
--config=/etc/02_vdr/plugins/xineliboutput/config"

# logfile base directory
LOGBASE="/var/log"

# number of desired test laps
LAPS="3"

# number of desired (random) channel switches per lap
CHANNELS="20"

# stay on channel as long as (in seconds)
TIMEOUT="600"

# lower and upper channel boundary for random channel selection, refer to channels.conf
RANGE_L="0"
RANGE_H="24"

function sxfe_running()
{
    ps ax|grep -v grep|grep -qse vdr-sxfe
}

for a in $(seq $LAPS)
do
    TS=`date +%Y%m%d_%H%M%S`
    CH_OLD=0
    XINE_LOG="$LOGBASE/xineliboutput_$TS.log"
    su -c "$XINE_PRG >> $XINE_LOG 2>&1 &"
    for b in $(seq $CHANNELS)
    do
        CH=$RANDOM
        let CH%=$RANGE_H
        if [ $CH -le $RANGE_L -o $CH -eq $CH_OLD ]; then
	    let CH=$CH+1
	fi
	su -c "$SVDRP CHAN $CH >> $XINE_LOG"
	CH_OLD=$CH
	sleep $TIMEOUT
    done
    if (sxfe_running); then
	$KILL vdr-sxfe
	sleep 10
    fi
done
exit 0
