#!/bin/sh
# Version: 0.2.0 Test
# Author: Dimitrios Dimitrakos mail@dimitrios.de
# $Id: vdr2ac3.sh,v 1.6 2005/10/04 13:57:17 dimi Exp $

vco_user=`id -n -u`
homedir=`cat /etc/passwd|grep "^${vco_user}:"|cut -f6 -d ':'`
. $homedir/.vdrconvert/vdrconvert.env
[ -r $homedir/.vdrconvert/vdr2ac3.env ] && . $homedir/.vdrconvert/vdr2ac3.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



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

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


[ -z "$AC3_DESTDIR" ]  && AC3_DESTDIR=$DESTDIR/ac3
[ -z "$AC3_TITLEDATE" ] && AC3_TITLEDATE=$CONV_TITLEDATE


LTEMPDIR=${TEMPDIR}/${JOBFIL}

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

if [ -w ${LTEMPDIR} -a -w ${AC3_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
		if [ -z "$SHORTCIRCUIT_PID" ]; then
			TEMPJOBFILE=$LTEMPDIR/${JOBFIL}.u.$$
		else
			TEMPJOBFILE=$LTEMPDIR/${JOBFIL}.u.$SHORTCIRCUIT_PID
		fi

		> $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
			printf "\t`date +"%T"` Start conversion for List : \n"
			let part=0
			for i in $CMDLIN
			do
				let part=part+1
				printf "\t\t %5d) %s\n" $part "$i"
			done
			logger "$0 : Start conversion for $CMDLIN"
			for i in $CMDLIN
			do
			    [ "$AC3_SPLIT" = "no" ] && p=${i}/ || p=`echo $i/[0-9][0-9[0-9]*.vdr`
			    for j in $p
			    do
				let Number=1
				pxfiles=""
				printf "\n\t`date` : Begin conversion %s\n" "$i"
				UniqueDir[Number]=`mktemp -d ${LTEMPDIR}/VDRSYNC.XXXXXX`
				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
					[ "$AC3_TITLEDATE" = "yes" ] && Titel=${Datum}_${Record} \
						||Titel=${Record}
				else
					[ "$AC3_TITLEDATE" = "yes" ] && Titel=${SubTitel}/${Datum}_${Record} \
						|| Titel=${SubTitel}/${Record}
				fi
				[ ! -d ${UniqueDir[Number]}/${SubTitel} ] && mkdir -p ${UniqueDir[Number]}/${SubTitel}
				[ ! -d ${AC3_DESTDIR}/${SubTitel} ] && mkdir -p ${AC3_DESTDIR}/${SubTitel}
				[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "`echo $0|xargs basename` start for ${Record}" > /dev/null 2>&1
				mkdir -p ${UniqueDir[Number]}
				cd ${UniqueDir[Number]}
				LOG[Number]=${UniqueDir[Number]}/sync.log
				[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "Try demux with $DEMUXER" > /dev/null 2>&1
				printf "\t`date +"%T"`: Start demux with %s\n" ${DEMUXER}
				case `basename $DEMUXER` in

					vdrsync)
						nice -n $PRIO $VDRSYNC -o ${UniqueDir[Number]} $j > ${LOG[Number]} 2>&1
						status=$?
						if [ $status -ne 0 ]
						then
							printf "\t\tERROR : %s  rc=<%d>\n" $VDRSYNC $status
							[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "ERROR : $VDRSYNC  rc=$status" > /dev/null 2>&1
							logger "ERROR : $VDRSYNC  rc=$status"
							# Cleaning
							cd $VDRCONVERTDIR
							rm -rf ${UniqueDir[Number]}
							Exit
						fi
						;;
					vdrsync2)
						nice -n $PRIO $VDRSYNC2 -basename 001 -script-output $VDRSYNC_OPTS -o ${UniqueDir[Number]} $j > ${LOG[Number]} 2>&1
						status=$?
						if [ $status -ne 0 ]
						then
							printf "\t\tERROR : %s  rc=<%d>\n" ${VDRSYNC2} $status
							[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "ERROR : $VDRSYNC2  rc=$status"
							logger "ERROR : $VDRSYNC2  rc=$status"
							# Cleaning
							cd $VDRCONVERTDIR
							rm -rf ${UniqueDir[Number]}
							Exit
						fi
						;;
					vdrsync3)
						nice -n $PRIO $VDRSYNC2 -basename 001 -script-output $VDRSYNC_OPTS -o ${UniqueDir[Number]} $j > ${LOG[Number]} 2>&1
						status=$?
						if [ $status -ne 0 ]
						then
							printf "\t\tERROR : %s  rc=<%d>\n" ${VDRSYNC3} $status
							[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "ERROR : $VDRSYNC3  rc=$status"
							logger "ERROR : $VDRSYNC3  rc=$status"
							# Cleaning
							cd $VDRCONVERTDIR
							rm -rf ${UniqueDir[Number]}
							Exit
						fi
						;;
					px)
						# Virtuellen X-Server starten
						#$XVFB :1.0 >/dev/null 2>&1 &
						#export DISPLAY=:1.0
		
						[ -d $j ] && pxfiles=$j/[0-9][0-9][0-9].vdr || pxfiles=$j
						for l in $vdrfiles
						do
							let len=`$LS -Ls $l|awk '{print $1}'`
							[ $len -gt 1 ] && pxfiles="${pxfiles} ${l}"
						done
	
	
						# Project X starten
						nice -n $PRIO $JAVA -jar $PX $PXINIOPT $PXINI $PXOUTOPT ${UniqueDir[Number]} $pxfiles > ${LOG[Number]} 2>&1
						status=$?
		
						# Virtuellen X-Server beenden
						#$KILL -15 $XVFB >/dev/null
						if [ $status -ne 0 ]
						then
							printf "\t\tERROR : %s  rc=<$status>\n" $PX $status
							[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "ERROR : $PX  rc=$status"
							logger "ERROR : $PX  rc=$status"
							# Cleaning
							cd $VDRCONVERTDIR
							rm -rf ${UniqueDir[Number]}
							Exit
						fi
						;;
					pva)
						# VDR Files zusammenfuehren
						cat $i/[0-9][0-9][0-9].vdr >> ${UniqueDir[Number]}/001.vdr
						[ -f $i/marks.vdr ] && cp $i/marks.vdr ${UniqueDir[Number]}
						[ -f $i/index.vdr ] && cp $i/index.vdr ${UniqueDir[Number]}
						# Winpfad erstellen
						WINEPFAD=`echo ${UniqueDir[Number]}|sed 's/\//\\\/g'`
						WINEFILE="demux \"Z:${WINEPFAD}\\001.vdr\" \"Z:${WINEPFAD}\\001.mpv\" \"Z:${WINEPFAD}\\001.mpa\""
						## Jobfile anlegen
						echo "offeryn pvas job v000001" > ${UniqueDir[Number]}/demux.ajb
						echo "\$JOB" >> ${UniqueDir[Number]}/demux.ajb
						echo "$WINEFILE" >> ${UniqueDir[Number]}/demux.ajb
						cat $VDRCONVERTDIR/share/vdrconvert/pva/demux.template >> ${UniqueDir[Number]}/demux.ajb
		
						#$XVFB :1.0 >/dev/null 2>&1 &
						#export DISPLAY=:1.0
		
						nice -n $PRIO $WINE $VDRCONVERTDIR/share/vdrconvert/pva/cPVAS.exe ${UniqueDir[Number]}/demux.ajb > ${LOG[Number]} 2>&1
		
						[ -f "${UniqueDir[Number]}/001.mpv.ac3" ] && mv ${UniqueDir[Number]}/001.mpv.ac3 ${UniqueDir[Number]}/001.ac3
		
						#$KILL -15 $XVFB >/dev/null
						;;
					*)
						printf "\t\tNo support for Demuxer %s \n" $DEMUXER
						logger "$0 : No support for Demuxer $DEMUXER"
						;;
				esac
		
				if [ -f ${UniqueDir[Number]}/*.ac3 ]
				then
					if [ ! -f ${AC3_DESTDIR}/${Titel}.ac3 ]
					then
						cp ${UniqueDir[Number]}/*.ac3 ${AC3_DESTDIR}/${Titel}.ac3
						status=$?
					else
						let index=1
						until [ ! -f ${AC3_DESTDIR}/${Titel}-${index}.ac3 ]
						do
							let index=index+1
						done
						cp ${UniqueDir[Number]}/*.ac3 ${AC3_DESTDIR}/${Titel}-${index}.ac3
						status=$?
						File=${AC3_DESTDIR}/${Titel}-$index.${AC3_SUFFIX}
						[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "AC3 for `echo $File|xargs basename` success" > /dev/null 2>&1
					fi
					if [ $status -eq 0 ]
					then
						printf "\t\tAC3 for %s success\n" "$File"
						logger "$0 : AC3 for $File success"
					fi
				else
					printf "\t\tERROR : AC3 for %s fail.\n" "${Titel}"
					logger "$0 : AC3 for ${Titel} fail"
				fi
	
	
				cd -
				rm -rf ${UniqueDir[Number]}
				let Number=Number+1
				printf "\t`date` : Finish %s\n" "$i"
			    done
			done
			printf "\t`date +"%T"` Parts from List : \n"
			let part=0
			for i in $CMDLIN
			do
				let part=part+1
				printf "\t\t %5d) %s\n" $part "$i"
			done
			printf "\t`date +"%T"` completed\n"
			cp $NewJobs $NewJobs.$$
			grep -x -v "$CMDLIN" $NewJobs.$$ > $NewJobs 
			rm $NewJobs.$$
		done
		rm $NewJobs
	fi
else
	echo "ERROR : Please check write permissions for ${AC3_DESTDIR} and ${LTEMPDIR}. $0 exit"
	logger "ERROR : Please check write permissions for ${AC3_DESTDIR} and ${LTEMPDIR}. $0 exit"
	[ "$OSDMONITOR" = "yes" ] && $SVDRPSEND MESG "Please check write permissions for ${AC3_DESTDIR} and ${LTEMPDIR}" > /dev/null 2>&1
fi
printf "`date +"%d-%m-%2Y %T"` : Session %s end\n" "$0"
Exit
