#!/bin/sh

#Version easyVDR 0.6.0

NOADBIN="noad"
LOG="/var/log/noad.log"
DO_NOAD="no"

source /etc/vdr/sysconfig
source /etc/vdr/noad/noadlist.conf

if [ ! which noad >/dev/null 2>&1 ] ; then
  logger -s "noad.sh: <$NOADBIN> nicht gefunden"
  exit 1
fi

##### mit speziellen Dateiname auslassen ##### 
if [ $(echo $2 | grep -ic "#5FNONOAD") -gt 0 ]; then 
  logger -s "noad.sh: Film $2 mit #5FNONOAD markiert; kein noad durchgeführt"
  exit 0
fi

##### nur für bestimmte Sender  #####
if [ x$NOAD_ON_CHANNELS = x"yes" ]; then
  OLDIFS=$IFS
  IFS=$'\t'$'\n'
  for channel in $NOADLIST; do 
    [ "$3" = $channel ] && DO_NOAD="yes"
  done
  IFS=$OLDIFS
  # Falls der parameter fehlt: Noad ausführen
  [ "$3" = "" ] && DO_NOAD="yes"
  if [ "$DO_NOAD" = "no" ]; then
    logger -s "noad.sh: kein noad für Sender $3"
    exit 0
  fi
fi

# gibt osd-meldung aus. wenn nicht gewünscht leer lassen
[ x$NOAD_OSD = x"yes" ] && OSDOPTS="--OSD"

# set additional args for every call here here
ADDOPTS="nice -v --background --comments --ac3 --overlap --jumplogo"

# set special args for a call with 'before' here
# e.g. set a specail statistikfile
BEFOREOPTS="--statisticfile=$LOG"

# set special args for a call with 'after' here
# e.g. backup the marks from the online-call before
#      so you can compare the marks and see
#      how the marks vary between online-mode 
#      and normal scan (backup-marks are in marks0.vdr)
AFTEROPTS="--backupmarks --statisticfile=$LOG"

# set the online-mode here 
# 1 means online for live-recording only
# 2 means online for every recording
##### VDR-Start in Abhängigkeit der NOAD-Einstellung #####

case "$NOAD" in
  "Aus")
    logger -s "noad.sh: Noad deaktiviert"
    exit 0
  ;;
  "Online nur Live Aufnahmen")
    ONLINEMODE="--online=1"
  ;;
  "Online alle Aufnahmen")
    ONLINEMODE="--online=2"
  ;;
  "Offline")
    ONLINE="no"
  ;; 
esac

case "$1" in
  "before")
    NOADCMD="$NOADBIN before $ADDOPTS $ONLINEMODE $BEFOREOPTS $OSDOPTS \"$2\""
    logger -s "noad.sh: Starte $NOADCMD"
    #screen -dm sh -c "su - root -p -c \"$NOADCMD\"" 
    su - root -p -c "$NOADCMD" 
  ;;
  "after")
    if [ x$ONLINE = x"no" ]; then
      NOADCMD="$NOADBIN after $ADDOPTS $ONLINEMODE $AFTEROPTS $OSDOPTS \"$2\""
      logger -s "noad.sh: Starte $NOADCMD"
      #screen -dm sh -c "su - root -p -c \"$NOADCMD\""
      su - root -p -c "$NOADCMD" 
    else
      logger -s "noad.sh: Starte noad nicht \"after\", da Online-Modus gesetzt wurde"
    fi
    /usr/bin/recordingdone.sh "$2"
    /usr/bin/CreateRecCover.sh "$2"
  ;;
esac
