#!/bin/bash
#
# dvdwizard - create a DVD image from mpeg stream(s)
#             with chapters and menus without further user interaction
#
# Version   0.41 written by Wolfgang Wershofen (mailto: itconsult at wershofen.de)
#           0.42 / Stefan Becker,
#                   - pass Subpicture Information to dvdauthor (-s)
#                   - pass Palette File to dvdauthor (-P)
#                   - Widescreen-Options (-WS)
#                   - Widescreen Options for every Title Set (-ws)
#		 / Wolfgang Wershofen
#                   - Zuordnung Sprache/Audiotracks geaendert
#		    - Neue Option --audio-seq
#
# needs the following tools to run:
# - dvdauthor >= 0.6.0
#

# We need some sub-routines from dvdwizardrc
# This file must reside in the same directory as the called script
# Should maybe go into a directory like /usr/local/share/dvdwizard
# but this would require an installation procedure - maybe in some
# later version.
#
rcfile="/usr/share/dvdwizard/dvdwizardrc"
#echo $rcfile
if [ -e "$rcfile" -a -r "$rcfile" ]; then
	. "$rcfile"
else
	echo "dvdwizardrc not found or is not readable. Aborting" >&2
    exit 1
fi

# ------------------------------
# Function specification
#
usage()
{
 cat <<EOF
Usage: `basename $0` [options] mpeg-file(s)

miscellaneous options:
----------------------
-h | --help   	print this lot out
-v | --version	print version and exit

general processing:
-------------------
-C | --config-file	filename of dvdwizard-configuration file
			[~/.dvdwizard/dvdwizard.conf]
-o | --output		Path where the final DVD will be stored [\$BASEDIR/dvd]
-x | --xml		Print dvdauthor XML-specs into this file
			[\$BASEDIR/dvdwizard.xml]
-l | --logfile		Write Log-Messages to this file [\$BASEDIR/dvdwizard.log]
--restart		Do not start from beginning but from step x
			x may be dvdcpics, mk_vmgm, mk_vtsm, author
--xmlonly		Do not author final DVD, only create menus and xml

Note: \$BASEDIR defaults to the current directory if not stated otherwise in the
config file (either default ~/.dvdwizard/dvdwizard.conf or specified with the -C
option)

DVD-specific options:
---------------------
-T | --vmgm	DVD-Title <string> Titlestring for the whole DVD [-t]
                if not specified, Titlestring from the first Title on the
                DVD will be used
-B | --vmgmbg	Background-Picture for VMGM-Menu
-N | --tvnorm	TV-Norm to use <PAL|NTSC> [PAL]
-V | --tvsize	Visible Area on TV-Screen <XxY> [635x535]
-WS             Widescreen-Option (nopanscan or noletterbox) for whole DVD
		may be overwritten by -ws for a specific titleset
-P | --palette  Name of palette File, needed i. E. for subpictures
-A | --Audio	language for audiotracks. <lang1,lang2,...,langn> [de,en]
		for each different audio-track format (mp2,ac3), assignment of
		language starts from beginning of list. first mp2 and ac3 track
		found in stream will be assigned to "lang1", second track to
		"lang2" etc.
		applies to all titlesets on the DVD, may be overwritten by -a
		for a specific titleset
--audio-seq	Assign languages in sequential mode, regardless of audio formats
--notitle	Suppressed printing of DVD-Title on VMGM-Menu

Title-specific options:
-----------------------
-t | --vts	Create a new titleset on the DVD with the subsequent options and
		mpeg-files.
		-t must be followed by a filename or a string or the word "auto"
		"auto" determines title-string from filename of first mpeg-file,
		if -t is followed by an existing filename, that file is considered
		to be the first mpeg in this titleset, else the string is used as
		movie title for this titleset
-c | --chapters	Chapter-Specification <0|file|timecodes|interval> [300]
		0 means no chaptering except for different mpeg-files
-b | --vtsmbg	Background-Picture for VTSM-Menu
-ws             Widescreen-Option (nopanscan or noletterbox) for this titleset
-a | --audio	see -A, but only applies to this titleset
-s | --subpic   pass subpicture information to dvdauthor.
                This is a string like "en" for english, "de" for German etc.
                More Languages by Comma Separation, i.e. "-s de,en".

Note: Every new "-t" creates a new titleset on the DVD. Subsequent options -c,
-b and -a and mpeg-file(s) apply only to that titleset. To create a DVD with 2
titlesets, use for example:
	dvdwizard -t auto -c 300 title1.mpg -t auto -b background.ppm title2.mpg

EOF
exit 1
}

# ------------------------------
# Check, if all Parms are valid
#
checkParms()
{
mk_check_dir "$BASEDIR" "NEW"
mk_check_dir "$DESTDIR" "NEW"
mk_check_file "$XMLFILE" "NEW"
mk_check_file "$LOGFILE" "NEW"
mk_check_file "$CMDFILE" "NEW"

#
# Check input files
#
[ ! -z "$VMGMBG" ] && mk_check_file "$VMGMBG" "OLD"
[ ! -z "$PALETTE" ] && mk_check_file "$PALETTE" "OLD"
for i in $(seq 1 $vts); do
	eval vtsbg=\$VTSMBG_$i
	[ ! -z "$vtsbg" ] && mk_check_file "$vtsbg" "OLD"
	eval fcount=\$VTSM_"$i"_FILES
    if [ $fcount -eq 0 ]; then
    	echo "No input files specified for titleset $i. Aborting" >&2
        usage
    fi
    for j in $(seq 1 $fcount); do
    	eval mk_check_file \"\$VTSM_"$i"_FILE_$j\" "OLD"
    done
	eval probeFile=\"\$VTSM_${i}_FILE_1\"
	audiotracks=$(mpgprobe "$probeFile" | grep Audio: | awk -F: '{print $2}') || error_out
    acount=$(echo "$audiotracks" | wc -w)
 	vattribs=$(mpgprobe "$probeFile" | grep Video:) || error_out
    vres=$(echo "$vattribs" | cut -d' ' -f1 | cut -d':' -f2)
    vratio=$(echo "$vattribs" | cut -d' ' -f2)
    eval VTSM_"$i"_ATRACKS=\"\$audiotracks\"
    eval VTSM_"$i"_ACOUNT=\"\$acount\"
    eval VTSM_"$i"_VRES=\$vres
    eval VTSM_"$i"_VRATIO=\$vratio
done

#
# Now that almost everything seems ok, open the Log
#
echo "Creating DVD with $dvdwizard_ver"
cat <<EOF >> "$LOGFILE"
Logfile from $dvdwizard_ver run at `date +%d.%m.%Y\ -\ %H:%M:%S`
dvdwizard invoked with  `basename $0` "$allargs"
Default processing options read from $confFile
-----------------------------------------------------------------
EOF

#
# Let's handle the title(s) of the DVD
#
for i in $(seq 1 $vts); do
	eval tstring=\"\$VTSTITLE_$i\"
    if [ "$(echo "$tstring" | tr a-z A-Z)" == "AUTO" ]; then
    	eval vtstitle=\"\$VTSM_"$i"_FILE_1\"
    	tstring=`dvdtguess "$vtstitle"`
    fi
    echo "Title #$i: $tstring"
    eval VTSTITLE_$i=\"\$tstring\"
done
if [ -z "$VMGMTITLE" ]; then
	VMGMTITLE=$VTSTITLE_1
    echo DVD-Title: "$VMGMTITLE"
fi

return 0
}

# ------------------------------
# Create Chapter-Pics
#
call_dvdcpics()
{
echo -n Capturing Chapter-Screenshots from temporary DVD...
cat <<EOF >> "$LOGFILE"
+-------------------------------------+
| Calling dvdcpics                    |
| Capture Screenshots from Chapters   |
+-------------------------------------+
EOF
for i in $(seq 1 $vts); do
	echo "Capturing Chapters from Titleset $i" >> "$LOGFILE"
    cmd="dvdcpics -b -i \"$DESTDIR\" -t $i -o \"$BASEDIR/cpics/VTS$i\" -f \$VTSM_"$i"_VRES"
    [ -e "$confFile" ] && cmd="${cmd} -C \"$confFile\""
    eval echo "$cmd" >> "$CMDFILE"
    eval $cmd >> "$LOGFILE" 2>&1 || error_out
    eval cshots=( $(ls -QS "$BASEDIR/cpics/VTS$i") )
    eval VTSM_PIC_$i=\"\$BASEDIR/cpics/VTS$i/\${cshots[0]}\"
done
echo "...done"

return 0
}

# ------------------------------
# Create DVD-Main-Menu
#
call_mk_vmgm()
{
echo -n "Building VMGM-Menu..."
cat <<EOF >> "$LOGFILE"
+-------------------------------------+
| Calling mk_vmgm                     |
| Creating VMGM-Menu                  |
+-------------------------------------+
EOF
#
# If restarting, delete xml beyond the restart mark
#
restartmark="call_mk_vmgm"
sed -i '/^<!-- '$restartmark' \/\/-->/,/\n$/d' $XMLFILE
#grep $restartmark "$XMLFILE" > /dev/null && cat $XMLFILE | sed -e '/^<!-- '$restartmark' \/\/-->/,/\n$/d' > $XMLFILE
echo "<!-- $restartmark //-->" >> $XMLFILE

#
# Assemble the needed command
#
cmd="mk_vmgm -T \"$VMGMTITLE\" -x \"$XMLFILE\" -N $TVNORM -V $TVSIZE"
[ -e "$confFile" ] && cmd="${cmd} -C \"$confFile\""
[ -e "$VMGMBG" ] && cmd="${cmd} -B \"$VMGMBG\""
[ "$NOTITLE" -eq 1 ] && cmd="${cmd} --notitle"
for i in $(seq 1 $vts); do
	eval title=\"\$VTSTITLE_$i\"
	cmd="${cmd} -t \"$title\""
    eval bgpic=\"\$VTSMBG_$i\"
	[ -e "$bgpic" ] && pic="$bgpic" || eval pic=\"\$VTSM_PIC_$i\"
    cmd="${cmd} -b \"$pic\""
    eval acount=\$VTSM_"$i"_ACOUNT
    [ "$acount" -gt 1 ] && cmd="${cmd} --hasaudio"
done
echo "$cmd" >> "$CMDFILE"
eval $cmd >> "$LOGFILE" 2>&1 || error_out
echo "...done"

return 0
}

# ------------------------------
# Create the Titlesets
#
call_mk_vtsm()
{
#
# If restarting, delete xml beyond the restart mark
#
restartmark="call_mk_vtsm"
sed -i '/^<!-- '$restartmark' \/\/-->/,/\n$/d' $XMLFILE
#grep $restartmark "$XMLFILE" > /dev/null && cat $XMLFILE | sed -e '/^<!-- '$restartmark' \/\/-->/,/\n$/d' > $XMLFILE
echo "<!-- $restartmark //-->" >> $XMLFILE

for title in $(seq 1 $vts); do
    echo "Creating Titleset #$title..."
    echo -e "\t<titleset>" >> "$XMLFILE"

#
# Get audio characteristics of the title
#
    echo -e "\tAssigning Languages to Audio-Tracks..." | tee -a $LOGFILE
    [ -z "$AUDIOSEQUENTIAL" ] && AUDIOSEQUENTIAL=0
    eval vtsaudio=\"\$VTSMAUDIO_${title}\"
    [ -z "$vtsaudio" ] && vtsaudio="$DVDAUDIO"
    eval audiotracks=\"\$VTSM_${title}_ATRACKS\"
    acount=$(echo "$audiotracks" | wc -w)
    audioparm=""
    langlist=$(echo "$vtsaudio" | tr ',' ' ')
    lcount=$(echo "$langlist" | wc -w)
    langptr_all=1
    disind=1
    alangs=""
    for i in $audiotracks; do
	if [ "$AUDIOSEQUENTIAL" -eq 0 ]; then
	    eval this_langptr=\"\$langptr_${i}\"
	    if [ -z "$this_langptr" ]; then
		this_langptr=1
	    fi
	else
	    this_langptr="$langptr_all"
	fi
	alang=$(echo "$langlist" | cut -d' ' -f"$this_langptr" | tr [a-z] [A-Z])
	alangs="$alangs $alang"
	audioparm="${audioparm}${i}+${alang},"
	eval "language=\$"$alang
	[ -z "$language" ] && language="unknown"
	echo -e "\t$disind. audio-track ($i) will be assigned to language ${alang} ($language)" | tee -a $LOGFILE
	let disind+=1
	[ "$this_langptr" -lt "$lcount" ] && let this_langptr+=1
	[ "$AUDIOSEQUENTIAL" -eq 0 ] && eval langptr_$i=\"\$this_langptr\" || langptr_all="$this_langptr"
    done
    audioparm="${audioparm%,}"

#
# Create the vtsm-menu(s)
#
	echo -en "\tBuilding VTSM-Menu(s)..."
	cat <<EOF >> "$LOGFILE"
+-------------------------------------+
| Calling mk_vtsm                     |
| Creating VTSM-Menu(s)               |
+-------------------------------------+
EOF
#
# First create the Chapter Selection Menu
#
	echo -n "Chapters..."
	parm="-t \"$title\" -o \"$BASEDIR/vtsm\" -x \"$XMLFILE\" -N $TVNORM -V $TVSIZE"
	[ -e "$confFile" ] && parm="${parm} -C \"$confFile\""
    eval bgpic=\"\$VTSMBG_$title\"
	[ -e "$bgpic" ] && parm="${parm} -b \"$bgpic\""
    vtsparms="$parm"
    cmd="mk_vtsm ${parm}"
    [ "$acount" -gt 1 ] && cmd="${cmd} --hasaudio"
    cmd="${cmd} -r \$VTSM_"$title"_VRATIO \"$BASEDIR/cpics/VTS$title/\"chapter*.jpg"
    eval echo "$cmd" >> "$CMDFILE"
	eval $cmd >> "$LOGFILE" 2>&1 || error_out
#
# If VTS has more than one Audio-Track, create Audio Track Selection Menu
#
	if [ "$acount" -gt 1 ]; then
    	echo -n "Audio..."
		cmd="mk_vtsm_audio ${vtsparms} -a $audioparm"
    	eval echo "$cmd" >> "$CMDFILE"
    	eval $cmd >> "$LOGFILE" 2>&1 || error_out
	fi
    echo -e "\t\t</menus>" >> "$XMLFILE"
    echo "...done"

#
# Now, create the title
#
	echo -en "\tWriting titleset XML..."
	echo -e "\t\t<titles>" >> "$XMLFILE"
	# Video informations
	eval vres=\"\$VTSM_"$title"_VRES\"
	eval vratio=\"\$VTSM_"$title"_VRATIO\"
	[ ! -z $DVD_WS ] && wscreen="widescreen=\"$DVD_WS\"" || wscreen=""
	eval vts_ws=\"\$VTSMWS_"$title"\"
	[ ! -z $vts_ws ] && wscreen="widescreen=\"$vts_ws\""
	echo -e "\t\t\t<video format=\"$TVNORM\" aspect=\"$vratio\" resolution=\"$vres\" $wscreen />" >> "$XMLFILE"
	# Audio informations
	for i in $alangs; do
		echo -e "\t\t\t<audio lang=\"$i\" />" >> "$XMLFILE"
	done
	# Subtitle informations
	eval subtracks=\"\$VTSMSUBS_"$title"\"
	if [ ! -z "$subtracks" ]; then
		for i in $(echo "$subtracks" | tr ',' ' '); do
			echo -e "\t\t\t<subpicture lang=\"$i\" />" >> "$XMLFILE"
		done
	fi
	[ ! -z $PALETTE ] && paltag="palette=\"$PALETTE\"" || paltag=""
    echo -e "\t\t\t<pgc $paltag pause=\"0\">" >> "$XMLFILE"
    echo -e "\t\t\t\t<pre> g1=9; </pre>" >> "$XMLFILE"

#
# Verify and convert chapters (if neccessary)
#
	eval chapterspec=\"\$CHAPTERSPEC_$title\"
	chapters=$(chaptercheck "$chapterspec" 2>/dev/null) || error_out

#
# Loop through MPEG-Files
#
	eval mpegfiles=\$VTSM_${title}_FILES
	for i in $(seq 1 $mpegfiles); do
    	eval mf=\"\$VTSM_${title}_FILE_$i\"
        echo -e "\t\t\t\t<vob file=\"$mf\" chapters=\"$chapters\" pause=\"2\" />" >> "$XMLFILE"
 	done
    echo -e "\t\t\t\t<post> call vmgm menu entry title; </post>" >> "$XMLFILE"
    echo -e "\t\t\t</pgc>"  >> "$XMLFILE"
    echo -e "\t\t</titles>"  >> "$XMLFILE"
    echo -e "\t</titleset>"  >> "$XMLFILE"
    echo -e "\t...done"
    echo "Finished Titleset #$title"
done

return 0
}

# ------------------------------
# Final Authoring of the DVD
#
call_author()
{
#
# If restarting, delete xml beyond the restart mark
#
restartmark="call_author"
sed -i '/^<!-- '$restartmark' \/\/-->/,/\n$/d' $XMLFILE
#grep $restartmark "$XMLFILE" > /dev/null && cat $XMLFILE | sed -e '/^<!-- '$restartmark' \/\/-->/,/\n$/d' > $XMLFILE
echo "<!-- $restartmark //-->" >> $XMLFILE

#
# Print xml trailer records
#
echo -n "Finishing XML creation..."
echo "</dvdauthor>" >> "$XMLFILE"
echo "<!-- xml-file creation ended at `date +%d.%m.%Y\ -\ %H:%M:%S` //-->" >> "$XMLFILE"
echo "...done"

#
# Now author the temporary DVD
#
if [ "$XMLONLY" -eq 0 ]; then
	echo "Authoring DVD into $DESTDIR..."
	cat <<EOF >> "$LOGFILE"
+-------------------------------------+
| Calling dvdauthor                   |
| Final authoring of DVD              |
+-------------------------------------+
EOF
	[ -e "$DESTDIR" ] && rm -R "$DESTDIR"
	echo dvdauthor -x "$XMLFILE" >> "$CMDFILE"
	dvdauthor -x "$XMLFILE" >> "$LOGFILE" 2>&1  || error_out
	echo "...done"
else
	echo "Only XML-file creation chosen. Removing temporary DVD structure"
    rm -R "$DESTDIR"
    echo "You may find the dvdauthor XML in a file called $XMLFILE"
fi
echo "dvdwizard finished successfully. Bye ;-)"
echo "dvdwizard finished successfully at `date +%d.%m.%Y\ -\ %H:%M:%S`" >> "$LOGFILE"

return 0
}

# ------------------------------
# Main Processing
#
#
# Ok, first define some default values
#
set_defaults "$@"
vts=""
allargs="$*"
restart=""

#
# Now deal with command line arguments
#
while [ -n "$*" ]; do
    case "$1" in
  	-h|--help)
   	    usage
  	;;
  	-v|--version)
   	    echo $dvdwizard_ver
            exit 0
  	;;
  	-C|--config-file)
	    shift
# -C and it's following parm already processed in set_defaults()
    	    shift
  	;;
	-o|--output)
 	    shift
	    DESTDIR="$1"
	    shift
  	;;
  	-x|--xml)
	    shift
   	    XMLFILE="$1"
   	    shift
  	;;
  	-l|--logfile)
	    shift
   	    LOGFILE="$1"
   	    shift
  	;;
	--xmlonly)
	    XMLONLY=1
	    shift
  	;;
  	-T|--vmgm)
	    shift
   	    VMGMTITLE="$1"
   	    shift
	;;
  	-B|--vmgmbg)
	    shift
   	    VMGMBG="$1"
   	    shift
  	;;
  	-N|--tvnorm)
	    shift
   	    TVNORM=$(echo "$1" | tr [a-z] [A-Z])
            if [ "$TVNORM" == "PAL" -o "$TVNORM" == "NTSC" ]; then
        	:
	    else
        	echo "Incorrect TV-Norm $TVNORM specified, only PAL and NTSC are supported. Aborting" >&2
    		usage
            fi
   	    shift
  	;;
  	-V|--tvsize)
	    shift
   	    TVSIZE="$1"
   	    shift
  	;;
  	-WS)
	    shift
   	    DVD_WS=$(echo "$1" | tr [a-z] [A-Z])
	    if [ "$DVD_WS" == "NOPANSCAN" -o "$DVD_WS" == "NOLETTERBOX" ]; then
	    	:
	    else
	    	echo "Incorrect Widescreen-Option $DVD_WS specified, only NOPANSCAN or NOLETTERBOX are supported. Aborting" >&2
		usage
	    fi
   	    shift
  	;;
  	-P|--palette)
	    shift
	    PALETTE="$1"
   	    shift
  	;;
  	-A|--Audio)
	    shift
	    DVDAUDIO="$1"
   	    shift
  	;;
  	--audio-seq)
   	    AUDIOSEQUENTIAL=1
   	    shift
  	;;
  	--notitle)
   	    NOTITLE=1
   	    shift
  	;;
  	-t|--vts)
	    shift
    	    if [ -z "$vts" ]; then
    		vts=1
            else
        	let vts+=1
            fi
	    eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
    	    eval VTSMBG_$vts=\"\$VTSMBG\"
            eval VTSMAUDIO_$vts=""
	    eval VTSMWS_$vts=""
	    eval VTSMSUBS_$vts=""
	    if [ -e "$1" ]; then
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
	        eval VTSM_"$vts"_FILES=1
	        eval VTSM_"$vts"_FILE_1=\"\$1\"
   	    else
   	    	eval VTSTITLE_$vts=\"\$1\"
        	eval VTSM_"$vts"_FILES=0
     	    fi
   	    shift
  	;;
  	-c|--chapters)
	    shift
            if [ -z "$vts" ]; then
        	vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval VTSMBG_$vts=\"\$VTSMBG\"
		eval VTSMAUDIO_$vts=""
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMWS_$vts=""
	    	eval VTSMSUBS_$vts=""
            fi
	    eval CHAPTERSPEC_$vts=\"\$1\"
   	    shift
  	;;
  	-b|--vtsmbg)
	    shift
            if [ -z "$vts" ]; then
        	vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
		eval VTSMAUDIO_$vts=""
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMWS_$vts=""
	    	eval VTSMSUBS_$vts=""
            fi
	    eval VTSMBG_$vts=\"\$1\"
   	    shift
  	;;
  	-ws)
	    shift
            if [ -z "$vts" ]; then
        	vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval VTSMBG_$vts=\"\$VTSMBG\"
		eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
		eval VTSMAUDIO_$vts=""
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMSUBS_$vts=""
            fi
	    VTSMWS=$(echo "$1" | tr [a-z] [A-Z])
	    if [ "$VTSMWS" == "NOPANSCAN" -o "$VTSMWS" == "NOLETTERBOX" ]; then
	    	eval VTSMWS_$vts=\"\$VTSMWS\"
	    else
	    	echo "Incorrect Widescreen-Option $1 specified, only NOPANSCAN or NOLETTERBOX are supported. Aborting" >&2
		usage
	    fi
   	    shift
  	;;
  	-a|--audio)
	    shift
            if [ -z "$vts" ]; then
        	vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
		eval VTSMBG_$vts=\"\$VTSMBG\"
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMWS_$vts=""
	    	eval VTSMSUBS_$vts=""
            fi
   	    eval VTSMAUDIO_$vts=\"\$1\"
   	    shift
  	;;
  	-s|--subpic)
	    shift
            if [ -z "$vts" ]; then
        	vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
		eval VTSMBG_$vts=\"\$VTSMBG\"
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMWS_$vts=""
   	    	eval VTSMAUDIO_$vts=""
            fi
	    eval VTSMSUBS_$vts=\"\$1\"
   	    shift
  	;;
  	--restart)
	    shift
            case "$1" in
    		dvdcpics|mk_vmgm|mk_vtsm|author)
        	    restart="$1"
    		;;
    		*)
        	    echo "Incorrect Restart-Parameter specified: $1" >&2
        	    usage
		;;
    	    esac
   	    shift
  	;;
  	*)
            if [ -z "$vts" ]; then
    		vts=1
		eval VTSTITLE_$vts=\"\$VTSTITLE\"
		eval CHAPTERSPEC_$vts=\"\$CHAPTERSPEC\"
		eval VTSMBG_$vts=\"\$VTSMBG\"
		eval VTSMAUDIO_$vts=""
		eval VTSM_"$vts"_FILES=0
	    	eval VTSMWS_$vts=""
	    	eval VTSMSUBS_$vts=""
    	    fi
    	    eval fnum=\$VTSM_"$vts"_FILES
    	    let fnum+=1
    	    eval VTSM_"$vts"_FILES=\$fnum
    	    eval VTSM_"$vts"_FILE_"$fnum"=\"\$1\"
   	    shift
  	;;
    esac
done

#
# Clear the log if we are not restarting
#
[ -z "$restart" ] && rm -f "$LOGFILE"

#
# Let's see, if all options are correct so far
#
checkParms

#
# If restart was specified, start with the given step
#
case "$restart" in
dvdcpics)
	call_dvdcpics
    call_mk_vmgm
    call_mk_vtsm
    call_author
;;
mk_vmgm)
	call_mk_vmgm
    call_mk_vtsm
    call_author
;;
mk_vtsm)
	call_mk_vtsm
    call_author
;;
author)
    call_author
;;
*)
#
# Start from the very beginning
# Save Commands generated from dvdwizard
#
	echo "`basename $0` $allargs" > "$CMDFILE"

	cat <<EOF > "$XMLFILE"
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- dvdauthor xml-file created with `basename $0` at `date +%d.%m.%Y\ -\ %H:%M:%S`
     Called with `basename $0` "$( echo $allargs | sed -e s/--/-/g)" //-->
<dvdauthor dest="$DESTDIR">
EOF

#
# First step: Create temporary DVD-image with chapters for chapter-menu-creation
# This step is to speed up generation of chapter thumbnails for the vtsm
# I do not know any CLI program which can extract single frames at a specified position of an mpeg stream
# except to transcode. But transcode is very slow in seeking (runs at approx 35fps on my Athlon1.4 - would take
# 90 minutes to seek a 2 hour movie). In DVD chapter mode, transcode is much faster because it reads from the first
# few frames only and so the 10 minutes to author the temporary DVD pay back with better overall performance
#
	echo -n Creating temporary DVD-structure...
	cat <<EOF >> "$LOGFILE"
+-------------------------------------+
| Calling mk_tmpdvd                   |
| Creating temporary DVD-structure    |
+-------------------------------------+
EOF

#
# Since enabling multi-title DVD-creation, mk_tmpdvd has been "degraded" from a stand-alone script to an
# internal function in dvdwizardrc.
# The script is of little use outside dvdwizard and parsing all needed parms would have resulted in a
# calling syntax nearly as complicated as using dvdauthor without xml, so I cut that. ;-)
#
	mk_tmpdvd >> "$LOGFILE" 2>&1 || error_out
	echo "...done."

#
# Ok, now process restartable steps one by one
#
	call_dvdcpics
	call_mk_vmgm
    call_mk_vtsm
    call_author
;;
esac

exit 0
