#!/bin/sh

# runvdr: Loads the DVB driver and runs VDR
#
# If VDR exits abnormally, the driver will be reloaded
# and VDR restarted.
#
# Any command line parameters will be passed on to the
# actual 'vdr' program.
#

PLUGOPTS="-Pstreamdev-server"

VDRPRG="./bin/vdr"
VDRCMD="/bin/nice --11 $VDRPRG -w 60 -c /opt/vdr/etc $PLUGOPTS $*"

KILL="killall -q -TERM"

if [ "$(ps -A | grep " vdr")" != "" ]; then
   echo Error! VDR is already running!
   exit 1
fi

mkdir -p /var/video
cd /opt/vdr
while (true) do
#
# If you have stability Problems at tuning or similar
# unload and reload the dvb-modules here ...
# Example is for Hauppauge Nexus 2.0
#
#     rmmod dvb_ttpci
#     rmmod stv0299
#     rmmod dvb_core
#     modprobe dvb_ttpci
#     sleep 5
#
# Check if DVB device is present / remove this check if you
# would use vdr as streaming client only
#
      if [ ! -e /dev/dvb/adapter0/frontend0 ]; then
	  echo No DVB tuner found!
	  exit 1
      fi
#
#     Run VDR    
#
      eval "$VDRCMD"
      if test $? -eq 0 -o $? -eq 2; then exit; fi
      $KILL $VDRPRG
      sleep 10
      echo "`date` restarting VDR"
done
