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

# riptrack - read selected tracks from an audio cd into wav files
#
# 20021208 raf <raf@raf.org>

# Set defaults and load configuration

cdr_rdev="0,0,0"
cdrd_spd="40"
cdr_rip="cdparanoia"

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

help()
{
	name="`basename $0`"
	echo "NAME"
	echo
	echo "$name - read selected tracks from an audio cd into wav files"
	echo
	echo "SYNOPSIS"
	echo
	echo "  $name [options] start-track [end-track]"
	echo "  options:"
	echo "    -h, --help       - Show the help message then exit"
	echo "    -V, --version    - Show the version message then exit"
	echo "    -C, --cdda2wav   - Read CD with cdda2wav"
	echo "    -P, --cdparanoia - Read CD with cdparanoia"
	echo "    -R, --reader cmd - Read CD with arbitrary \"cmd\""
	echo
	echo "DESCRIPTION"
	echo
	echo "$name reads a single track or a range of tracks from an audio cd."
	echo "The resulting wav files will reside in the current directory with"
	echo "whatever names they were given by the reader software (e.g. cdparanoia"
	echo "or cdda2wav)."
	echo
	echo "The default reader software to use is specified in /etc/jukebox.conf."
	echo "The -C, -P and -R options override the default reader."
	echo
	echo "To encode the wav files into some other format, use rip(1)."
	echo
	echo "EXAMPLES"
	echo
	echo  "Read the first three tracks from three CDs and burn them onto a CDR"
	echo
	echo  "    mkdir compilation"
	echo
	echo  "    # insert 1st cd"
	echo  "    riptrack 1 3"
	echo  "    mv *01*.wav compilation/01.wav"
	echo  "    mv *02*.wav compilation/02.wav"
	echo  "    mv *03*.wav compilation/03.wav"
	echo
	echo  "    # insert 2nd cd"
	echo  "    riptrack 1 3"
	echo  "    mv *01*.wav compilation/04.wav"
	echo  "    mv *02*.wav compilation/05.wav"
	echo  "    mv *03*.wav compilation/06.wav"
	echo
	echo  "    # insert 3rd cd"
	echo  "    riptrack 1 3"
	echo  "    mv *01*.wav compilation/07.wav"
	echo  "    mv *02*.wav compilation/08.wav"
	echo  "    mv *03*.wav compilation/09.wav"
	echo
	echo  "    # insert cdr"
	echo  "    burn compilation/*.wav"
	echo
	echo  "    # clean up"
	echo  "    rm -rf compilation"
	echo
	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 1
}

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

# Check the arguments

getopt -T >&- 2>&- || eval set -- `getopt -q -o hVCPR: -l help,version,cdda2wav,cdparanoia,reader: -- "$@"`

while :
do
	case "$1" in
		-h|--help) help;;
		-V|--version) version;;
		-C|--cdda2wav) cdr_rip="cdda2wav";;
		-P|--cdparanoia) cdr_rip="cdparanoia";;
		-R|--reader) shift; cdr_rip="$1";;
		-[^-]*) die "Invalid option: $1 (Note: options can't be combined on this system)";;
		--?*) die "Invalid option: $1";;
		--) ;;
		*) break;;
	esac
	shift;
done

[ $# = 0 ] && help

# Prepare command arguments

start="$1"
end="$2"

case "$cdr_rip" in
	cdda2wav)   rip_args="-B -O wav -D $cdr_rdev -S $cdrd_spd -t $start+${end:-$start}";;
	cdparanoia) rip_args="-B -w -S $cdrd_spd $start-${end:-$start}";;
esac

# Read the tracks into wav files

[ -z "$cdr_rip" ] && die "CD reader software not specified"
$cdr_rip $rip_args

# vi:set ts=4 sw=4
