#!/bin/bash
# set -x
# . /etc/sysconfig

#TEXTDOMAIN="vdr"
#TEXTDOMAINDIR="/usr/share/vdr/locale"
#export LANG TEXTDOMAIN{,DIR}

# export LD_ASSUME_KERNEL=2.4.1
export JAVA_HOME="/usr/lib/jvm/java-1.5.0-sun"
export PROJECTX_HOME="/usr/local/bin/projectx"
export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/lib/jvm/java-1.5.0-sun:."
export NAME="vdr"
export DESC="Digital Videorecorder"
export LANG=de_DE@euro
export LC_CTYPE=de_DE@euro
export VDR_CHARSET_OVERRIDE="ISO-8859-15"
# export LANG=de_DE.ISO-8859-1
# export LC_CTYPE=de_DE.ISO-8859-1
# export LANG=de_DE.UTF-8
# export LC_CTYPE=de_DE.UTF-8
export HOME="/root"

BINDIR="/usr/bin"
CONFDIR="/etc/vdr"
PLUGINDIR="/usr/lib/vdr/plugins"

export VDRTTY="8"

loadmodule() {
  modprobe -q dvb-code dvb_shutdown_timeout=0
  modprobe $*
}

loaddriver() {
   /usr/bin/insdvb.sh load
}

unloaddriver() {
   /usr/bin/insdvb.sh unload
}

setdefaults() {
  if [ -w $CONFDIR/setup.conf ]; then
    shopt -s extglob
    SETUP_CONF="`cat $CONFDIR/setup.conf`"
    if [ "$STARTCHAN" -gt "0" ]; then
      SETUP_CONF=${SETUP_CONF/CurrentChannel = +([0-9])/CurrentChannel = $STARTCHAN}
    fi
    if [ "$STARTVOL" -gt "0" ]; then
      SETUP_CONF=${SETUP_CONF/CurrentVolume = +([0-9])/CurrentVolume = $STARTVOL}
    fi
    echo "$SETUP_CONF" > $CONFDIR/setup.conf
    shopt -u extglob
  fi
}

case "$1" in
  start)
#rm /dev/cdrom /dev/dvd
#  ln -s /dev/cdrom /dev/dvd
  /bin/bash -c '\
    /bin/echo $$ > /var/run/vdr.pid; \
    while /bin/true; do \
      start=`date +%s`; \
      /etc/init.d/runvdr up 2>&1 >/dev/null; \
      stop=`date +%s`; \
      if [ $(($stop - $start)) -lt 15 ] ; then \
        echo "Something went wrong. VDR respawning to fast, sleeping one minute" > /dev/tty$VDRTTY; \
        sleep 60; \
      fi; \
      sleep 1; \
      /etc/init.d/runvdr down; \
      sleep 5; \
    done >/dev/null 2>/dev/null' &
  ;;
  stop)
    /etc/init.d/runvdr down 2>&1 >/dev/null
    if [ -e /var/run/vdr.pid ]; then
      PID=`cat /var/run/vdr.pid`
      if [ x"$PID" != "x" ]; then
        kill -9 $PID
      fi
      rm -f /var/run/vdr.pid
    fi
    sleep 2
     /usr/bin/stoplcd.sh &
    ;;
  up)
  if [ -z $2 ]; then
    loaddriver
  fi
  
# setserial /dev/ttyS1 uart none
# loadmodule lirc_serial
modprobe lirc_serial irq=3 io=0x2f8
/usr/sbin/lircd --device=/dev/lirc/0 /etc/lircd.conf

setdefaults

# sleep 5 && nice -n 19 vdradmind.pl &
umask 0002
$BINDIR/vdr -L $PLUGINDIR $COMMON_PARAMETER -v /media/video -c $CONFDIR -w 900 -E /var/epg.data -b /var -s /usr/bin/poweroff.pl \
# $BINDIR/vdr -L $PLUGINDIR $COMMON_PARAMETER -v /video -c $CONFDIR -w 900 -E /ramdisk/epg.data -b /ramdisk -s /usr/bin/poweroff.pl \
#                       -Pgraphlcd \
#                       -Ptext2skin \
#                       -Pmp3ng \
#                       -Pdvdswitch \
#                       -Pdirector \
#                       -Pdvd \
#                       -Pmplayer \
#                       -Pnordlichtsepg \
#                       -Pextrecmenu \
#                       -Pburn \
#                       -Pradio \
#                       -Pfemon \
#                       -Ppremiereepg \
#                       -Pmpcsmon \
#                       -Pcdda \
#
  ;;
  down)
#   vdradmind.pl -k
   PID=`ps -C vdr -o "%p" | grep -vw PID | tac`
    if [ x"$PID" != "x" ]; then
      kill $PID
    fi
    
sleep 1
  
  unloaddriver
   
  killall lircd >/dev/null 2>/dev/null
  rmmod lirc_serial >/dev/null 2>/dev/null
  ;;
  loaddriver)
  loaddriver
  ;;
  unloaddriver)
  unloaddriver
  ;;
  *)
  N=/etc/init.d/$NAME
  # echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
  echo "Usage: $N {start|stop|up|down}" >&2
  exit 1
  ;;
esac

exit 0