#!/bin/sh

# runvdr: Loads the DVB driver and runs VDR
#
# If VDR exits abnormally, the driver will be reloaded
# and VDR restarted.
#
# In order to actually use this script you need to implement
# the functions DriverLoaded(), LoadDriver() and UnloadDriver()
# and maybe adjust the VDRPRG and VDRCMD to your particular
# requirements.
#
# Since this script loads the DVB driver, it must be started
# as user 'root'. Add the option "-u username" to run VDR
# under the given user name.
#
# Any command line parameters will be passed on to the
# actual 'vdr' program.
#
# See the main source file 'vdr.c' for copyright information and
# how to reach the author.
#
# $Id: runvdr 1.19 2006/05/14 16:02:05 kls Exp $

# Charset de l'EPG
export VDR_CHARSET_OVERRIDE=ISO-8859-1

# Liste des modules necessaire pour vos cartes (Separer par des espaces si vous en avez plusieurs)
# dvb_ttpci et stv0299 sont pour une carte full
MODULESDVB="dvb_ttpci stv0299 saa7134-dvb"

# User lancant VDR
USER="vdr"

# Repertoire de conf
VDRCONF="/etc/vdr"

# Repertoire de conf des plugins
VDRCONFPLUGIN="$VDRCONF/plugins"

# Repertoire de l'executable VDR
VDRDIR="/usr/local/bin"

# Repertoire des lib VDR
VDRDIRLIB="/usr/local/lib/vdr"

# Repertoire des locales
LOCALEDIR="/usr/local/src/VDR/locale"

# Repertoire Video
VDRVIDEO="/video0"

# Repertoire temporaire
TEMP="/tmp"

# Executable VDR
VDRPRG="$VDRDIR/vdr"

# Commande de lancement de VDR
VDRCMD="$VDRPRG --lirc --no-kbd -L $VDRDIRLIB -v $VDRVIDEO -g $TEMP -c $VDRCONF --localedir $LOCALEDIR -s $VDRDIR/vdrpoweroff.sh -w 20 -l 3.6 $*"

KILL="/usr/bin/killall -q -TERM"

# Detect whether the DVB driver is already loaded
# and return 0 if it *is* loaded, 1 if not:
function DriverLoaded()
{
  /sbin/lsmod | grep dvb > /dev/null
  return $?
}

# Load all DVB driver modules needed for your hardware:
function LoadDriver()
{
  /sbin/modprobe -a $MODULESDVB
}

# Unload all DVB driver modules loaded in LoadDriver():
function UnloadDriver()
{
  /sbin/modprobe -r -a $MODULESDVB
}

# Load driver if it hasn't been loaded already:
if ! DriverLoaded; then
   LoadDriver
   fi

while (true) do
      ALL_PLUGINS=`cat $VDRCONFPLUGIN/plugin_setup_runvdr.conf`
      eval "su $USER -c \"$VDRCMD $ALL_PLUGINS\""
      if test $? -eq 0 -o $? -eq 2; then exit; fi
      echo "`date` reloading DVB driver"
      $KILL $VDRPRG
      sleep 10
      UnloadDriver
      LoadDriver
      echo "`date` restarting VDR"
      done
