#! /bin/sh
# Author: Mirko Gnther (mi.guenther@ib-helms.de)
#
# be founded on runvdr by Klaus Schmidinger
#
# start_vdr: Loads the DVB driver and runs VDR
#
# start_vdr:
#
# If VDR exits abnormally, the driver will be reloaded (if no program has open /dev/video)
# and VDR restarted.
#
# Any command line parameters will be passed on to the
# actual 'vdr' program.
#
# version 0.2.1

# !!don't change this!!
HOMEDIR="/usr/local/vdr/rec/video0"

VDRPRG="/usr/local/bin/vdr"
VDRUSR="vdr"
VDRCMD="$VDRPRG -v $HOMEDIR -w 60 $*"

KILL="/usr/bin/killall -q -TERM"
LSMOD="`/sbin/lsmod | grep -w '^dvb' | wc -l`"
USEVID="`/bin/fuser /dev/video | grep -w 'video' | wc -l`"


export DVDCSS_METHOD=key

# Load driver if it hasn't been loaded already:
if [ $LSMOD -eq 0 ] ; then
   (rcdvb start)
   sleep 10
   fi

while (true) do
      su $VDRUSR -c "$VDRCMD"
      if test $? -eq 0; then exit; fi
      date
      echo "restarting VDR"
      $KILL $VDRPRG
      sleep 10
      if [ $USEVID -eq 0 ]; then
       echo "restarting DVB"
       (rcdvb restart)
       sleep 10
      fi
      date
      done

