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

# cdbackup - backup one audio cd onto a cdr
#
# 20021208 raf <raf@raf.org>

# Set defaults and load configuration

cdr_rdev="0,0,0"
cdr_wdev="0,0,0"
cdr_spd="24"
juke_tmp="/mnt/music/tmp"
cdr_bak="cdrdao"
cdr_drv="generic-mmc"
cdr_sdrv="generic-mmc"

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

help()
{
	name="`basename $0`"

	echo "NAME"
	echo
	echo "$name - backup one audio cd onto a cdr"
	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 "    -t, --tmp dir    - Override default tmpdir ($juke_tmp)"
	echo "    -s, --speed #    - Override default speed ($cdr_spd)"
	echo "    -w, --writer cmd - Override default writer ($cdr_bak)"
	echo
	echo "DESCRIPTION"
	echo
	echo "$name backs up an audio cd onto a cdr. The -t option overrides"
	echo "the default temporary directory to use. The -s option overrides"
	echo "the default speed. The -w option overrides the default writer"
	echo "software to use. The only valid values are \"cdrdao\" and"
	echo "\"cdrecord\". cdrdao is better because it preserves the gap"
	echo "between tracks. cdrecord always puts a two second gap between"
	echo "tracks. cdrdao also preserves the cddbid of the cd. If cdrdao"
	echo "needs to be told which driver to use, set it in cdr_drv and"
	echo "cdr_sdrv in /etc/jukebox.conf."
	echo
	echo "After backing up the audio cd, the user is given the option of"
	echo "creating further backups and finally of deleting the temporary"
	echo "data. If the data is not deleted, it can be used the next time"
	echo "$name is run (instead of reading a new cd)."
	echo
	echo "All parameters such as the cdr device, the speed and so on"
	echo "are specified in the configuration file."
	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 "cdbackup-0.1"; exit 0; }
die() { echo "`basename $0`: $@" >&2; exit 1; }

prompt() { echo -n "$@ then press return."; read return; }
askyes() { echo -n "$@ ([y]/n): "; read response; [ x"$response" != "xn" ]; }
askno() { echo -n "$@ (y/[n]): "; read response; [ x"$response" = "xy" ]; }
exists() { [ x"`ls -1 $1 2>&-`" != "x" ]; }
cleanup() { [ "$create" = "yes" ] && rm -rf "$juke_tmp" || delete; }

# Check the arguments

getopt -T >&- 2>&- || eval set -- `getopt -q -o hVt:s:w: -l help,version,tmp:,speed:,writer: -- "$@"`

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

# Prepare the tmp directory

[ -d "$juke_tmp" ] && create="no" || create="yes"

if [ "$create" = "yes" ]
then
	mkdir -p "$juke_tmp" || die "Failed to mkdir $juke_tmp"
fi

# Change to the tmp directory

cd "$juke_tmp" || die "Failed to cd $juke_tmp"

# The cdrecord method

do_cdrecord()
{
	delete() { rm -f *.wav; }

	exists '*.wav' && askyes "Use existing wav files?" ||
	(
		delete
		prompt "Insert an audio cd to backup"
		rip --wav --notoc
	)

	while :
	do
		prompt "Insert a blank cdr to write to"
		burn --speed "$cdr_spd" *.wav
		askno "Make another backup?" || break
	done

	askyes "Delete temporary data?" && cleanup
}

# The cdrdao method

do_cdrdao()
{
	delete() { rm -f cd*.toc cddata*.bin; }
	cdrdao_opts="--device $cdr_wdev ${cdr_drv:+--driver $cdr_drv} --speed $cdr_spd -n --keepimage"
	[ "$cdr_rdev" != "$cdr_wdev" ] && cdrdao_copyopts="--source-device $cdr_rdev ${cdr_sdrv:+-source-driver $cdr_sdrv}"

	exists '*.toc' && askyes "Use existing data?" ||
	(
		delete
		prompt "Insert an audio cd to backup"
		cdrdao copy $cdrdao_opts $cdrdao_copyopts
	)

	while askno "Make another backup?"
	do
		prompt "Insert a blank cdr to write to"
		cdrdao write $cdrdao_opts *.toc
	done

	askyes "Delete temporary data?" && cleanup
}

# Copy an audio cd to a cdr

case "$cdr_bak" in
	cdrecord) do_cdrecord;;
	cdrdao)   do_cdrdao;;
	*) die "Invalid: cdr_bak='$cdr_bak' (Must be 'cdrecord' or 'cdrdao')";
esac

# vi:set ts=4 sw=4
