#! /bin/sh
ME="[makevcd]:"
. tovid-init

# makevcd 
# Part of the tovid suite
# =======================
# A bash script for creating a VCD cue/bin set and burning it
# to a recordable CD.
#
# Project homepage: http://www.tovid.org
#
#
# Copyright (C) 2005 tovid.org <http://www.tovid.org>
# 
# 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., 675 Mass Ave, Cambridge, MA 02139, USA. Or see:
#
#           http://www.gnu.org/licenses/gpl.txt

SCRIPTNAME=`cat << EOF
--------------------------------
makevcd
A script to create cue/bin files for a (S)VCD and burn them to a CD
Part of the tovid suite, version $TOVID_VERSION
$BUILD_OPTIONS
$TOVID_HOME_PAGE
--------------------------------
EOF`

USAGE=`cat << EOF
Usage: makevcd [OPTIONS] {VCDIMAGER.xml}

Where OPTIONS may be any of the following:

  -overwrite         Overwrite existing ISO image
  -noburn            Author the disc image only
  -burn              (default noburn)
  -device DEVICE     (default /dev/cdrw)
  -speed NUM         (default 12)

And:

  VCDIMAGER.xml is the name of a file containing a VCDImager XML
      description (For XML format, see http://www.vcdimager.org/).
      If you use(d) 'makexml' to create the XML file, you can use
      that as input here.

See the makevcd manual page ('man makevcd') for additional documentation.

EOF`

SEPARATOR="=========================================="

# Print script name, usage notes, and optional error message, then exit.
# Args: $1 == text string containing error message
usage_error ()
{
  echo "$USAGE"
  echo $SEPARATOR
  echo "$@"
  exit 1
}

# Print out a runtime error specified as an argument, and exit
runtime_error ()
{
    killsubprocs
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    echo "makevcd encountered an error during the VCD creation process:"
    echo $@
    echo "See if anything in the above output helps you diagnose the"
    echo "problem, and please file a bug report containing the above"
    echo "output. Sorry for the inconvenience!"
    echo "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"
    exit 1
}
# Defaults
CDRW_DEVICE="/dev/cdrw"
BURN_SPEED=12

# ==========================================================
# EXECUTION BEGINS HERE
echo $"$SCRIPTNAME"

assert_dep_group "vcd" "You are missing dependencies required to image and burn (S)VCDs!"

while test x"${1:0:1}" = "x-"; do
    case "$1" in
      "-device" )
        # Get device name
        shift
        CDRW_DEVICE="$1"
	;;
      "-speed" )
        shift
        BURN_SPEED=$1
	;;
      * )
        usage_error "Error: Unrecognized command-line option $1"
	;;
    esac

    # Get next argument
    shift
done

if test $# -ne 1; then
    usage_error "Please provide the name of a VCDimager XML file \
containing the (S)VCD description."
else
    # XML input is last argument
    VCDIMAGER_XML="$1"
fi

# Remind user to insert a CD
echo "Please insert a blank CD-R(W) disc into your CD-recorder"
echo "($CDRW_DEVICE) if you have not already done so."

# Sanity check: Make sure given device is valid (somehow)
# before creating the cue/bin. Give option to proceed anyway?
# (i.e., could there be any point to proceeding?)
# Here's a simple way: just quit
if test -b $CDRW_DEVICE; then :
   else
   echo "Couldn't find $DDRW_DEVICE! Stopping here."
   exit 1
fi

# Create cue/bin
# Warn if there isn't enough space to make cue/bin
VCDIMAGER_CMD="vcdxbuild -c \"$VCDIMAGER_XML.cue\" -b \
    \"$VCDIMAGER_XML.bin\" \"$VCDIMAGER_XML\""
echo $SEPARATOR
echo "Creating cue/bin disc image with the following command:"
echo $VCDIMAGER_CMD
( eval $VCDIMAGER_CMD )

# Burn the disc
CDRDAO_CMD="cdrdao write --device $CDRW_DEVICE --driver generic-mmc \
    --speed $BURN_SPEED \"$VCDIMAGER_XML.cue\""
echo $SEPARATOR
echo "Burning cue/bin image to $CDRW_DEVICE with the following command:"
echo $CDRDAO_CMD
if eval $CDRDAO_CMD; then 
    echo "Done. You should now have a working VCD or SVCD. Please report"
    echo "any problems you might have to wapcaplet99@yahoo.com."
else 
    runtime_error "Could not burn the disc to $CDRW_DEVICE at speed $BURN_SPEED"
fi
exit 0
