#!/bin/bash
BASEDIR=/mnt/workdisk/tmp
if [ $# -lt 1 ]; then
echo -e "Usage:  makedvd mpeg [mpeg...]"
echo -e "Each mpeg should have space for the VOB headers."
echo -e "'mplex -f 8' has the ability to generate this space."
echo -e "-m will automatically remultiplex any of the mpegs that follow the"
echo -e "option.  -m- will disable it.  Example:"
echo -e "makedvd foo.mpg -m bar.mpg -m- baz.mpg"
echo -e "will deconstruct and remultiple bar.mpg, but not foo.mpg or baz.mpg"
exit 0
fi
THISPROG=`basename $0`
FD=`mktemp -d $BASEDIR/$THISPROG.XXXXXX` || FD=$BASEDIR/$THISPROG.$$
# attempt to make FD just in case mktemp failed
mkdir $FD 2> /dev/null
pos=0;
vobpos=0;
mplex=0;
for i in "$@"; do
    nf=$i
    if [ "x$i" == "x-m" ]; then
	mplex=1
    elif [ "x$i" == "x-m-" ]; then
	mplex=0
    else
	if [ $mplex -eq 1 -a -r "$i" ]; then
	    nf="$FD/mpg$pos"
	    mkfifo "$nf"
	    VOB[$vobpos]=$pos
	    ORIG[$pos]=$i
	    let vobpos=vobpos+1
	fi
	MPGS[$pos]=$nf
	let pos=pos+1
    fi
done
(for i in ${VOB[@]}; do makevob "${ORIG[$i]}" "${MPGS[$i]}" 2> /dev/null; done) &
dvdauthor "${MPGS[@]}"
dvdauthor -T
rm -r -f $FD
