#!/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

# burn/burnw - write wav files onto an audio 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
	*burn)  def_spd="$cdr_spd";;
	*burnw) def_spd="$cdrw_spd";;
esac

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

	echo "NAME"
	echo
	echo "$name - write wav files onto an audio cdr$suffix"
	echo
	echo "SYNOPSIS"
	echo
	echo "  $name [options] *.wav"
	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
	echo "DESCRIPTION"
	echo
	echo "$name writes the wav files specified on the command line onto a cdr$suffix."
	echo
	echo "All parameters such as the cdr$suffix device, the speed and so on are"
	echo "specified in the configuration file."
	echo
	echo "EXAMPLES"
	echo
	echo "Write all wav files in the current directory to cdr$suffix:"
	echo
	echo "  burn$suffix *.wav"
	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 "burn-0.1"; exit 0; }
die() { echo "`basename $0`: $@" >&2; exit 1; }

# Check the arguments

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

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

[ $# = 0 ] && help

for arg
do
	case "$arg" in
		*.wav)
			;;
		*)
			die "$0: $arg is not a wav file"
	esac
done

# Set the device, speed, blanking and other options

device="dev=$cdr_wdev"

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

options="driveropts=$cdr_opt fs=16m -pad -dao -audio"

# Write to the cdr/cdrw

cdrecord $device $speed $blank $options "$@"

# vi:set ts=4 sw=4
