#!/bin/sh
#
# infosatepg.sh
#
# Required: ps, svdrpsend.pl, infosatepg, tvm2vdr.pl
#
# add this lines to your commands.conf:
# folgende zeilen in die commands.conf eintragen:
#
# Run infosatepg               : echo "/usr/local/bin/infosatepg.sh" | at now
# Run infosatepg + tvmovie2vdr : echo "/usr/local/bin/infosatepg.sh -tvmovie2vdr" | at now
#

# CONFIG START
  INFOSAT_OPT="-v"                               # options
  CHANNEL="VIVA"                                 # which channel, e.g: <NUM> or <NAME>
  SHOWSTATUS="1"                                 # show status (1=true/0=false)
  INFOSAT_OUT="/video/received_data/"            # store infosatepg in
  INFOSAT_LOGFILE="/var/log/infosatepg.log"      # logfile
  TVM_PATH="/usr/local/src/tvmovie2vdr"                  # path to tvmovie2vdr
# CONFIG END

PATH=/usr/local/bin:$PATH

if [ -n "$(pidof infosatepg)" ] ; then
    svdrpsend.pl MESG "infosatepg is already running..."
else
    if [ "$(svdrpsend.pl CHAN $CHANNEL | grep -c '^250.*')" -eq 0 ] ; then
	svdrpsend.pl MESG "switch to $CHANNEL failed..."
    else
	mkdir -p $INFOSAT_OUT
	infosatepg $INFOSAT_OPT -o $INFOSAT_OUT > $INFOSAT_LOGFILE &
	PID=$!
	if [ "$SHOWSTATUS" -eq 1 ] ; then
	    sh -c "until ! ps -p $PID; do sleep 30s; svdrpsend.pl MESG \"\$(grep -s [0-9]% $INFOSAT_LOGFILE | tail -n 1)\"; done" &
	fi
	wait $PID; kill $!
	svdrpsend.pl MESG "$(tail -n 1 $INFOSAT_LOGFILE)..."
	sleep 1s
	svdrpsend.pl HITK 0
	if [ -n "$1" ] ; then
            cd $TVM_PATH
            ./tvm2vdr.sh
	fi
    fi
fi \
>/dev/null 2>&1
