#!/bin/sh
# Version: 0.2.0
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# $Id: vdrconvert.sh,v 1.17 2005/10/04 13:49:49 dimi Exp $

vco_user=`id -n -u`
homedir=`cat /etc/passwd|grep "^${vco_user}:"|cut -f6 -d ':'`
. $homedir/.vdrconvert/vdrconvert.env
umask 007
unset COLUMNS

# Default Directorys
[ -z "$VDRROOT" ]           && export VDRROOT=/video
[ -z "$DESTDIR" ]           && export DESTDIR=/video
[ -z "$TEMPDIR" ]           && export TEMPDIR=$DESTDIR/tmp
[ -z "$LOGDIR" ]            && export LOGDIR=/var/log/vdrconvert
[ -z "$VCORUNDIR" ]         && export VCORUNDIR=/var/run/vdrconvert
[ -z "$VCOQUEUEDIR" ]       && export VCOQUEUEDIR=/var/spool/vdrconvert

[ -d $VCORUNDIR ]      || mkdir -p $VCORUNDIR
PIDFILE="$VCORUNDIR/`basename ${0}.pid`"
echo "$$" >> $PIDFILE

trap do_clean SIGINT SIGQUIT SIGTERM 
do_clean() {
	echo "$0 aborted by signal"
	logger "$0 aborted by signal"
	if [ -f $PIDFILE ]
	then
	    TEMPPID=/tmp/`basename $PIDFILE`.$$
	    mv $PIDFILE $TEMPPID
	    status=$?
	    if [ $status -eq 0 ]
	    then
	        [ `cat $TEMPPID|wc -l` -gt 0 ] && cat $TEMPPID|grep -v $$ > $PIDFILE 
	    fi
	    [ -f $TEMPPID ] && rm $TEMPPID
	fi
	exit
}


[ "$DEBUG" = "yes" ] && set -x 
echo "`date` : Start $0"

# Default values for executables

[ -z "$SVDRPSEND" ]         && export SVDRPSEND=svdrpsend.pl
[ -z "$VDRSYNC" ]           && export VDRSYNC=$VDRCONVERTBINDIR/vdrsync-0.1.2.2.pl
[ -z "$VDRSYNC2" ]          && export VDRSYNC2=$VDRCONVERTBINDIR/vdrsync-0.1.2.2-dev2.pl
[ -z "$VDRSYNC3" ]          && export VDRSYNC3=$VDRCONVERTBINDIR/vdrsync-0.1.3.pl
# Default demux
[ -z "$DEMUXER" ]           && DEMUXER="vdrsync"
[ -z "$TCMPLEX" ]           && export TCMPLEX=tcmplex
[ -z "$DVBMPLEX" ]          && export DVBMPLEX=dvb-mplex
[ -z "$MPLEX" ]             && export MPLEX=mplex
[ -z "$MP2ENC" ]            && export MP2ENC=mp2enc
[ -z "$TCMPLEX_PANTELTJE" ] && export TCMPLEX_PANTELTJE=tcmplex-panteltje
[ -z "$MKISOFS" ]           && export MKISOFS=mkisofs
[ -z "$MENCODER" ]          && export MENCODER=mencoder
[ -z "$LAME" ]              && export LAME=lame
[ -z "$DVDAUTHOR" ]         && export DVDAUTHOR=dvdauthor
[ -z "$TOSVCD" ]            && export TOSVCD=tosvcd
[ -z "$COMPRESS" ]          && export COMPRESS="bzip2 -f"
[ -z "$GOZER" ]             && export GOZER=gozer
[ -z "$RECODE" ]            && export RECODE=recode
[ -z "$SPUMUX" ]            && export SPUMUX=spumux
[ -z "$TRANSCODE" ]         && export TRANSCODE=transcode
[ -z "$ARECORD" ]           && export ARECORD=arecord
# Nice level
[ -z "$PRIO" ]              && export PRIO=15
[ -z "$SLEEP_TIME" ]        && export SLEEP_TIME=10
[ -z "$CHECK_SLEEP_TIME" ]  && export CHECK_SLEEP_TIME=5
# Schalter
[ -z "$PARQ" ]              && export PARQ="none"
[ -z "$CONV_TITLEDATE" ]    && export CONV_TITLEDATE=no
# Project X
[ -z "$LS" ]                && export LS="ls"
[ -z "$XVFB" ]              && export XVFB=Xvfb
[ -z "$XVFB_ID" ]           && export XVFB_ID=1
# Java Pfad
[ -z "$JAVA" ]              && export JAVA=java
[ -z "$OLDPX" ]             && export OLDPX=no
if [ "$OLDPX" = "yes" ]
then
    [ -z "$PX" ]            && export PX=$VDRCONVERTDIR/share/vdrconvert/pX/pX.jar
    [ -z "$PXINI" ]         && export PXINI=$VDRCONVERTDIR/share/vdrconvert/pX/pX.ini
    export PXINIOPT="-c"
    export PXOUTOPT="-o"
else
    [ -z "$PX" ]            && export PX=$VDRCONVERTDIR/share/vdrconvert/pX/PX.jar
    [ -z "$PXINI" ]         && export PXINI=$VDRCONVERTDIR/share/vdrconvert/pX/PX.ini
    export PXINIOPT="-ini"
    export PXOUTOPT="-out"
fi
[ -z "$KILL" ]              && export KILL=killall
# Wine pfad
[ -z "$WINE" ]              && export WINE=wine
# Jobs
[ -z "$CONVERT_JOBS" ]      && export CONVERT_JOBS="vdr2divx vdr2dvd vdr2vcd vdr2svcd vdr2mpg vdr2mp3 vdr2ogg vdr2ac3 burn"
# Monitor
[ -z "$OSDMONITOR" ]        && export OSDMONITOR="yes"
[ -z "$DVDCDLABELGEN" ]     && export DVDCDLABELGEN="$VDRCONVERTDIR/bin/cdlabelgen"
[ -z "$DVDCDLABELCONV" ]    && export DVDCDLABELCONV="$VDRCONVERTDIR/bin/jpeg2eps"

if [ ! -d "$VDRROOT" ]
then
        echo "ERROR : VDRROOT is not available: $VDRROOT"
        logger "$0 : ERROR : VDRROOT is not available: $VDRROOT"

	exit 1
fi

[ "$VERBOSE" = "yes" ] && echo "INFO  : DEMUXER is $DEMUXER"
if [ -z "$VDRCONVERT_ONESHOT" -o "$DISPLAY_XVFB" = "yes" ]
then
	case $DEMUXER in
	    pva|px)
	      echo "INFO  : Start Virtual X Server: $XVFB"
              XAUTHORITY="" $XVFB :${XVFB_ID} -screen ${XVFB_ID} 1024x768x24 >/dev/null 2>&1 &
              XVFB_PID="$!"
		sleep 1
		# Check for PID
		if ! ps -p $XVFB_PID >/dev/null; then
                       echo "ERROR : major problem starting virtual server for pva|px" >&2
                       exit 1
		fi

		echo "INFO  : Start Virtual Server sucessfully: PID=$XVFB_PID"
		export DISPLAY=:${XVFB_ID}.0
		# Allow localhost to connect
		xhost +localhost
		;;
	esac
fi
while true; do	

 [ -d $TEMPDIR ] 	|| mkdir -p $TEMPDIR
 [ -d $LOGDIR ] 	|| mkdir -p $LOGDIR
 [ -d $VCOQUEUEDIR ] 	|| mkdir -p $VCOQUEUEDIR
 [ -d $TEMPDIR ] 	|| mkdir -p $TEMPDIR
 [ -d $DESTDIR ] 	|| mkdir -p $DESTDIR
 
 if [ -w ${TEMPDIR} -a -w ${LOGDIR} -a -w $VCOQUEUEDIR -a -w $TEMPDIR -a -w $DESTDIR -a -w $VCORUNDIR ]
 then
  for JOBFIL in $CONVERT_JOBS
  do
    if [ ! -f $VCOQUEUEDIR/$JOBFIL ]
    then 
	touch $VCOQUEUEDIR/$JOBFIL
    fi

    let LINES=0
    LINES=`cat $VCOQUEUEDIR/$JOBFIL | wc -l`  
    
    if [ $LINES -gt 0 ]
    then
      let run=0
      if [ "$PARQ" = "no" -a "$JOBFIL" != "ripcda" ] #create a var for non checking jobs
      then
	run=`ps -aef|grep $VDRCONVERTBINDIR/${JOBFIL}.sh|grep -v grep|wc -l`
	#JOBPIDFILE="$VCORUNDIR/`basename ${JOBFIL}.sh.pid`"
	#[ -f $JOBPIDFILE ] && let run=`cat $JOBPIDFILE|wc -l`
      fi
      if [ "$PARQ" = "none" ]
      then
	ps -aef > $TEMPDIR/vcorun.$$
        for r in $CONVERT_JOBS
	do
	    if [ $run -eq 0 -a "$r" != "ripcda" ] #create a var for non checking jobs
	    then
		#run=`ps -aef|grep ${r}.sh|grep -v grep|wc -l`
		run=`cat $TEMPDIR/vcorun.$$|grep ${r}.sh|grep -v grep|wc -l`
		#JOBPIDFILE="$VCORUNDIR/`basename ${r}.sh.pid`"
		#[ -f $JOBPIDFILE ] && let run=`cat $JOBPIDFILE|wc -l`

	    fi
	done
	[ -f $TEMPDIR/vcorun.$$ ] && rm $TEMPDIR/vcorun.$$
      fi

      if [ $run -eq 0 ]
      then
          NewJobs=`mktemp ${TEMPDIR}/${JOBFIL}.XXXXXX`
	  status=$?
	  if [ $status -eq 0 ]
	  then
    	      # Define Logfile
    	      CONVERTLOG=${LOGDIR}/${JOBFIL}.log

	      [ -x $VDRCONVERTBINDIR/pre${JOBFIL}.sh ] && $VDRCONVERTBINDIR/pre${JOBFIL}.sh $JOBFIL $CONVERTLOG
	      echo "Start $VDRCONVERTBINDIR/${JOBFIL}.sh $NewJobs $JOBFIL Log = $CONVERTLOG" 
	      if [ ! -z "$VDRCONVERT_ONESHOT" ]
	      then
	        $VDRCONVERTBINDIR/${JOBFIL}.sh $NewJobs $JOBFIL $CONVERTLOG
	      else
		$VDRCONVERTBINDIR/${JOBFIL}.sh $NewJobs $JOBFIL $CONVERTLOG >> $CONVERTLOG 2>&1 &
	      fi
      	      logger "${JOBFIL}.sh $NewJobs $JOBFIL  $CONVERTLOG 2>&1 gestartet"
	  else
	      echo "ERROR : mktemp ${TEMPDIR}/${JOBFIL}.XXXXXX failed."
	      logger " $0 : ERROR : mktemp ${TEMPDIR}/${JOBFIL}.XXXXXX failed." 
	  fi
      fi
    fi
  done

  # One shot run
  if [ ! -z "$VDRCONVERT_ONESHOT" ]; then
       echo "NOTICE: One shot only . $0 exit"
       logger "$0 : NOTICE: One shot only. $0 exit"

       break
  fi


  sleep $CHECK_SLEEP_TIME
 else
      echo "ERROR : Wrong permission . $0 exit"
      logger "$0 : ERROR : Wrong permissions. $0 exit"
 fi
done
if [ ! -z "$VDRCONVERT_ONESHOT" ]
then
       # Cleanup jobs
       #rm -rf $VCORUNDIR/*
       #rm -rf $TEMPDIR/*
       true
fi

# XXX Nur von uns gestartetes Display killen
if [ -z "$VDRCONVERT_ONESHOT" -o "$DISPLAY_XVFB" = "yes" ]
then
    case $DEMUXER in
	pva|px)
              $KILL -15 $XVFB >/dev/null 2>&1
	      sleep 1
              $KILL -9 $XVFB >/dev/null 2>&1
	      ;;
	*)
	      ;;
    esac
fi
