#!/bin/sh

# jukebox - http://raf.org/jukebox/
#
# Copyright (C) 2002 raf <raf@raf.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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
# or visit http://www.gnu.org/copyleft/gpl.html

# cdr/cdrw - write files/directories or an iso9660 image to cdr/cdrw
#
# 20021208 raf <raf@raf.org>

# Set defaults and load configuration

cdr_wdev="0,0,0"
cdr_spd="24"
cdrw_spd="10"
cdr_opt="burnproof"

[ -r /etc/jukebox.conf ] && . /etc/jukebox.conf
[ -r ~/.jukeboxrc ] && . ~/.jukeboxrc

case "$0" in
	*cdr)  def_spd="$cdr_spd";;
	*cdrw) def_spd="$cdrw_spd";;
esac

help()
{
	name="`basename $0`"
	case "$name" in
		cdrw) suffix="w";;
		cdr)  suffix="";;
	esac

	echo "NAME"
	echo
	echo "$name - write files/directories or an iso9660 image to cdr$suffix"
	echo
	echo "SYNOPSIS"
	echo
	echo "  $name [options] arg..."
	echo "  options:"
	echo "    -h, --help    - Show the help message then exit"
	echo "    -V, --version - Show the version message then exit"
	echo "    -s, --speed # - Override default speed ($def_spd)"
	echo "    -i, --iso     - Argument is an iso9660 image"
	echo
	echo "DESCRIPTION"
	echo
	echo "$name writes files and directories or an iso9660 image to cdr$suffix."
	echo "All parameters such as the cdr$suffix device, the speed and so on"
	echo "are specified in the configuration file. The speed can be"
	echo "overridden with the -s option."
	echo
	echo "The command line arguments are passed directly through to mkisofs along"
	echo "with the -graft-points option. This means that if you specify a single"
	echo "directory, the root directory of the cdr$suffix will contain the contents"
	echo "of that directory. If you specify multiple directories, their contents"
	echo "will be merged to form the root directory of the cdr$suffix."
	echo
	echo "In order to place multiple directories in the cdr$suffix's root directory,"
	echo "you need to graft them into place. For example, to create a cdr$suffix"
	echo "containing the contents of the \"a\" directory (without the \"a\" directory"
	echo "itself), the \"b\" and \"c\" directories and the file \"d.e\":"
	echo
	echo "    $name a b/=b c/=c d.e"
	echo
	echo "Don't forget the slashes when grafting a directory into place"
	echo "or the cdr$suffix will contain its contents, not the directory itself."
	echo "See \"man mkisofs\" for more details."
	echo
	echo "The -i option treats the (single) non-option command line argument as the"
	echo "name of an iso9660 image file and burns it as an image, not as a file."
	echo "If you pass a single non-option command line argument to cdr$suffix, and"
	echo "it ends with \".cd\" or \".iso\", cdr$suffix will ask if you want it to assume"
	echo "the -i option. This allows you to be careless without wasting cdr${suffix}s."
	echo
	echo "EXAMPLES"
	echo
	echo "Write the contents of a directory to cdr$suffix"
	echo
	echo "  $name dir"
	echo
	echo "Write a file and three directories to cdr$suffix"
	echo
	echo "  $name file dir1/=dir1 dir2/=dir2 dir3/=dir3"
	echo
	echo "Write an iso9660 image to cdr$suffix"
	echo
	echo "  $name -i mycd.iso"
	echo
	echo "FILES"
	echo
	echo "  /etc/jukebox.conf - System wide configuration file"
	echo "  ~/.jukeboxrc      - User specific configuration file"
	echo
	echo "SEE ALSO"
	echo
	echo "  rip(1), riptrack(1), mktoc(1), toc2names(1), toc2tags(1),"
	echo "  cdr(1), cdrw(1), burn(1), burnw(1), cdbackup(1), mp3backup(1),"
	echo "  jukebox(1), jukeboxc(1), jukeboxc.jar(1), jukeboxd(8),"
	echo "  jukeboxd-init.d(8), jukebox.conf(5),"
	echo "  http://raf.org/jukebox/Jukebox-HOWTO"
	echo
	echo "AUTHOR"
	echo
	echo "raf <raf@raf.org>"
	echo
	exit 0
}

version() { echo "cdr-0.1"; exit 0; }
die() { echo "`basename $0`: $@" >&2; exit 1; }

ask()
{
	echo -n "$1 ([y]/n): "
	read response
	[ x"$response" != "xn" ]
}

looks_like_iso()
{
	case "$1" in
		*.cd|*.iso|*.iso9660)
			return 0;;
		*)
			return 1;;
	esac
}

# Check the arguments

getopt -T >&- 2>&- || eval set -- `getopt -q -o hVs:i -l help,version,speed:,iso -- "$@"`

iso="0"
while :
do
	case "$1" in
		-h|--help) help;;
		-V|--version) version;;
		-s|--speed) shift; cdr_spd="$1"; cdrw_spd="$1";;
		-i|--iso) iso="1";;
		-[^-]*) die "Invalid option: $1 (Note: options can't be combined on this system)";;
		--?*) die "Invalid option: $1";;
		--) ;;
		*) break;
	esac
	shift
done

[ $# = 0 ] && help

# Is it an iso9660 image, or files and directories?

if [ "$iso" = 1 ]
then
	[ $# != 1 ] && die "A single argument must follow the -i option"
	image="$1"
elif [ $# = 1 ] && looks_like_iso "$1" && ask "This looks like an iso9660 image. Should I assume -i?"
then
	iso=1
	image="$1"
else
	args="-graft-points $@"
fi

# Set the device, speed, blanking, tsize and other options

device="dev=$cdr_wdev"

case "$0" in
	*cdrw) speed="speed=$cdrw_spd"; blank="blank=fast";;
	*cdr)  speed="speed=$cdr_spd";;
	*)     die "Invalid command name: $0";;
esac

[ "$iso" = "0" ] && tsize="tsize=`mkisofs -rJq -print-size $args`s"
options="driveropts=$cdr_opt fs=16m -pad"

# Write to the cdr/cdrw

if [ "$iso" = 1 ]
then
	cdrecord $device $speed $blank $options "$image"
else
	mkisofs -rJq $args | cdrecord $device $speed $blank $tsize $options -
fi

# vi:set ts=4 sw=4
