#!/bin/sh
# dvdgen - generate a video DVD image
# (c) 2002-2003 Ark Linux (http://www.arklinux.org/)
# Written by Bernhard Rosenkraenzer <bero@arklinux.org>
#
# Hereby released under the terms of the GNU General Public License
# (GPL) version 2, or, if (and only if) version 2 is ruled invalid
# in a court of law, any later version of the GPL.
#
version() {
	cat >&2 <<EOF
dvdgen 0.1.4
(c) 2002-2003 Ark Linux (http://www.arklinux.org/)
Written by Bernhard Rosenkraenzer <bero@arklinux.org>
EOF
}
usage() {
	version
	[ -n "$1" ] && echo $1 >&2
	cat >&2 <<EOF
Usage:
	dvdgen [options] input1.mpg ...
Options:
	-o filename.img, --output filename.img
		Change output filename to filename.img
		(default: dvd.img)
	-k, --keep
		Keep temporary files (uses up lots of
		diskspace)
	-t dir, --tmp dir
		Create temporary files in directory dir
		(default: autogenerated using mktemp)
		(mostly for use with -k/--keep)
	-r WIDTHxHEIGHT, --resize WIDTHxHEIGHT
		Resize the video to WIDTHxHEIGHT pixels
		(default: keep the resolution of the
		video file)
		You can specify some special values:
		-r PAL = 720x576 (normal PAL resolution)
		-r NTSC = 720x480 (normal NTSC resolution)
	-n, --noimg
		Don't run mkisofs, just generate the
		vob/ifo files and directory structure
	-h, --help
		Display this message and exit
	-v, --version
		Display version information and exit
EOF
}
OUT="dvd.img"
KEEP=""
TMP=""
NOIMG=""
RESIZE=""
DIR="`pwd`"
export LANG=C # We need to parse the output of other apps
while [ "`echo $1 |cut -b1`" = "-" ]; do
	if [ "$1" = "-o" -o "$1" = "--output" ]; then
		shift
		OUT="$1"
	elif [ "$1" = "-k" -o "$1" = "--keep" ]; then
		KEEP=1
	elif [ "$1" = "-t" -o "$1" = "--tmp" ]; then
		shift
		TMP="$1"
	elif [ "$1" = "-n" -o "$1" = "--noimg" ]; then
		NOIMG=1
	elif [ "$1" = "-r" -o "$1" = "--resize" ]; then
		shift
		if [ "$1" = "PAL" -o "$1" = "pal" ]; then
			RESIZE="-Z720x576 -f 25"
		elif [ "$1" = "NTSC" -o "$1" = "ntsc" ]; then
			RESIZE="-Z720x480 -f 29.970"
		else
			RESIZE="-Z$1"
		fi
		RESIZE="$RESIZE"
	elif [ "$1" = "-h" -o "$1" = "--help" ]; then
		usage
		exit 0
	elif [ "$1" = "-v" -o "$1" = "--version" ]; then
		version
		exit 0
	else
		usage "Warning: unknown parameter $1"
	fi
	shift
done
[ "`echo $OUT |cut -b1`" != "/" ] && OUT="$DIR/$OUT"
if [ "$#" = "0" ]; then
	usage "No input files found."
	exit 1
fi
if [ -z "$TMP" ]; then
	TMP="`mktemp -d /tmp/dvdXXXXXX`"
else
	[ "`echo $TMP |cut -b1`" != "/" ] && TMP="$DIR/$TMP"
	rm -rf $TMP
fi
WORK="`mktemp -d /tmp/dvdXXXXXX`"
pushd $WORK
mkdir -p $TMP/VIDEO_TS $TMP/AUDIO_TS
for i in `seq 1 $#`; do
	FN="track$i.vob"
	INPUT="$1"
	ARGS=""
	[ "`echo $INPUT |cut -b1`" != "/" ] && INPUT="$DIR/$INPUT"
	if echo $INPUT | grep -qE '\.a[0-9]+\.avi$'; then
		# The Archimedis VCR marks the aspect ratio of MPEG-4 files
		# in the filename because normal AVI files don't remember it...
		# Detect and handle those files, adjusting to the closest match.
		# This is greatly suboptimal, but better than nothing
		# (transcode's --import_asr just isn't flexible enough).
		ASPECT=`echo $INPUT | sed -e 's,\.avi$,,;s,.*\.a,,'`
		if [ $ASPECT -le 110 ]; then
			ARGS="--import_asr 1" # <= 1.1:1 -> 1:1
		elif [ $ASPECT -le 155 ]; then
			ARGS="--import_asr 2" # <= 1.55:1 -> 4:3 (1.33:1)
		elif [ $ASPECT -le 199 ]; then
			ARGS="--import_asr 3" # <= 1.99:1 -> 16:9 (1.77:1)
		else
			ARGS="--import_asr 4" # >  1.99:1 -> 2.21:1
		fi
	fi
	# TODO: -A etc. for AC3, ogg support
	ARGS="$ARGS -N 0x50 -y mpeg -V -x mplayer -F d"
	if ! transcode -i "$INPUT" $ARGS -o tmp $RESIZE --keep_asr; then
		# Try without --keep_asr
		if ! transcode -i "$INPUT" $ARGS -o tmp $RESIZE; then
			# The stream seems to be broken, and mencoder is less
			# picky. This is UGLY, but works
			ASPECT=`mencoder -oac copy -ovc copy "$INPUT" -o "$TMP/fixedstream.avi" 2>/dev/null |grep aspect |sed -e 's,.*aspect *,,;s,).*,,'`
			if ! echo $ARGS |grep -q import_asr; then
				[ -n "$ASPECT" ] && ARGS="$ARGS --import_asr $ASPECT"
			fi
			if ! transcode -i "$TMP/fixedstream.avi" $ARGS -o tmp $RESIZE --keep_asr; then
				if ! transcode -i "$TMP/fixedstream.avi" $ARGS -o tmp $RESIZE; then
					echo "Is this really a video??" >&2
				fi
			fi
			rm -f "$TMP/fixedstream.avi"
		fi
	fi
	tcmplex -o $FN -i tmp.m2v -p tmp.mpa -m d
	VOBS="$VOBS $FN"
	if [ -z "$KEEP" ]; then
		rm -f tmp.m2v tmp.mpa
	else
		mv -f tmp.m2v track$i.m2v
		mv -f tmp.mpa track$i.mpa
	fi
	shift
done
ifogen -o "$TMP/VIDEO_TS/VTS" $VOBS
tocgen "$TMP/VIDEO_TS"
[ -z "$KEEP" ] && rm $VOBS
if [ -z "$NOIMG" ]; then
	mkisofs -dvd-video -udf -o "$OUT" "$TMP"
	rm -rf "$TMP"
fi
popd
rm -rf "$WORK"
