File runvdr of Package vdr

#!/bin/bash

# runvdr: Loads the DVB driver and runs VDR
#
# If VDR exits abnormally, the driver will be reloaded
# and VDR restarted.
#
# original by Klaus Schmidinger
# adapted and enhanced for SuSE Linux by Ludwig Nussel
#
# $Id: runvdr 1.11 2003/08/26 16:15:41 kls Exp $

VDR_BIN="/usr/sbin/vdr"
rcdvb="/usr/sbin/rcdvb"

cd # go home

runvdrexit()
{
	code="$1"
	shift
	[ -n "$1" ] && echo -e "$@"
	# switch back to tty1 so the vdr console is not busy
	if [ "$PREVLEVEL" = N -a "$VDR_SWITCHVT" = yes ]; then
		chvt 1
	fi
	exit "$code"
}

. /etc/rc.status || runvdrexit 2
. /etc/sysconfig/vdr || runvdrexit 2
. /etc/sysconfig/language || runvdrexit 2
test -s ~vdr/.i18n && . ~vdr/.i18n

unset ${!LC_*} LANGUAGE
for lc in LANG LC_CTYPE LC_NUMERIC LC_TIME      \
          LC_COLLATE LC_MONETARY LC_MESSAGES    \
          LC_PAPER LC_NAME LC_ADDRESS           \
          LC_TELEPHONE LC_MEASUREMENT           \
          LC_IDENTIFICATION LC_ALL
do
    eval val="\$RC_$lc"
    if test -n "$val"; then
        eval $lc="\$RC_$lc"
        export $lc
    fi
done
unset lc val

# use utf8 by default
if [ -z "$LANG" -a -z "$LC_ALL" ]; then
	export LANG='en_US.UTF-8'
fi

lsmod=/bin/lsmod
[ ! -x "$lsmod" ] && lsmod=/sbin/lsmod

if [ ! -x "$lsmod" ]; then
	echo -e "$lsmod not found, exit"
	exit 2
fi

makeargs()
{
    [ -n "$VDR_CONFIGDIR" ] && echo "-c" "\"$VDR_CONFIGDIR\""
    [ -n "$VDR_LOGLEVEL" ] && echo "-l" "\"$VDR_LOGLEVEL\""
    [ -n "$VDR_SVDRP_PORT" ] && echo "-p" "\"$VDR_SVDRP_PORT\""
    [ -n "$VDR_WATCHDOG" ] && echo "-w" "\"$VDR_WATCHDOG\""
    [ -n "$VDR_RECORD_SCRIPT" ] && echo "-r" "\"$VDR_RECORD_SCRIPT\""
    [ -n "$VDR_SHUTDOWN_SCRIPT" ] && echo "-s" "\"$VDR_SHUTDOWN_SCRIPT\""
    [ "$VDR_MUTE" = yes ] && echo "-m"

    for c in $VDR_CARDS; do
	echo "-D$c"
    done
	
    for p in $installed_plugins; do
	option="-P$p"
	eval args=\$VDR_PLUGIN_ARGS_${p//[^[:alnum:]]/_}
	[ -n "$args" ] && option="$option $args"
	echo "\"$option\""
    done

    [ -n "$VDR_ADDITIONAL_ARGS" ] && echo "$VDR_ADDITIONAL_ARGS"
}

processplugins()
{
	installed_plugins=""
	missing_plugins=""
	local missing=0
	while read plugin; do
		if [ "$plugin" = "--" ]; then
			missing=1
		elif [ "$missing" = 0 ]; then
			installed_plugins="$installed_plugins $plugin"
		else
			missing_plugins="$missing_plugins $plugin"
		fi
	done < <($VDR_BIN --version | /bin/gawk -v plugins="$VDR_PLUGINS" '
		{
			inst[$1]=1
		}
		END {
			split(plugins,want)
			for (i in want)
				if (want[i] in inst)
					print want[i]
				else
				{
					notinst[want[i]]=1
					delete want[i]
				}
			print "--"
			for (i in notinst)
				print i
		}')
}

# Load driver if it hasn't been loaded already:
if [ "$VDR_RELOADDVB" = yes ] && ! $lsmod | grep -q '^dvb'; then
   $rcdvb start
fi

last_restart=`date +%s`

while true; do
	. /etc/sysconfig/vdr || runvdrexit 2
	date

	processplugins

	echo -n "${extd}Starting Video Disk Recorder${norm} "
	[ -n "$installed_plugins" ] && echo "[$installed_plugins ]" || echo
	[ -n "$missing_plugins" ] && echo "  missing plugins:$missing_plugins"

	eval $VDR_BIN `makeargs`
	status="$?"
	if [ "$status" -eq 0 ]; then
	      runvdrexit 0 "vdr exited normally"
	elif [ "$status" -eq 2 ]; then
	      runvdrexit 2 "${warn}configuration error${norm}, check log file"
	fi
	num_restart=$((num_restart+1))
	restarted=`date +%s`
	if [ $((restarted-last_restart)) -le "$VDR_RESTART_INTERVAL" -a "$num_restart" -gt "$VDR_MAX_RESTART" ]; then
		# too many restarts within interval
		echo -e "${warn}vdr died${norm} too often, exiting"
		if [ -n "$VDR_FAIL_MAILTO" ]; then
			echo "VDR died $num_restart times in " $((restarted-last_restart)) " seconds" | \
				mail -s "VDR died at `date`" "$VDR_FAIL_MAILTO"
		fi
		if [ "$VDR_FAIL_BOOT" -gt 0 ]; then
			echo -ne "${warn}reboot in $VDR_FAIL_BOOT seconds${norm}"
			while [ "$VDR_FAIL_BOOT" -gt 0 ]; do
				sleep 1
				echo -n .
				VDR_FAIL_BOOT=$((VDR_FAIL_BOOT-1))
			done
			echo -e " ${extd}booting${norm}"
			/sbin/reboot
		fi
		runvdrexit 1
	elif [ $((restarted-last_restart)) -gt "$VDR_RESTART_INTERVAL" ]; then
		# last restart too long ago, reset
		last_restart=$restarted
		num_restart=0
	fi

	echo -e "${extd}vdr died, restarting${norm}"
	echo "sending TERM signal"
	/sbin/killproc -TERM $VDR_BIN
	sleep 2
	echo "sending KILL signal"
	/sbin/killproc -KILL $VDR_BIN
	sleep 1
	if [ "$VDR_RELOADDVB" = yes ]; then
		$rcdvb restart
		sleep 1
	fi
done

runvdrexit 0