#!/bin/sh

# For DEBUG Output - can be left since logfiles are deleted when job finishes
set -x

# To make the script exit whenever a command fails (MUST NOT BE REMOVED)
set -e

# Variables passed in:
# $RECORDING_PATH           Path of VDR recording (/video0/%Bla/2004-01-...rec)
# $MPEG_PATH                Path where streams and movie are stored
# $VIDEO_FILE               Full path- and filename of demuxed video stream
# $AUDIO_FILES              Space-separated list of demuxed audio streams
# $MOVIE_FILE               Full path- and filename of muxed movie
# $REQUANT_FACTOR           Factor by that video shall be shrinked
# $IGNORE_TRACKS            Comma-separated list of audio track IDs to be 
#                           ignored
# $USED_TRACKS              Comma-separated list of audio track IDs to be used
# $DVDAUTHOR_XML            Full path- and filename of the DVDAuthor XML
# $DVDAUTHOR_PATH           Full path of the written DVD structure
# $ISO_FILE                 Full path- and filename of target ISO
# $ISO_PIPE                 Fifo where the created ISO should be piped into
# $DVD_DEVICE               Full path- and filename of the DVD burner device

if [ -z $JAVA_HOME ]; then
	export JAVA_HOME=/opt/j2re1.4.2
fi

if [ -z $PROJECTX_HOME ]; then
	export PROJECTX_HOME=/opt/ProjectX
fi

case $1 in
	render)
		# XXX TODO !!!!
		png2yuv -j $MENU_BACKGROUND -f 25 -n 1 -I t -L 1 | \
		mpeg2enc -f 8 -n p -o /tmp/vdrburn-menu-bg.m2v
		mplex -f 8 -o /dev/stdout /tmp/vdrburn-menu-bg.m2v $MENU_SOUNDTRACK | \
		spumux -v 2 $MENU_XML > $MENU_MPEG
	;;

	demux)
		REQUANTCMD=""
		if [ ! -z $REQUANT_FACTOR ]; then
			REQUANTCMD="requant $REQUANT_FACTOR |"
		fi

		IGNORE=""
		if [ ! -z $IGNORE_TRACKS ]; then
			IGNORE="-ignore $IGNORE_TRACKS"
		fi

		vdrsync.pl -o $MPEG_TMP_PATH \
			-v-filter "$REQUANTCMD burn-buffers" \
			-a-filter "burn-buffers" \
			-ac3-filter "burn-buffers" \
			$IGNORE "$RECORDING_PATH"
	;;

	demuxpx)
		test -e $MPEG_TMP_PATH/convert && rm $MPEG_TMP_PATH/convert
		ln -s "$RECORDING_PATH" $MPEG_TMP_PATH/convert

		$JAVA_HOME/bin/java -Djava.awt.headless=true \
				-jar $PROJECTX_HOME/ProjectX.jar \
				-ini $PROJECTX_HOME/VDR.ini \
				-id $USED_TRACKS \
				-demux -out $MPEG_DATA_PATH -name pjx \
				$(ls $MPEG_TMP_PATH/convert/[0-9][0-9][0-9].vdr)

		if [ ! -z $REQUANT_FACTOR ]; then
		   if [ -e "$MPEG_DATA_PATH/pjx.mpv" ]; then
			 cat "$MPEG_DATA_PATH/pjx.mpv" | requant $REQUANT_FACTOR \
				> "$MPEG_TMP_PATH/vdrsync.mpv" \
				&& rm -f "$MPEG_DATA_PATH/pjx.mpv" &
		   fi
		else
		   if [ -e "$MPEG_DATA_PATH/pjx.mpv" ]; then
			 cat "$MPEG_DATA_PATH/pjx.mpv" > "$MPEG_TMP_PATH/vdrsync.mpv" \
			 	&& rm -f "$MPEG_DATA_PATH/pjx.mpv" &
		   fi
		fi

		if [ -e "$MPEG_DATA_PATH/pjx.mpa" ]; then
			cat "$MPEG_DATA_PATH/pjx.mpa" > "$MPEG_TMP_PATH/vdrsync0.mpa"  \
				&& rm -f "$MPEG_DATA_PATH/pjx.mpa" &
		fi
		if [ -e "$MPEG_DATA_PATH/pjx[1].mpa" ]; then
			cat "$MPEG_DATA_PATH/pjx[1].mpa" > "$MPEG_TMP_PATH/vdrsync1.mpa" \
				&& rm -f "$MPEG_DATA_PATH/pjx[1].mpa" &
		fi
		if [ -e "$MPEG_DATA_PATH/pjx[2].mpa" ]; then
			cat "$MPEG_DATA_PATH/pjx[2].mpa" > "$MPEG_TMP_PATH/vdrsync2.mpa" \
				&& rm -f "$MPEG_DATA_PATH/pjx[2].mpa" &
		fi
		if [ -e "$MPEG_DATA_PATH/pjx.ac3" ]; then
			cat "$MPEG_DATA_PATH/pjx.ac3" > "$MPEG_TMP_PATH/vdrsync.ac3" \
				&& rm -f "$MPEG_DATA_PATH/pjx.ac3" &
		fi

		wait
	;;

	mplex)
		mplex -f 8 -o $MOVIE_FILE $VIDEO_FILE $AUDIO_FILES
	;;

	author)
		dvdauthor -x $DVDAUTHOR_XML
	;;

	mkiso)
		mkisofs -V "$DISC_ID" -dvd-video $DVDAUTHOR_PATH >"$ISO_FILE"
	;;

	burndir)
		SPEED=""
		if [ $BURN_SPEED -gt 0 ]; then
			SPEED="-speed=$BURN_SPEED"
		fi
		growisofs -use-the-force-luke=tty $SPEED -dvd-compat -Z $DVD_DEVICE \
				  -V "$DISC_ID" -dvd-video $DVDAUTHOR_PATH
	;;

    burndircd)
        SPEED=""
        if [ $BURN_SPEED -gt 0 ]; then
        	SPEED="speed=$(($BURN_SPEED * 4))"
        fi
        mkisofs -V "$DISC_ID" -dvd-video $DVDAUTHOR_PATH \
			| cdrecord dev=$DVD_DEVICE driveropts=burnfree fs=10m \
					   $SPEED - 2>&1 >> /tmp/cdrecod.log
    ;;

	pipeiso)
		mkisofs -V "$DISC_ID" -dvd-video $DVDAUTHOR_PATH \
			| tee "$ISO_FILE" > "$ISO_PIPE"
	;;

	burniso)
		SPEED=""
		if [ $BURN_SPEED -gt 0 ]; then
			SPEED="-speed=$BURN_SPEED"
		fi
		growisofs -use-the-force-luke=tty $SPEED -dvd-compat \
				  -Z $DVD_DEVICE=$ISO_PIPE
	;;

	burnisocd)
		SPEED=""
		if [ $BURN_SPEED -gt 0 ]; then
			SPEED="speed=$(($BURN_SPEED * 4))"
		fi
		cdrecord dev=$DVD_DEVICE driveropts=burnfree $SPEED fs=10m $ISO_PIPE \
			2>&1 >> /tmp/cdrecord.log
	;;

	*)
		echo "Usage: $0 {demux}"
		exit 1
	;;
esac

exit $?
