#!/bin/bash


. /etc/sysconfig

export PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin"
export NAME="vdr"
export DESC="Digital Videorecorder"
export LANG="C"
export HOME="/root"

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

export VDRTTY="10"

MODULES="
cx88_dvb
evdev
videodev
#v4l1_compat
v4l2_common
video_buf
dvb_core
ves1x93
stv0299
ves1820
tda1004x
cx24110
#mt312
mt352
saa7146
saa7146_vv
ttpci_eeprom
dvb_ttpci
budget_core
budget
budget_ci
budget_av
#b2c2_flexcop
b2c2-flexcop-pci
"

for PLUGIN in `unique.pl $PLUGINLIST`;
do
  PLUGINS="$PLUGINS -P$PLUGIN"
  if [ "$PLUGIN" = "dxr3" ]; then
        dxr3Used=1
  fi
done


loadmodule() {
  modprobe $*
  #insmod /lib/modules/`uname -r`/misc/$*
}

loaddriver() {
  #modprobe -q dvb_core dvb_shutdown_timeout=0
  OLDIFS=$IFS
  IFS=$'\t'$'\n'
  for module in $MODULES; do
    IFS=$OLDIFS
    if [ -n "${module##\#*}" ]; then
      grep -w "^${module%% *}" /proc/modules >/dev/null 2>/dev/null
      if [ "$?" == "1" ]; then
        loadmodule $module
      fi
    fi
    IFS=$'\t'$'\n'
  done  
  IFS=$OLDIFS
  # load dxr3 modules if dxr3-plugin is activated
  if [ "$dxr3Used" = "1" ]; then
        /etc/init.d/load_dxr3
  fi
}

unloaddriver() {
  echo $IFS >&2
  sleep 1
  OLDIFS=$IFS
  IFS=""
  MODULES="`echo $MODULES | tac`"
  IFS=$OLDIFS
  OLDIFS=$IFS
  IFS=$'\t'$'\n'

  for module in $MODULES; do
    if [ -n "${module##\#*}" ]; then
      grep -w "^${module%% *}" /proc/modules >/dev/null 2>/dev/null
      if [ "$?" == "0" ]; then
        #echo "Removing module: ${module%% *} It has value used: $?" >&2
        modprobe -r ${module%% *}
      fi
    fi
  done
  # workaround for airstar2 and skystar2 if modprobe -r fails
  if [ -n $(grep -w flexcop /proc/modules) ]; then
        #echo "Removing module: b2c2_flexcop_pci" >&2
        modprobe -r b2c2_flexcop_pci >&2
        #echo "Removing module: b2c2_flexcop" >&2
        modprobe -r b2c2_flexcop
  fi
  IFS=$OLDIFS
  # unload dxr3 modules if the dxr3-plugin is activated
  if [ "$dxr3Used" = "1" ]; then
        /etc/init.d/unload_dxr3
  fi
}

setdefaults() {
  OLDIFS=$IFS
  IFS=""
  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
  IFS=$OLDIFS
}

export WITHOUTDRIVER=$2
case "$1" in
  start)
  rm /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 $WITHOUTDRIVER 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
    #killall -9 vdr # XXX
    ;;
  up)
  if [ -z $2 ]; then
    loaddriver
  fi
  
  if [ $USE_INTERNAL_IR != "yes" ]; then
    #setserial /dev/ttyS0 uart none
    loadmodule lirc_serial
    $BINDIR/lircd
  else
    #if [ $INVERT_SIGNAL = "yes" ]; then
    #  LOADKEYS_PARAM="-i"
    #fi
    #/usr/bin/av7110_loadkeys $LOADKEYS_PARAM /etc/vdr/loadkeys/default.rc5 > /proc/av7110_ir
    # add -Premote to $PLUGINS?
    PLUGINS="$PLUGINS -Premote"
  fi
        
  if [ x$START_FBTV == x"yes" ]; then
    fbtv -t 10 2>/dev/null >/dev/null &
    # doesnt work, why?
    #bash -c 'while true; do fbtv -t 12 2>/dev/null >/dev/null; wait; sleep 5; done' &
  else
    chvt $VDRTTY
    echo "Here reads VDR from the keyboard as remote control replacement." > /dev/tty$VDRTTY
    echo "For a login console switch to console 1 to 4 with [ALT][F1] to [ALT][F4]" > /dev/tty$VDRTTY
    echo "To get back here, please press [ALT][F10]" > /dev/tty$VDRTTY
  fi
 
  if [ "$USE_DEFAULTS" == "yes" ]; then
    setdefaults
  fi 

  sleep 5 && LANG=de_DE nice -n 19 vdradmind.pl &
  # premiere workaround
  #sleep 15 && svdrpsend.pl "chan 100" 2>/dev/null >/dev/null &
  umask 0002

  $BINDIR/vdr -L $PLUGINDIR $PLUGINS $COMMON_PARAMETER -v /video0 -c $CONFDIR -w 900 -E /ramdisk/epg.data -s /usr/bin/poweroff.pl -r /usr/bin/noadcall.sh -P"externalplayer -c 'dxr3player -D ac3' -m DVD -r" < /dev/tty$VDRTTY
  ;;
  down)
  vdradmind.pl -k
  PID=`ps -C vdr -o "%p" | grep -vw PID | tac`
  if [ x"$PID" != "x" ]; then
    kill $PID
  fi
  sleep 3
  
  if [ x$START_FBTV == x"yes" ]; then
    PID=`ps auxw | grep "fbtv" | grep -v grep | awk '{ print $2 }'`
    if [ x"$PID" != "x" ]; then
      kill $PID                           
    fi
  fi    

  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
