#!/bin/bash
#    dvd-menu
#    Copyright 2003-2006 Scott Dylewski  <scott at dylewski.com>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

name='dvd-menu'
version='0.7.5'

echo "[dvd-menu]            dvd-menu $version"
echo "[dvd-menu]            Licensed under the GNU GPL"
echo "[dvd-menu]            Copyright 2003-2006 by Scott Dylewski"
echo "[dvd-menu]            "

## TODO:
# read in variables from ~/.dvd-menurc and local .dvd-menurc

## Known bugs:

changes () 
{
echo 'Changes:
0.7.5	
	Allow embedded .png to keep transparency	
	Fix error message quotes when looking for XML file existing.
0.7.4	no changes
0.7.3	
	** Changed output directory option to be the same as dvd-slideshow
	Spaces in filenames should work now.
	Checks that xml files exist before proceeding.
	Added option to pass an embedded image (-e image.jpg). Thanks Marcus Brown
	Added "upAction" option for use with dvd-bakery. Thanks Marcus Brown!
	Fixed bug in pal mode variable passing.	 (thanks Marcus Brown)
	Changed head 4 to head -n 4
	Added variable "mpeg2enc_params" for easier changing of mpeg2enc parameters.	
	Get rid of rpm checks.
	Added low quality mode -L for use with dvd-slideshow.
	Added -nocleanup option to save files in temp directory for debugging.
	Added "-compose src-over" to all composite lines for compatibility with Debian Etch.
	Added -H high-quality option as a placeholder for future use
	Added -smp option as a dummy option for compatibility with dvd-slideshow
0.7.2	
	Better checking for correct versions of required programs.
	Logfile output to dvd-menu.log
	Reads default variables from ~/.dvd-slideshowrc	
	Obeys "autocrop" option	
	Pixel aspect ratio should be correct now.
0.7.1	Automatically creates output directory if it does not exist.
	Added option -nomenu if you do not want to create a menu
	AC3 is default audio type now.  Pass -mp2 if you want to 
	force mp2 audio.
0.7.0	Added option -iso option to create the iso image
	(requires mkisofs)
0.6.2	Removed -c and -R 2 options from mpeg2enc because some versions
	do not have those switches.
	Works with toolame 0.2m now.
	Got rid of calls to NetPBM functions, so it is no longer required...
0.6.0	Added AC3 audio option (requires ffmpeg > 0.4.8).
	Mplexed files are now correctly labeled with the .vob extension.
        Better font searching (thanks Jim Crumley)
0.5.4	No changes.
0.5.2	Fixed audio bug when no audio was used. (re-mplex error)
	Added -c continuous play option (<post> jumps to next title).
	Added -D do not run dvdauthor option.
	Added -W do not create a 50% white background under menu text.
	dvdauthor xml structure changed slightly
	DVD Title (-n) is now optional.
	Added man page.
	Checks for nice fonts.
0.5.0	Changed name to "dvd-menu"
	Only requires lame or oggdec if you are using that file format.
	Fixed some audio bugs (sampling) so it should work better.
	Will automatically delete AUDIO_TS and VIDEO_TS directories
	in your output directory if they already exist.
0.4.6	Fixed bug when checking for toolame (thanks Scott Merrill)
	Removed -t option from toolame for better compatibility with old versions.	
0.4.2	Changed location of main title slightly lower.
	Fixed one hardcoded 720x480 image so PAL option should work better.
	Added -depth 8 to ImageMagick calls to fix ppmtoy4m errors. 
	Upgraded main menu background when no background is passed.
0.4	Fixed bug when no background file was passed. Works now.
	Ogg audio format supported now.
	Uses toolame for mp2 audio encoding when available.
	-p (PAL format option) added.  Not tested, but should work.
	Menu button white background is now 50% transparent.
0.3	Added section to check for required programs
	If no background passed, a black one will be used.
0.2	Initial release'
}

help ()
{
echo "dvd-menu is part of dvd-slideshow $version"
echo "`basename $0`"
echo "http://freshmeat.net/dvdslideshow/"
echo 'Copyright 2003 Scott Dylewski <scott at dylewski.com>
 
Usage: 
  dvd-menu -t <titleset1> ... -t <titlesetN>
  -f <xmlfile1> ... -f <xmlfileN> [-o output_directory]
  [-n "menu title"] [-e <embed image>] [-L]
  [-b <background jpeg>] [ -a <menu audio file> ] [-p]
  [-c] [-D] [-W] [-mp2] [-iso] [-nomenu] 

Description: 
	Creates a simple vmgm menu with buttons that
	link to the mpeg files in the specified 
	dvdauthor-compatible xml files. 
	
Options: 
 -t <titleset1> -t <titleset2> ... -t <titlesetN>
	Name of each title.  Each string will be used for 
	the button menu corresponding to each xmlfile.
	This sets the order of the menu buttons!

 -f <xmlfile1> <xmlfile2> ...
	Name of each titleset xml file.  You can use
	dvd_slideshow to generate one for each slideshow
	or you can create your own for a simple mpeg2
	file.

 [-e <embedded image>]
	Embed a jpeg image in the middle of the screen.
	If one image is passed for each button text, 
	That image will be shown as each button is active.

 [-o <output directory>]
	Directory where the output dvd file system resides

 [-b <background jpeg>]
	Image to put in the background of the menu.

 [-a <audiofile>]
	Audio file to use in the background of the
	menu.  The full audio file will keep looping.
	Supports mp3, ogg, or wav.

 [-mp2]
        Use MP2 audio by default.  AC3 seems to be more stable
	in hardware players, but requires ffmpeg.

 [-n "dvd_title"]
        This string will be placed on the top of the
        menu page.

 [-p]
        Use PAL format instead of NTSC (untested initial support)

 [-c]
        Use continuous method.  Each video plays one after another.

 [-W]
        Do not generate 50% white background behind menu text.

 [-L]
        Use low quality mode for compatibility with the same option
	in dvd-slideshow.  This renders half-resolution menus.

 [-D]
        Do not call dvdauthor at the end.  Useful when you want
	to edit your own xml file and pass it to dvdauthor by hand.

 [-iso]
        Create iso image with mkisofs when finished.

 [-nomenu]
        Do not create a menu.  Useful for just finishing the 
	dvdauthor xml file from one video that you want to just
	start playing when the user puts it into a dvd player.

 [-nocleanup]
        Save files in temp directory for debugging.

 -h or -help 
   Prints this help. 

Requires:
	dvdauthor 0.6.11
	sox
	mjpegtools  (mp2enc, mpeg2enc, ppmtoy4m)
	ImageMagick (convert)
 '
}

if [ $# -lt 1 ]; then
	help
	exit 1
fi

# [-button <button image.jpg>]
#	Use image.jpg for a highlight image button.
#	The text from -t <titleset> will be placed underneath.
#	Pass one -b <image.jpg> for each titleset xml file.
#


##################################################################
## Functions:

myecho ()
{
	## use this version of echo to write to screen and to the logfile:
	echo "$*"
	echo "$*" >> "$logfile"
}

logecho ()
{
	## use this version of echo to write to the logfile:
	echo "$*" >> "$logfile"
}

myechon ()
{
	## use this version of echo to write to screen and to the logfile:
	echo -n "$*"
	echo -n "$*" >> "$logfile"
}


check_rm () # check_rm checks to see if the file exists before it's deleted:
{
	if [ -f "${1}" ] ; then
		rm "${1}"
	fi
}

cleanup ()
{
	## clean up temporary files
	myecho "[dvd-menu] cleanup..."
	check_rm "$tmpdir/slideshow_background.ppm"
#	check_rm "$tmpdir/silence.mp2"
	check_rm "$tmpdir/button_background.png"
	check_rm "$tmpdir/button_text.png"
	check_rm "$tmpdir/button_mask.png"
	check_rm "$tmpdir/menu.ppm"
	check_rm "$tmpdir/menu.mpg"
	check_rm "$tmpdir/menu.spumux"
	check_rm "$tmpdir/menu_mask.png"
	check_rm "$tmpdir/menu_t.mpg"
	check_rm "$tmpdir/menu_t.vob"
	check_rm "$tmpdir/menu_audio.wav"
	check_rm "$tmpdir/audio.mp2"
	check_rm "$tmpdir/audio.wav"
	check_rm "$tmpdir/audio.ac3"
	check_rm "$tmpdir/embed.jpg"
	check_rm "$tmpdir/dvd_title.png"
	check_rm "$tmpdir/titles_vmgm.xml"
        k=0
        dk=0
        for file in "${title[@]}"; do
                [ $k -lt 10 ] && dk="0$k" || dk=$k
#              	echo "k=$k  dk=$dk"
                check_rm "$tmpdir"/title_"$dk"_mask.png
                check_rm "$tmpdir"/title_"$k"_mask.png
                check_rm "$tmpdir/title_$dk.ppm"
                check_rm "$tmpdir/title_$k.ppm"
                let k=$k+1
        done
	rmdir "$tmpdir"
}

forcequit () ## function gets run when we have some sort of forcequit...
{
	## clean up temporary files
	cleanup
	exit 1
}

trap 'forcequit' INT
trap 'forcequit' KILL
trap 'forcequit' TERM

## check for the necessary programs:
checkforprog ()
{
        it=`which $1`
        if [ -z "$it" ] ; then
                myecho "[dvd-menu] ERROR:  $1 not found! "
                myecho "[dvd-menu] Check the dependencies and make sure everything is installed."
                exit 1
        fi
}

rpmversion ()
## don't use this since most machines don't have rpm!
{
        if [ -z `which rpm 2> /dev/null` ] ; then
                ver=''
        else
                ver=`rpm -q $1`
        fi
        if [ `echo $ver | awk -F- '{print $1}'` == "$1" ] ; then
                # rpm returned version of program
                version=`echo $ver | awk -F- '{print $2}'`
#                myecho "[dvd-menu] Found $1 version $version"
        else
                # no rpm, try other methods
                version=0  # no version found (yet)
#                myecho "[dvd-menu] Found $1"
        fi
        echo "$version"  # returns 0 if no version found, but executable exists
}

hms ()
{
        ## pass a number in hundreths of seconds and get back a
        ## time code of the form HR:MM:SS:HU
        hours=$(( $1 / 360000 ))
        it=$(( $1 - $hours * 360000 ))
        minutes=$(( $it / 6000 ))
        it=$(( $1 - $minutes * 6000 ))
        seconds=$(( $it / 100 ))
        hundreths=$(( $it - $seconds * 100 ))
        it="$hours:$minutes:$seconds.$hundreths"
        echo "${it}"
}

white_button_mask ()
{
	# white_button_mask outfile
	convert -size ${width}x$menuh -quality 100 xc:transparent -depth 8 -fill white \
	-draw "roundRectangle $menu_location_x,$menu_location_y,$menuright,$menubottom $radius,$radius" \
	-draw "Rectangle 0,0,$width,$title_white_height" -crop $resolution-0-0 "$1"
}

title_text ()
{
	# title_text outfile
	convert -depth 8 -size $resolution xc:transparent -fill black \
	-pointsize $title_font_size -gravity NorthWest $font \
	-draw "text $title_location_x,$title_location_y \"${dvd_title}\"" "$1" 
}

imagewidth ()
{
	it="`identify "$1" | awk -F'JPEG ' '{print $2}' | cut -d 'x' -f 1`"
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'PNM ' '{print $2}' | cut -d 'x' -f 1`"
	fi
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'PNG ' '{print $2}' | cut -d 'x' -f 1`"
	fi
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'MPC ' '{print $2}' | cut -d 'x' -f 1`"
	fi
	it="$(( $it * $sq_pixel_multiplier / 1000 ))"
	echo "$it"
}

imageheight ()
{
	it="`identify "$1" | awk -F'JPEG ' '{print $2}'  | cut -d 'x' -f 2 | cut -d ' ' -f 1`"
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'PNM ' '{print $2}'  | cut -d 'x' -f 2 | cut -d ' ' -f 1`"
	fi
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'PNG ' '{print $2}'  | cut -d 'x' -f 2 | cut -d ' ' -f 1`"
	fi
	if [ -z $it ] ; then
		it="`identify "$1" | awk -F'MPC ' '{print $2}'  | cut -d 'x' -f 2 | cut -d ' ' -f 1`"
	fi
	echo "$it"
}

checkforautocrop ()
{
	if [ "$autocrop" -eq 1 ] ; then
		# figure out whether to autocrop the image or not
	        image_width=`imagewidth "$1"`
	        image_height=`imageheight "$1"`
	        ratio="$(( 100* $image_width / $image_height ))"
	        out_ratio="$(( 100* $width / $height ))"
		do_autocrop_w=0 ; do_autocrop_h=0
		if [ "$ratio" -lt "$(( $out_ratio ))" ] ; then
			do_autocrop_h=1 # image too wide, crop height
		elif [ "$ratio" -gt "$(( $out_ratio ))" ] ; then
			do_autocrop_w=1 # image too tall, crop width
		fi
		[ $debug -ge 2 ] && myecho "[dvd-menu:checkforautocrop] image_width=$image_width image_height=$image_height ratio=$ratio out_ratio=$out_ratio"
		[ $debug -ge 2 ] && myecho "[dvd-menu:checkforautocrop] do_autocrop_w=$do_autocrop_w  do_autocrop_h=$do_autocrop_h"
	else
		do_autocrop_h=0 ; do_autocrop_w=0
	fi
}

background ()
{
		## input is:  $1 = effect/image
		# output is written to "$tmpdir"/slideshow_background.ppm 
		bg="$1"
		if [ -f "$bg" ] ; then # if effect is a background file
			myecho "[dvd-menu] Creating background image ${bg}"
			checkforautocrop "$bg"
                       if [ "$do_autocrop_w" -eq 1 ]; then   
                               # autocrop background image width (width too large)
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize x"$height" \
                               -gravity center -crop "$width"x"$height"'+0!+0!' -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
                       elif [ "$do_autocrop_h" -eq 1 ]; then
                               # autocrop background image height (height too large)
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize "$width"x \
                               -gravity center -crop "$width"x"$height"'+0!+0!'  -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
                       else
                               #don't autorop
                               convert "${bg}" -resize "$sq_to_dvd_pixels" -resize x"$height" -bordercolor black -border "$width"x240 \
                               -gravity center -crop "$width"x"$height"'+0!+0!' -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
                       fi
			bgfile="$bg"
		elif [ "$bg" == 'black' ] ; then  # I guess we could add other types of backgrounds here!
			## use plain black background with no picture
			myecho "[dvd-menu] Creating black background"
			convert -size "$width"'x'"$height" xc:black -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
			bgfile="black"
		elif [ "$bg" == 'white' ] ; then  # I guess we could add other types of backgrounds here!
			## use plain white background with no picture
			myecho "[dvd-menu] Creating white background"
			convert -size "$width"'x'"$height" xc:white -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
			bgfile="white"
		elif [ "${bg:0:1}" == '#' ] ; then  # user passed a #RRGGBB hex color
			myecho "[dvd-menu] Creating $bg color background"
			convert -size "$width"'x'"$height" xc:"$bg" -type TrueColor -depth 8 "$tmpdir"/slideshow_background.ppm
			bgfile="hex"
		fi
#		convert "$tmpdir/slideshow_background.ppm" -type TrueColor -depth 8 "$tmpdir/slideshow_background.mpc"
}


########################################### end of functions


###################################################################
# Default variables
# order of perference:
# program defaults --> ~.dvd-slideshowrc --> .txtfile settings

## setup program default variables (user configurable)
debug=0  # 1 or 0
pal=0
continuous=0
no_dvdauthor=0
no_white=0
ac3=1
iso=0
no_menu=0
autocrop=1
upAction=0
nocleanup=0

# define some possible fonts:
font_dir="/usr/share/fonts/"
font1='n019004l.pfb' # helvetica bold URW fonts
font2='helb____.ttf' # helvetica bold truetype

## other cool colors:  cadetblue cornflowerblue midnightblue
bg_color='steelblue'  # imagemagick colors
menu_height=28
menu_width=288
text_size=$(( 3 * $menu_height / 4 ))
text_loc=$(( $menu_height/2 + $text_size/2 ))
menu_location_x=360
menu_location_y=175  # distance of the top of the first menu item to the top of the screen
title_font_size=40
title_white_height=125
radius=20
title_location_x=80
title_location_y=50

hilight_border=50
hilight_spacing=20
hilight_font_size=12

embed_image_width=260
embed_image_height=240
frames=20 	# number of frames in the menu mpg file. 
		# I guess this only really needs to be 1 frame
		# but 30 is easier to debug so we can play it.
		## don't forget that the audio is fixed at 1 second!

# soon to be user configurable:
logfilename='dvd-menu.log'
low_quality=0
high_quality=0

mpeg2enc_params='-v 0 -a 2 -q 4 -4 2 -2 1 -s -M 0 -f 8'
#mpeg2enc_params='-v 0 -a 2 -q 8 -s -M 0 -f 8 -b 6000 -I 0'
# mpeg2enc_params='-v 0 -a 2 -q 4 -4 2 -2 1 -s -M 0 -f 8 -b 6000 -I 0'
# end of default  variables


#########################################################
## read user rc file, if it exists:
if [ -f "${HOME}/.dvd-slideshowrc" ] ; then
        myecho "[dvd-menu] Reading default variables in ${HOME}/.dvd-slideshowrc"
        . "${HOME}/.dvd-slideshowrc"  # insecure?  maybe do a loop...
fi

#####################################################################
# read command-line options:

let n=0
let m=0
let q=0
let p=0
for arg
do
	case "$arg" in
	-t) shift ; title[$n]="${1}"; let n=$n+1; shift ;;  # each title
	-f) shift ; xmlfile[$m]="${1}"; let m=$m+1; shift ;;  # each titleset
	-e) shift ; embedfile[$p]="${1}"; let p=$p+1; shift ;;  # the menu hilight images
        -u) shift; upAction="1" ;;
#	-button) shift ; buttonfile[$q]="${1}"; let q=$q+1; shift ;;  # each button hilight images
	-o) shift; outdir="${1%%/}"; shift ;;  # dvd directory
	-b) shift; bgfile="$1"; shift ;;  # background jpeg
	-n) shift; dvd_title="$1"; shift ;; # name of DVD disk
	-p) shift; pal=1 ;;
	-c) shift; continuous=1 ;;
	-D) shift; no_dvdauthor=1 ;;
	-W) shift; no_white=1 ;;
	-L) shift; low_quality=1 ;;
	-H) shift; high_quality=1 ;;  # not used yet...
	-smp) shift ;;  # not used yet...
	-nomenu) shift; no_menu=1 ;;
	-nocleanup) shift; nocleanup=1 ;;
	-a) shift; audiofile="$1"; shift;; # audio file playing during menu
        -ac3) shift; ac3=1 ;;  # use ac3 audio
        -mp2) shift; ac3=0 ;;  # use mp2 audio
        -iso) shift; iso=1 ;;  # make iso image when finished
	-h) help ; exit 0 ; shift ;;
	-?) help ; exit 0 ; shift ;;
	-help) help ; exit 0 ; shift ;;
	esac
done


# verify output directory exists:
if [ -z "$outdir" ] ; then
        if [ -w "`pwd`" ] ; then
                echo "[dvd-menu] Output directory not specified."
                echo "[dvd-menu] Using `pwd`"
                outdir="`pwd`"
        else
                echo '[dvd-menu] ERROR: Output directory not specified.'
                exit 0
        fi
fi

#########################################################
## read local rc file, if it exists:
if [ -f "${HOME}/.dvd-slideshowrc" ] ; then
        myecho "[dvd-menu] Reading default variables in ${HOME}/.dvd-slideshowrc"
        . "${HOME}/.dvd-slideshowrc"  # insecure?  maybe do a loop...
fi
for it in "${xmlfile[@]}"; do
	# verify xml file exists:
	if [ ! -r "$it" ] ; then
		echo "[dvd-menu] ERROR: XML file $it does not exist."
		exit 0
	fi
done

## initialize log file:
logfile="$outdir/$logfilename"
echo "[dvd-menu] `date`" > "$logfile"
echo "[dvd-menu] dvd-menu version $version" >> "$logfile"
echo "[dvd-menu] `uname -a`" >> "$logfile"
echo "[dvd-menu] Output directory=$outdir" >> "$logfile"

tmpdir="$outdir"/"dvd-menu_temp_$$"
if [ ! -d "$tmpdir" ] ; then
	mkdir -p "$tmpdir"
fi
dvd_dir="$outdir"/"dvd_fs"
if [ -d "$dvd_dir" ] ; then	
	## output directory already exists.  Delete old files
#	echo "[dvd-menu] Output directory already exists. "
	echo "[dvd-menu] Deleting existing AUDIO_TS and VIDEO_TS directories..."
	if [ -d "$dvd_dir/AUDIO_TS" ] ; then rm -r "$dvd_dir/AUDIO_TS" ; fi
	if [ -d "$dvd_dir/VIDEO_TS" ] ; then rm -r "$dvd_dir/VIDEO_TS" ; fi
else
	mkdir -p "$dvd_dir"
fi

## add parsing of .txt input file here in the future:
##

#######################################################3
# Summarize settings:
myecho "[dvd-menu] Final dvd filesystem is in $dvd_dir"
[ "$pal" -eq 1 ] && ntsc_or_pal="PAL" || ntsc_or_pal="NTSC"
[ "$ac3" -eq 1 ] && mp2_or_ac3="AC3" || mp2_or_ac3="MP2"
myecho "[dvd-menu] Video: $ntsc_or_pal  Audio: $mp2_or_ac3"
myecho "[dvd-menu] Debug=$debug  "
if [ "$nocleanup" == 1 ] ; then
        myecho "[dvd-menu] Leaving all temporary files in temp directory"
	myecho "[dvd-menu] Temporary directory is $tmpdir"
fi
#echo "[dvd-menu] audio files=$audiofile"
#echo "[dvd-menu] menu titles:" 
#echo "[dvd-menu] ${title[@]}" 

if [ -z "${title[0]}" ] ; then  # arrays start at index 0
	# no titles passed! assume user wants no menu
	no_menu=1
	echo "[dvd-menu] WARNING: No button titles passed! Not creating menu."
fi

#############################################################

if [ "$pal" -eq 1 ] ; then
	framerate='25'
	frames_per_sec=2500  # in 1/100ths of a second
	sq_to_dvd_pixels='93.75x100%' # i.e., make the horizontal 9% smaller
        sq_pixel_multiplier=$(( 1000 * 540 / 576 )) 
		# see http://www.uwasa.fi/~f76998/video/conversion
	if [ "$low_quality" -eq 1 ] ; then
		width='352' ; height='288'
		resolution='352x288'
	elif [ "$high_quality" -eq 1 ] ; then
		width='720' ; height='576'
		resolution='720x576'
	else # 
		width='720' ; height='576'
		resolution='720x576'
	fi
else  ## NTSC
	framerate='29.97'
	frames_per_sec=2997  # in 1/100ths of a second
	sq_to_dvd_pixels='112.5x100%' # i.e., make the horizontal 10% larger
        sq_pixel_multiplier=$(( 1000 * 540 / 480 )) 
		# see http://www.uwasa.fi/~f76998/video/conversion
	if [ "$low_quality" -eq 1 ] ; then
		width='352' ; height='240'
		resolution='352x240'
	elif [ "$high_quality" -eq 1 ] ; then
		width='720' ; height='480'
		resolution='720x480'
	else # 
		width='720' ; height='480'
		resolution='720x480'
	fi
fi

hilight_height=$(( ( $height - $hilight_border -$title_location_y - $title_font_size - 2*$hilight_spacing - 2*$hilight_font_size)/2 ))
hilight_width=$(( $hilight_height * $width / $height ))  # assume this aspect ratio.
hilight_start_location_x=$(( ($width - 3*$hilight_width - 2*$hilight_spacing )/2 ))
hilight_start_location_y=$(( $title_location_y + $title_font_size + $hilight_spacing ))

if [ "$low_quality" -eq 1 ] ; then
	menu_height=$(( $menu_height / 2 ))
	menu_width=$(( $menu_width / 2 ))
	text_size=$(( 3 * $menu_height / 4 ))
	text_loc=$(( $menu_height/2 + $text_size/2 ))
	menu_location_x=$(( $menu_location_x / 2 ))
	menu_location_y=$(( $menu_location_y / 2 ))  
	title_font_size=$(( $title_font_size / 2 ))
	title_white_height=$(( $title_white_height / 2 ))
	radius=$(( $radius / 2 ))
	title_location_x=$(( $title_location_x / 2 ))
	title_location_y=$(( title_location_y / 2 ))
	
	embed_image_width=$(( $embed_image_width / 2 ))
	embed_image_height=$(( $embed_image_height / 2 ))

	hilight_border=$(( $hilight_border / 2 ))
	hilight_spacing=$(( $hilight_spacing / 2 ))
	hilight_font_size=$(( $hilight_font_size / 2 ))
fi

############################################################3
## Check for required programs

# ppmtoy4m
checkforprog ppmtoy4m
progver=`mplex 2>&1 | grep version | awk '{ print $4 }'`
logecho "[dvd-menu] Found mjpegtools version $progver"
if ppmtoy4m -S 420mpeg2 xxxxx 2>&1 | grep -q xxxxx; then
        logecho "[dvd-menu] mjpegtools is >= 1.6.3-rc1"
        subsample='420mpeg2'
else
        logecho "[dvd-menu] mjpegtools is <= 1.6.2"
        # ppmtoy4m did not accept the -S 420mpeg2 option
        # so it's probably a version older than 1.6.3
        subsample='420_mpeg2'
fi

checkforprog sox
checkforprog convert
checkforprog dvdauthor

# ffmpeg
if [ "$ac3" -eq 1 ] ; then
	it=`which ffmpeg`
	       if [ -z "$it" ] ; then
	               # no ffmpeg!  use mp2 audio instead:
	               myecho "[dvd-menu] Warning:  no ffmpeg found for AC3 audio encoding."
	               myecho "[dvd-menu]           Using MP2 audio instead."
	               myecho "[dvd-menu]           MP2 audio is less compatible with DVD player hardware."
	               ac3=0
	fi
fi

# mkisofs
checkforprog mkisofs

################################################## done finding programs

#Find the fonts
font1=`find $font_dir -name $font1 | head -n 1`
font2=`find $font_dir -name $font2 | head -n 1`

# verify fonts exist:
if [ -f "$font1" ] ; then
        font="-font $font1"
elif [ -f "$font2" ] ; then
        font="-font $font2"
else
        echo "[dvd-menu] Cannot find required fonts.  Using default ImageMagick font."
        font=""
fi
###############################

myecho "[dvd-menu] ###################################"

###########################################################
# Start creating menus:

if [ "$no_menu" -eq 0 ] ; then
	## first, do the background image, then do the alpha mask:
	if [ -f "${bgfile}" ] ; then
	        echo "[dvd-menu] using background image ${bgfile}"
		background "$bgfile"
#	        convert "${bgfile}" -resize x"$height" -bordercolor black -border "$width"x240 -gravity center -crop "$width"x"$height"'+0+0!' -depth 8 -quality 100 "$tmpdir"/slideshow_background.ppm
	        mv "$tmpdir/slideshow_background.ppm" "$tmpdir/menu.ppm"
	else
	        ## need to write out a blank image of the given size:
	        echo "[dvd-menu] creating background image "
	        convert -depth 8 -size "$width"'x'"$height" gradient:white-$bg_color -type TrueColor "$tmpdir/menu.ppm"
	fi
	
	## create overlay transparent image:
	convert -depth 8 -colors 3 +antialias -size $resolution xc:transparent "$tmpdir"/button_background.png
	cp "$tmpdir/button_background.png" "$tmpdir/menu_mask.png"
	
	menuright=$(( $menu_location_x + $menu_width))
	menubottom=$(( $height + 50 ))  ## add 50 so we can chop off the bottom later
	menuh=$(( $height + 50 ))
	if [ "$no_white" -eq 0 ] ; then
		white_button_mask "$tmpdir/button_background.png"
		composite -compose src-over -type TrueColor -depth 8 -dissolve 50 "$tmpdir/button_background.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
	fi

	if [ -n "$dvd_title" ] ; then
		title_text "$tmpdir/dvd_title.png"
		composite -compose src-over -depth 8 -type TrueColor "$tmpdir/dvd_title.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
	fi

	## so, we have a few different layouts:
	## 1.  straight text on the right side
	## 2.  6 icon buttons with text underneath (not working)
	## 3.  6 icon buttons per page, multiple pages (not working)
	## 4.  15 icon buttons per page, no text?	 (not working)
	
	if [ "${#buttonfile[@]}" -gt 1 ] ; then
		menutype=2  ## don't use this yet!!!
	else
		menutype=1
	fi
	menutype=1

	## case 1:  old layout:
	if [ "$menutype" -eq 1 ] ; then  # plain text on right side
		## create embedded image if defined:
	        if [ -f "${embedfile[0]}" ]; then  # only one embedfile possible
	                echo "[dvd-menu] using image highlight ${embedfile[0]}"
	                # Resize to 240x320 (max), keep aspect ratio.
                        embed_height=$(identify -format %h "${embedfile[0]}")
                        embed_width=$(identify -format %w "${embedfile[0]}")
                        if [ $embed_width -gt $embed_height ]; then
                                embed_resolution=$embed_image_width"x"$embed_image_height
                            else
                                embed_resolution=$embed_image_height"x"$embed_image_width
                            fi
#                        echo "[dvd-menu] running convert"
	                convert "${embedfile[0]}" -resize "$sq_to_dvd_pixels" -resize "$embed_image_width"x"$embed_image_height" "$tmpdir"/embed.png
	                # Add embed image to menu background
	                embed_offset_y=$[ ( ($height-$menu_location_y) - $(identify -format %h "$tmpdir"/embed.png) ) / 3 ]
	                embed_offset_x=$[ ( ($width-$menu_location_x) - $(identify -format %w "$tmpdir"/embed.png) ) /2 ]
	                composite -compose src-over -geometry +$embed_offset_x+$[$menu_location_y+$embed_offset_y] -type TrueColor -depth 8 -dissolve 100 "$tmpdir"/embed.png "$tmpdir"/menu.ppm "$tmpdir"/menu.ppm
	        fi
		## loop over each title:
		let i=0
		for this_title in "${title[@]}"; do
			echo "[dvd-menu] Title=${title[$i]}"
			# make each individual button:
			# to be fancy, let's add the button layers in two steps.  first the
			# button background, then the text:
			convert -size ${menu_width}x$menu_height xc:transparent -quality 100 $font -pointsize $text_size -draw "text 20,$text_loc \"${title[$i]}\"" "$tmpdir"/button_text.png
			# make each individual button mask (for when it's selected)
			convert -depth 8 -colors 3 +antialias -size ${menu_width}x$menu_height xc:transparent +antialias -pointsize $text_size $font -fill red -draw "text 20,$text_loc \"${title[$i]}\"" -quality 100 "$tmpdir"/button_mask.png
	
			## add each button as necessary, and increment the location downward
			composite -compose src-over -depth 8 -geometry +$menu_location_x+$menu_location_y "$tmpdir/button_text.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
			## add each overlay mask for the selection of buttons:
			composite -compose src-over -depth 8 -colors 3 -quality 100 -geometry +$menu_location_x+$menu_location_y "$tmpdir/button_mask.png" "$tmpdir/menu_mask.png" "$tmpdir/menu_mask.png"
		
			## now record the button location:
			right[$i]="$(( $menu_location_x + $menu_width ))"
			left[$i]="$(( $menu_location_x ))"
			top[$i]="$(( $menu_location_y ))"
			bottom[$i]="$(( $menu_location_y + $menu_height ))"
	
			vts="$(( $i + 1 ))"
#			echo "[dvd-menu] i=$i vts=$vts"
				
			let menu_location_y=$menu_location_y+$menu_height
			let i=$i+1
		done
	elif [ "$menutype" -eq 2 ] ; then   ### don't use this yet!
		## loop over each title:
		let i=0
		let embed=1
		let embed_row=1
		for this_title in "${title[@]}"; do
			echo "[dvd-menu] Title=${title[$i]}"
			# make each individual button:
			# to be fancy, let's add the button layers in two steps.  first the
			# button background, then the text:
			convert -size ${menu_width}x$menu_height xc:transparent -quality 100 $font \
			-pointsize $text_size -draw "text 20,$text_loc \"${title[$i]}\"" "$tmpdir/button_text.png"
		        ## create embedded image hilight if required:
			# hilight_border=50
			# hilight_spacing=20
			# hilight_font_size=12
			# hilight_height=$(( ( $height - 1*$border - $title_location_y - $title_font_size - 2*$hilight_spacing - 2*$hilight_font_size)/2 ))
			# hilight_width=$(( $hilight_height * $width / $height ))  # assume this aspect ratio.
			# hilight_start_location_x=$(( ($width - 3*$hilight_width - 2*$hilight_spacing )/2 ))
			# hilight_start_location_y=$(( $title_location_y + $title_font_size + $hilight_spacing ))
			## size constraint seems to be vertically when we have a title.
			## so, eventually, we could have different situations, depending on whether or not
			## a title exists, etc...  let's assume it does for now!
#			echo "hilight_border=$hilight_border"
#			echo "hilight_spacing=$hilight_spacing"
#			echo "hilight_font_size=$hilight_font_size"
#			echo "hilight_height=$hilight_height"
#			echo "hilight_width=$hilight_width"
#			echo "hilight_start_location_x=$hilight_start_location_x"
#			echo "hilight_start_location_y=$hilight_start_location_y"
		        if [ -f "${buttonfile[$i]}" ]; then
		                echo "[dvd-menu] using image ${buttonfile[$i]} as button $embed ${title[$i]}"
				echo "[dvd-menu] hilight_width=$hilight_width hilight_height=$hilight_height"
		                convert "${buttonfile[$i]}" -resize "$hilight_width"x"$hilight_height" -quality 100 "$tmpdir/embed.jpg"
				# make each individual button mask (for when it's selected)
				convert -depth 8 -colors 3 +antialias -size "$hilight_width"x"$hilight_height" xc:transparent -fill transparent -stroke red -strokewidth 2 -draw "rectangle 0,0 $hilight_width,$hilight_height" -quality 100 "$tmpdir/button_mask.png"
	
				## calculate location of this image:
				[ $embed -ge 4 ] && embed_row=2 || embed_row=1
				[ $embed -ge 4 ] && embed_col=$(( $embed - 3 )) || embed_col=$embed
				hilight_x=$(( $hilight_start_location_x + ($embed_col - 1)*($hilight_spacing + $hilight_width) ))
				hilight_y=$(( $hilight_start_location_y + ($embed_row - 1)*($hilight_spacing + $hilight_height) ))
				button_text_location_x=$(( $hilight_x ))
				button_text_location_y=$(( $hilight_y + $hilight_height ))
				echo "[dvd-menu] r,c=$embed_row,$embed_col hilight_x_loc=$hilight_x hilight_y_loc=$hilight_y"
				
				## add each button as necessary, and increment the location 
				composite -compose src-over -type TrueColor -depth 8 -geometry +$button_text_location_x+$button_text_location_y "$tmpdir/button_text.png" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"

		                # Add embed image to menu background
				composite -compose src-over -type TrueColor -depth 8 -geometry +$hilight_x+$hilight_y "$tmpdir/embed.jpg" "$tmpdir/menu.ppm" "$tmpdir/menu.ppm"
				## add each overlay mask for the selection of buttons:
				composite -compose src-over -depth 8 -colors 3 -quality 100 -geometry +$hilight_x+$hilight_y "$tmpdir/button_mask.png" "$tmpdir/menu_mask.png" "$tmpdir/menu_mask.png"
	
				## now record the button location:
				right[$i]="$(( $hilight_x + $hilight_width ))"
				left[$i]="$(( $hilight_x ))"
				top[$i]="$(( $hilight_y ))"
				bottom[$i]="$(( $hilight_y + $hilight_height ))"
				let embed=$embed+1
		        fi
			##################################################################33
			
			vts="$(( $i + 1 ))"
			echo "[dvd-menu] i=$i vts=$vts"
				
			let i=$i+1
		done
	fi	# end of different menu types

	## now convert the menu background to a mpg
	if [ "$pal" -eq 1 ] ; then
		ppmtoy4m -v 0 -n "$frames" -r -S "$subsample" -F 25:1 -A 59:54 -I p "$tmpdir/menu.ppm" | \
		mpeg2enc $mpeg2enc_params -o "$tmpdir/menu.mpg" 
	else
		ppmtoy4m -v 0 -n "$frames" -r -S "$subsample" -F 30000:1001 -A 10:11 -I p "$tmpdir/menu.ppm" | \
		mpeg2enc $mpeg2enc_params -o "$tmpdir/menu.mpg" 
	fi
	
	# optionally fade out at a certain time?
	if [ -n "$audiofile" ] ; then
		## get type of file:  mp3, ogg, or wav
		suffix=`echo "$audiofile" | awk -F. '{print $NF}'`
		echo "[dvd-menu] suffix=$suffix"
		if [ "$suffix" == "mp3" ] ; then
			checkforprog lame
		        lame --decode "$audiofile" "$tmpdir/audio_tmp.wav" 
			# convert it to 48khz:
			sox -v 0.90 "$tmpdir/audio_tmp.wav" -r 48000 "$tmpdir/audio.wav" 
			rm "$tmpdir/audio_tmp.wav"
		elif [ "$suffix" == "ogg" ] ; then
			checkforprog oggdec
		        oggdec -o "$tmpdir/audio_tmp.wav" "$audiofile" 
			# convert it to 48khz:
			sox -v 0.90 "$tmpdir/audio_tmp.wav" -r 48000 "$tmpdir/audio.wav" 
			rm "$tmpdir/audio_tmp.wav"
		elif [ "$suffix" == "wav" ] ; then
	#	        cp "$audiofile" "$tmpdir/audio_tmp.wav"
			# convert it to 48khz:
			sox -v 0.90 "$audiofile" -r 48000 "$tmpdir/audio.wav" 
		else
		        echo "[dvd-menu] ERROR:  Unknown audio file format.  Must be .mp3, .ogg, or .wav"
		fi
	else
		## create silence:  
		echo "[dvd-menu] creating silence .wav file"
		## create audio for 1 second:
		sox -t raw -s -w -c 2 -r 48000 /dev/zero -t wav - trim 0 0:01.000 > "$tmpdir/audio.wav" 
	fi
	

	if [ "$ac3" -eq 1 ] ; then
	        checkforprog ffmpeg
	        echo "[dvd-menu] Creating ac3 audio..."
	        check_rm "$tmpdir/audio.ac3"
	        ffmpeg -i "$tmpdir/audio.wav" -vn -y -ab 192 -acodec ac3 -ar 48000 -ac 6 "$tmpdir/audio.ac3" >> "$logfile" 2>&1
	else
		## toolame is way faster! (3x in my test)
		it=`which toolame`
		if [ -n "$it" ] ; then
		        toolame_version=`toolame -h | head -n 4 | grep version | awk '{ print $3 }'`
		        echo "[dvd-menu] using toolame $toolame_version..."
		        if [ "$toolame_version" == '0.2m' ] ; then
		        	toolame -s 48000 -b 128 "$tmpdir/audio.wav" "$tmpdir/audio.mp2" 
		        else
		        	toolame -s 48 -b 128 "$tmpdir/audio.wav" "$tmpdir/audio.mp2" 
		        fi
		else
		        myecho "[dvd-menu] using mp2enc"
		        mp2enc -v 0 -b 128 -r 48000 -s -o "$tmpdir/audio.mp2" < "$tmpdir/audio.wav" 
		fi
	fi
	

	## now multiplex the audio and video:
	myecho "[dvd-menu] mplexing the files............."
	if [ "$ac3" -eq 1 ] ; then
		mplex -v 0 -f 8 -o "$tmpdir/menu_t.vob" "$tmpdir/menu.mpg" "$tmpdir"/audio.ac3 
	else
		mplex -v 0 -f 8 -o "$tmpdir/menu_t.vob" "$tmpdir/menu.mpg" "$tmpdir"/audio.mp2 
	fi

	## start of button subpicture XML file creation for spumux:
	## now define the buttons:
	echo '<subpictures>' > "$tmpdir/menu.spumux"
	echo '	<stream>' >> "$tmpdir/menu.spumux"
	echo -n '		<spu start="00:00:00.00" end="00:00:00.00" highlight="' >> "$tmpdir/menu.spumux"
	echo "$tmpdir/menu_mask.png"'" force="yes" >' >> "$tmpdir/menu.spumux"
	## now loop over the buttons:
	let i=0
	for it in "${left[@]}"; do
		echo -n '                       <button ' >> "$tmpdir/menu.spumux"
		# insert an up link to the upAction for the first button
		if [ $i -eq 0 ] && [ "$upAction" -eq 1 ]; then
			echo -n ' up="upAction" ' >> "$tmpdir/menu.spumux"
		fi
		echo 'x0="'${left[$i]}'" y0="'${top[$i]}'" x1="'${right[$i]}'" y1="'${bottom[$i]}'" />' >> "$tmpdir/menu.spumux"

		let i=$i+1
	done
        # append an up action to the button list
        if [ "${upAction}" -eq 1 ]; then
                echo '                  <action name="upAction" />' >> "$tmpdir/menu.spumux"
        fi
	echo '		</spu>' >> "$tmpdir/menu.spumux"
	echo '	</stream>' >> "$tmpdir/menu.spumux"
	echo '</subpictures>' >> "$tmpdir/menu.spumux"

	myecho "[dvd-menu] ###############################################"
	myecho "[dvd-menu] spumuxing the files............."
	## using spumux now:
#	spumux -v 0 -P "$tmpdir/menu.spumux" < "$tmpdir/menu_t.vob" > "$outdir/menu.vob" >> "$logfile" 2>&1
	spumux -v 0 -P "$tmpdir/menu.spumux" < "$tmpdir/menu_t.vob" > "$outdir/menu.vob" 2>> "$logfile"
	if [ $? -ne 0 ] ; then
		## spumux errored
		myecho "[dvd-menu] ERROR during spumux execution!"
		myecho "[dvd-menu] see $logfile for details"
		exit 1
	fi

fi   # end if we're actually making a menu.  otherwise, just to the stuff below

## now build the dvdauthor xml file:
## do we need "jumppad"?
echo '<dvdauthor dest="'$dvd_dir'" jumppad="0">' > "$outdir/vmgm.xml"
echo '        <vmgm>' >> "$outdir/vmgm.xml"
echo '           <menus>' >> "$outdir/vmgm.xml"

if [ "$no_menu" -eq 0 ] ; then
	echo '               <pgc entry="title" >' >> "$outdir/vmgm.xml"
	echo '                    <vob file="'$outdir/menu.vob'" pause="inf"/>' >> "$outdir/vmgm.xml"
	## now loop over each title:
	let i=1   ## titles start from 1, not zero.
	let m=1   ## menus start at 1
#	myecho "[dvd-menu] xmlfiles=${xmlfile[@]}"
	for it in "${xmlfile[@]}"; do
		# verify xml file exists:
		if [ ! -r "$it" ] ; then
			echo "[dvd-menu] ERROR: XML file $it does not exist."
			exit 0
		fi

		file_base=`basename "$it" .xml`
		if [ -f "$file_base"_submenu.xml ] ; then
			echo '             		<button> jump menu '$m'; </button>' >> "$outdir/vmgm.xml"
			let m=$m+1
		else
			echo '             		<button> jump title '$i'; </button>' >> "$outdir/vmgm.xml"
		fi
		let i=$i+1
		let m=$m+1
	done
	echo '                    <post> jump vmgm menu 1; </post>' >> "$outdir/vmgm.xml"
	echo '                </pgc>' >> "$outdir/vmgm.xml"
fi

echo '           </menus>' >> "$outdir/vmgm.xml"
echo '        </vmgm>' >> "$outdir/vmgm.xml"
echo '        <titleset>' >> "$outdir/vmgm.xml"

## now cat the other ones into this file...
let i=0   
let title=1   
let m=1   

## get rid of existing files:
check_rm "$tmpdir/menus_vmgm.xml"

## every dvd has at least one title!
echo '	<titles>' > "$tmpdir/titles_vmgm.xml"

total_files="${#xmlfile[*]}"
for it in `seq 1 1 $total_files` ; do
	if [ "$(( $i + 1 ))" -eq "$total_files" ] ; then continuous=0 ; fi
	let j=$i+2
	myecho "[dvd-menu] file=${xmlfile[$i]}"
	suffix=`echo ${xmlfile[$i]} | awk -F. '{print $NF}'`
	file_base=`basename "${xmlfile[$i]}" .xml`
	nextmenu=$(( $m + 1 ))
	lastmenu=$(( $m - 1 ))
#	myecho "[dvd-menu] file=${xmlfile[$i]} file_base=$file_base suffix=$suffix"
	if [ "$suffix" == 'xml' ] ; then
		if [ -f "$file_base"_submenu.xml ] ; then
			if [ $m -eq 1 ] ; then
				## first menu!
				echo '	<menus>' > "$tmpdir/menus_vmgm.xml"
                                echo '               <pgc entry="root" >' >> "$tmpdir/menus_vmgm.xml"
			else
                                echo '               <pgc>' >> "$tmpdir/menus_vmgm.xml"
			fi
			## read in submenu for this title:
			cat "$file_base"_submenu.xml | sed -e "s/thetitle/title $title/g" \
			| sed -e "s/nextmenu/$nextmenu/" >> "$tmpdir/menus_vmgm.xml"
			echo '		<post> jump menu '$m'; </post>' >> "$tmpdir/menus_vmgm.xml"
			echo '		</pgc>' >> "$tmpdir/menus_vmgm.xml"
			let m=$m+1
			nextmenu=$(( $m + 1 ))
			lastmenu=$(( $m - 1 ))
		fi
		# parse in either the chapter select menus or the
		#  browsable slideshow for this title:
		let n=1
		if [ -f "$file_base"_browse.xml ] ; then
			## read in browsable slideshow
				## read in chapter menu:
				cat "$file_base"_browse.xml >> "$tmpdir/menus_vmgm.xml"
				let m=$m+1
				nextmenu=$(( $m + 1 ))
				lastmenu=$(( $m - 1 ))
		elif [ -f "$file_base"_chap_$n.xml ] ; then
			## read in chapter select menu
			while [ -f "$file_base"_chap_$n.xml ] ; do
				if [ $m -eq 1 ] ; then
					## first menu!
					echo '	<menus>' > "$tmpdir/menus_vmgm.xml"
	                                echo '               <pgc entry="root" >' >> "$tmpdir/menus_vmgm.xml"
				else
	                                echo '               <pgc>' >> "$tmpdir/menus_vmgm.xml"
				fi
				## read in chapter menu:
				cat "$file_base"_chap_$n.xml | sed -e "s/title 1/title $title/g" \
				| sed -e "s/nextmenu/$nextmenu/" | sed -e "s/lastmenu/$lastmenu/" >> "$tmpdir/menus_vmgm.xml"
				echo '		<post> jump menu '$m'; </post>' >> "$tmpdir/menus_vmgm.xml"
				echo '		</pgc>' >> "$tmpdir/menus_vmgm.xml"
				let m=$m+1
				let n=$n+1
				nextmenu=$(( $m + 1 ))
				lastmenu=$(( $m - 1 ))
			done
		fi
		## xml file exists:  use it!
#		echo '	<titles>' >> "$tmpdir/titles_vmgm.xml"
#		echo '         <subpicture lang="EN" />' >> "$tmpdir/titles_vmgm.xml"
		echo '		<pgc>' >> "$tmpdir/titles_vmgm.xml"
		cat "${xmlfile[$i]}" >> "$tmpdir/titles_vmgm.xml"
		if [ "$continuous" -eq 1 ] ; then
			echo "		<post> jump title $j; </post>" >> "$tmpdir/titles_vmgm.xml"
		elif [ "$no_menu" -eq 0 ] ; then
			echo '		<post> call vmgm menu 1; </post>' >> "$tmpdir/titles_vmgm.xml"
		fi
		echo '		</pgc>' >> "$tmpdir/titles_vmgm.xml"
#		echo '	</titles>' >> "$tmpdir/titles_vmgm.xml"
	elif [ "$suffix" == 'mpg' ] || [ "$suffix" == 'vob' ]; then
		## assume user already made it vob compatible	
		## write the title xml:
#		echo '	<titles>' >> "$tmpdir/titles_vmgm.xml"
		echo '		<pgc>' >> "$tmpdir/titles_vmgm.xml"
		echo '		<vob file="'${xmlfile[$i]}'"  />' >> "$tmpdir/titles_vmgm.xml"
		if [ "$continuous" -eq 1 ] ; then
			echo "		<post> jump title $j; </post>" >> "$tmpdir/titles_vmgm.xml"
		elif [ "$no_menu" -eq 0 ] ; then
			echo '		<post> call vmgm menu 1; </post>' >> "$tmpdir/titles_vmgm.xml"
		fi
		echo '		</pgc>' >> "$tmpdir/titles_vmgm.xml"
#		echo '	</titles>' >> "$tmpdir/titles_vmgm.xml"
	else
		echo '[dvd-menu] error... filetype not supported!'
		exit
	fi
	let i=$i+1
	let title=$title+1
done

## finish up menus tag:
if [ $m -gt 1 ] ; then  # only if we had submenus:
	echo '	</menus>' >> "$tmpdir/menus_vmgm.xml"
fi

## finish up titles tag:
echo '	</titles>' >> "$tmpdir/titles_vmgm.xml"

## join the menus and titles with the vmgm menu:
if [ -f "$tmpdir/menus_vmgm.xml" ] ; then # only if submenus exist!
	cat "$tmpdir/menus_vmgm.xml" >> "$outdir/vmgm.xml"
fi
cat "$tmpdir/titles_vmgm.xml" >> "$outdir/vmgm.xml"

echo '</titleset>' >> "$outdir/vmgm.xml"
echo '</dvdauthor>' >> "$outdir/vmgm.xml"

myecho "[dvd-menu] ##############################"

## run dvdauthor?
if [ "$no_dvdauthor" -eq 0 ] ; then
	myecho "[dvd-menu] Running dvdauthor..."
#	dvdauthor -o "$dvd_dir" -x "$outdir/vmgm.xml" >> "$logfile" 2>&1
	dvdauthor -o "$dvd_dir" -x "$outdir/vmgm.xml" 2>> "$logfile"
	if [ $? -ne 0 ] ; then
		## dvdauthor errored
		myecho "[dvd-menu] ERROR during dvdauthor execution!"
		myecho "[dvd-menu] see $logfile for details"
		exit 1
	else
		myecho "[dvd-menu] dvdauthor ran ok. See logfile for details."
	fi
	if [ "$iso" -eq 1 ] ; then
		## create iso image afterward:
		myecho "[dvd-menu] Creating iso image dvd.iso"
		mkisofs -dvd-video -udf -o dvd.iso "$dvd_dir" 
		if [ $? -ne 0 ] ; then
			## mkisofs errored
			myecho "[dvd-menu] ERROR during mkisofs execution!"
			myecho "[dvd-menu] see $logfile for details"
			exit 1
		fi
	fi
else
	myecho "[dvd-menu] Not running dvdauthor.  Edit the vmgm.xml file"
	myecho "[dvd-menu] if you wish, and then run dvdauthor: "
	myecho "[dvd-menu] dvdauthor -x vmgm.xml"
	echo " "
fi

if [ "$nocleanup" -eq 0 ] ; then
	cleanup
fi
# cleanup the logfile a little bit:
sed -e '/ bytes of data written[[:cntrl:]]INFO/d' "$logfile" > "$outdir"/tmp.txt
mv "$outdir"/tmp.txt "$logfile"

echo "[dvd-menu] Output directory is $outdir"
echo "[dvd-menu] Logfile output is at $logfile"
echo "[dvd-menu] The dvd filesystem is $dvd_dir"
echo "[dvd-menu] "
echo "[dvd-menu] You can play this video using one of the following commands:"
echo "[dvd-menu] xine -g -u 0 dvd:\"$dvd_dir\""
echo "[dvd-menu] "
echo "[dvd-menu] or, if you just want to watch the videos without the menus:"
echo "[dvd-menu] mplayer -sid 0 dvd:// -dvd-device $dvd_dir"
myecho "[dvd-menu] Done!"
echo
exit 0
