#!/bin/sh
# Version: 0.2.0 
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# $Id: vdr2vcd.sh,v 1.5 2004/11/10 15:26:18 dimi Exp $

RM=rm
#RM=echo
vco_user=`id -n -u`
homedir=`cat /etc/passwd|grep "^${vco_user}:"|cut -f6 -d ':'`
. $homedir/.vdrconvert/vdrconvert.env
[ -r $homedir/.vdrconvert/vdr2vcd.env ] && . $homedir/.vdrconvert/vdr2vcd.env


PIDFILE="$VCORUNDIR/`basename ${0}.pid`"
echo "$$" >> $PIDFILE

trap do_clean SIGINT SIGQUIT SIGTERM 
do_clean() {
	if [ -f $PIDFILE ]
	then
	    TEMPPID=$LTEMPDIR/`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
}

Exit() {
	do_clean
}

NewJobs=$1
JOBFIL=$2
LOGFILE=$3


[ "$VCD_DEBUG" = "yes" ] && set -x 

printf "`date +"%d-%m-%2Y %T"` : Session %s begin\n" "$0"


[ -z "$VCD_DESTDIR" ]   && VCD_DESTDIR=$DESTDIR/vcd 
[ -z "$VCD_TITLEDATE" ] && VCD_TITLEDATE=$CONV_TITLEDATE

LTEMPDIR=${TEMPDIR}/${JOBFIL}

[ -d $VCD_DESTDIR ] || mkdir -p $VCD_DESTDIR 
[ -d $LTEMPDIR ] || mkdir -p $LTEMPDIR

if [ -w ${LTEMPDIR} -a -w ${VCD_DESTDIR} ]
then
  let NLLINES=`cat $VCOQUEUEDIR/${JOBFIL}|nl|wc -l|awk '{ print $1 }'`
  let ALINES=`cat $VCOQUEUEDIR/${JOBFIL}|wc -l|awk '{ print $1 }'`

  if [ $ALINES -ge 1 ]
  then
    TEMPJOBFILE=/$LTEMPDIR/${JOBFIL}.u.$$
    > $TEMPJOBFILE
    printf "%s\n" "`cat $VCOQUEUEDIR/${JOBFIL}|head -n 1`" > $NewJobs
    let LINE=2
    while [ $LINE -le $ALINES ]
    do
	printf "%s\n" "`cat $VCOQUEUEDIR/${JOBFIL}|head -n $LINE|tail -n 1`" >> $TEMPJOBFILE
	let LINE=LINE+1
    done
    [ $NLLINES -gt $ALINES ] && printf "%s" "`cat $VCOQUEUEDIR/${JOBFIL}|tail -n 1`" >> $TEMPJOBFILE
    cp $TEMPJOBFILE $VCOQUEUEDIR/${JOBFIL} && $RM $TEMPJOBFILE
    while [ `cat $NewJobs|grep ^${VDRROOT}|wc -l` -gt 0 ]
    do
      CMDLIN=`head -n 1 $NewJobs`   # Job auslesen
      let Number=1
      echo "`date` : Start conversion for $CMDLIN"
      for i in $CMDLIN
      do
          printf "\t`date` : Begin conversion %s\n" "$i"
	  UniqueDir[Number]=`mktemp -d ${LTEMPDIR}/VCD.XXXXXX`
	  mkdir -p ${UniqueDir[Number]}
	  r=`echo $VDRROOT|sed 's/\// /g'|wc -w`
	  spalten=`echo $i|sed 's/\// /g'|wc -w`
	  Record=`echo $i|sed 's/\%//g'|cut -f $spalten -d '/'`
	  [ $[$r+2] -lt $[$spalten-1] ] && SubTitel=`echo $i|sed 's/\%//g'|cut -f $[$r+2]-$[$spalten-1] -d '/'` || SubTitel=""
	  Datum=`basename $i|cut -f1 -d'.'`
	  if [ -z "$SubTitel" ]
	  then
		[ "$VCD_TITLEDATE" = "yes" ] && Titel=${Datum}_${Record} ||Titel=${Record}
	  else
		[ "$VCD_TITLEDATE" = "yes" ] && Titel=${SubTitel}/${Datum}_${Record} || Titel=${SubTitel}/${Record}
	  fi
	  [ ! -d ${UniqueDir[Number]}/${Titel} ] && mkdir -p ${UniqueDir[Number]}/${Titel}
	  cd ${UniqueDir[Number]}/${Titel}
          printf "\t`date +"%T"`: Start TOSVCD for %s.\n" ${Titel} 
	  [ ! -z "${VCD_OPTS}" ] && printf "\t`date +"%T"`: Opts : %s\n" "${VCD_OPTS}" 
	  LOG[Number]=${UniqueDir[Number]}/vcd.log
          nice -n $PRIO $TOSVCD ${VCD_OPTS} -1 -a -o ${Titel} $i >${LOG[Number]} 2>&1
	  [ "$DEBUG" = "yes" ] && cat ${LOG[Number]}
	  if [ ! -d ${VCD_DESTDIR}/${Titel} ]
	  then
		    mkdir -p ${VCD_DESTDIR}/${Titel}
	            mv ${UniqueDir[Number]}/${Titel}/${Titel}*.bin ${VCD_DESTDIR}/${Titel}
	            mv ${UniqueDir[Number]}/${Titel}/${Titel}*.cue ${VCD_DESTDIR}/${Titel}
		    File=${VCD_DESTDIR}/${Titel}
	  else
		    let index=1
		    until [ ! -d ${VCD_DESTDIR}/${Titel}-${index} ]
		    do
		        let index=index+1
		    done
		    mkdir -p ${VCD_DESTDIR}/${Titel}-${index}
	            mv ${UniqueDir[Number]}/${Titel}/${Titel}*.bin ${VCD_DESTDIR}/${Titel}-${index}
	            mv ${UniqueDir[Number]}/${Titel}/${Titel}*.cue ${VCD_DESTDIR}/${Titel}-${index}
		    File=${VCD_DESTDIR}/${Titel}-${index}
	  fi
	  cd -
	  if [ "$VCD_AUTOWRITE" = "yes" ] 
	  then
	    for burnfile in ${File}/*.cue
	    do
	       echo "$burnfile:CUE:DEL" >> $VCOQUEUEDIR/burn
	    done	
	  fi
     	  $RM -rf ${UniqueDir[Number]}
          printf "\t`date` : Finish %s\n" "$i"
     done
     echo "`date` : Parts from $CMDLIN completed"
     cp $NewJobs $NewJobs.$$
     grep -x -v "$CMDLIN" $NewJobs.$$ > $NewJobs 
     $RM $NewJobs.$$
   done
   $RM $NewJobs
  fi
else
      echo "ERROR : ${VCD_DESTDIR} or ${LTEMPDIR} are not writable. $0 exit"
fi
printf "`date +"%d-%m-%2Y %T"` : Session %s end\n" "$0"
Exit
