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

# mktoc - create a table of contents template for some wav/mp3/ogg/flac... files
#
# 20021208 raf <raf@raf.org>

# Set defaults and load configuration

tocfile="00.toc"
juke_fmt="wav,mp3,ogg,flac"

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

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

	echo "NAME"
	echo
	echo "$name - create a table of contents template for some wav/mp3/ogg/flac... files"
	echo
	echo "SYNOPSIS"
	echo
	echo "  $name [options] [artist] [title]"
	echo "  options:"
	echo "    -h, --help            - Show the help message then exit"
	echo "    -V, --version         - Show the version message then exit"
	echo "    -n, --nocddb          - Do not ask to obtain cddb information"
	echo "    -t, --tocfile tocfile - Override default tocfile name ($tocfile)"
	echo "    -f, --formats formats - Override default formats ($juke_fmt)"
	echo
	echo "DESCRIPTION"
	echo
	echo "$name creates a table of contents template in a format suitable"
	echo "for use with jukebox. If cddb information is to be obtained from"
	echo "freedb.org, then the CD in question must be in the drive when this"
	echo "is run. Then the table of contents will be prepopulated."
	echo
	echo "Otherwise, the artist and title will be populated with the command"
	echo "line arguments (if supplied) and there will be a line containing an"
	echo "index number for each track file found in the current directory."
	echo "The track titles (and possibly the artist an album title) will need"
	echo "to be entered manually. Even if cddb information is used, further"
	echo "annotation will almost certainly be required so use your favourite"
	echo "text editor to finish the tocfile. Tocfiles look something like:"
	echo
	echo "  Artist: Janis Joplin"
	echo "  Title: Cheap Thrills"
	echo "  Jukebox: rock blues 60s hard awesome female vocal"
	echo "  cddbid: 5608b807"
	echo
	echo "  01 Combination of the Two"
	echo "  02 I Need a Man to Love"
	echo "  03 Summertime"
	echo "  04 Piece of My Heart"
	echo "  05 Turtle Blues"
	echo "  06 Oh, Sweet Mary"
	echo "  07 Ball and Chain"
	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 "mktoc-0.1"; exit 0; }
die() { echo "`basename $0`: $@" >&2; exit 1; }

# Check the arguments

getopt -T >&- 2>&- || eval set -- `getopt -q -o hVnt:f: -l help,version,nocddb,tocfile:,formats: -- "$@"`

cddb="yes"

while :
do
	case "$1" in
		-h|--help) help;;
		-V|--version) version;;
		-n|--nocddb) cddb="no";;
		-t|--tocfile) shift; tocfile="$1";;
		-f|--formats) shift; juke_fmt="$1";;
		-[^-]*) die "Invalid option: $1 (Note: options can't be combined on this system)";;
		--?*) die "Invalid option: $1";;
		--) ;;
		*) break;;
	esac
	shift
done

# Create the tocfile (cddb or manual)

if [ "$cddb" = "yes" ]
then
	echo -n "Obtain TOC from the net? ([y]/n): "
	read response
	[ "x$response" = "xn" ] && cddb="no"
fi

if [ "$cddb" = "yes" ]
then
	perl -we '
		$| = 1;
		use CDDB_get qw(get_cddb get_discids);
		my %config = (input => 1, multi => 0);
		$config{HTTP_PROXY} = $ENV{http_proxy} if $ENV{http_proxy};
		my (@cd) = get_cddb(\%config);
		print "There is no cddb entry for this CD.\nWill create an empty template for you to edit.\n" unless defined $cd[0];
		exit unless $#cd > 0 && $#cd & 1;
		my %cd = @cd;
		my $n = 0;
		exit unless defined $cd{title};
		my $ref = get_discids();
		my $local_id = sprintf "%08x", $ref->[0];
		if ($cd{id} ne $local_id)
		{
			print "Remote id ($cd{id}) does not match local id ($local_id).\nUse local id? ([y]/n): ";
			$cd{id} = $local_id unless <STDIN> =~ /n/i;
		}
		open(TOC, "> $ARGV[0]") or die "Failed to open $ARGV[0] for writing.\n";
		print TOC "Artist: $cd{artist}\n";
		print TOC "Title: $cd{title}\n";
		print TOC "Jukebox: $cd{cat}\n";
		print TOC "cddbid: $cd{id}\n\n";
		print TOC sprintf("%02d %s\n", ++$n, $_) for (@{$cd{track}});
		close TOC;
	' "$tocfile"
fi

if [ ! -s "$tocfile" ]
then
	(
		echo "Artist: $1"
		echo "Title: $2"
		echo "Jukebox: "
		cddbid="`perl -we '
			use CDDB_get qw(get_discids);
			my $ref = get_discids();
			print sprintf "%08x\n", $ref->[0];
		' 2>/dev/null`"
		[ -n "$cddbid" ] && echo "cddbid: $cddbid"
		echo

		for ext in `echo $juke_fmt | sed 's/,/ /g'`
		do
			tracks="`echo *.$ext`"
			[ "$tracks" != "*.$ext" ] && break
		done

		case "$tracks" in
			\**) die "Failed to find any ($juke_fmt) track files"
		esac

		for track in $tracks
		do
			index="`echo $track | sed 's/^[^0-9]*\([0-9][0-9]\)[^0-9].*$/\\1/'`"
			echo "$index "
		done
	) > "$tocfile"
fi

# vi:set ts=4 sw=4
