#!/bin/sh

# runvdr: Loads the DVB driver and runs VDR
#
# If VDR exits abnormally, the driver will be reloaded
# and VDR restarted.
#
# Set the environment variable VDRUSR to the user id you
# want VDR to run with. If VDRUSR is not set, VDR will run
# as 'root', which is not necessarily advisable.
#
# Since this script loads the DVB driver, it must be started
# as user 'root'.
#
# Any command line parameters will be passed on to the
# actual 'vdr' program.
#

if [ -f /var/lock/stopvdr ] ; then
   exit 1
fi
set x
export LC_COLLATE=de_DE

######## new - defined with export
export VDRPATH="/usr/local/bin"
export VDRLIBPATH="/usr/local/lib/vdr"
export CNFPATH="/_config/vdr"
export BINPATH="/_config/bin"
export PLGCNFPATH="$CNFPATH/plugins"
export VDRPRG="$VDRPATH/vdr"

######## new - PluginSetup
VDRCMD="$VDRPRG -c $CNFPATH -L $VDRLIBPATH -w 50  -t /dev/tty8 -s $BINPATH/vdrshutdown \
        -r '$BINPATH/donoad.sh' \
        -P pluginsetup "
######## new - PluginSetup

KILLPROC="/sbin/killproc -TERM"
sh $BINPATH/mpkeys.sh

while (true) do
   $BINPATH/dvbmod 1
   rm -rf /tmp/mpg-cache/* > /dev/null 2>&1 &
   rm -rf /tmp/images/* > /dev/null 2>&1 &
   mv -f $CNFPATH/setup.conf $CNFPATH/setup.conf.new
   sed 's/^CurrentChannel.*/CurrentChannel = 1/' $CNFPATH/setup.conf.new > $CNFPATH/setup.conf

   ######## new - PluginSetup
   #replaced su -c "$VDRCMD" $VDRUSR
   ALL_PLUGINS=`cat $PLGCNFPATH/plugin_setup_runvdr.conf`
   su -c "$VDRCMD $ALL_PLUGINS" $VDRUSR
   ######## new - PluginSetup

   $KILLPROC $VDRPRG
   killall -9 find > /dev/null 2>&1
   date
   sleep 1
   echo "restarting VDR"
   $BINPATH/dvbmod 0
   if [ -f /var/lock/stopvdr ] ; then
      rm /var/lock/stopvdr
      exit 1
   fi
done
