#!/usr/bin/perl
# version 0.9.2
use CDDB_get qw( get_cddb );
use MP3::Info;
use constant TRUE    => 1;
use constant FALSE   => 0;

# rip: Rips audio CD's into mp3 format, queries remote CDDB database to
#      name them and fill in the mp3 ID fields for you.
#
# Copyright 2001 by Peter Jay Salzman <p@dirac.org>, released under GNU GPL.
# 
#  rip 3           rips track 3            Argument is either "--whole-cd" or
#  rip 2,4-6,8     rip tracks 2,4,5,6,8    a comma separated list, with "-"
#  rip --whole-cd  rip the whole cd        denoting a range of tracks.
#
# Requires:
#    * the CDDB_get perl module (by Armin Obersteiner), available at CPAN.
#    * cdparanoia, the best ripper around.
#    * lame, the best encoder around.


# User defined parameters
#
my $bitrate = "128";
my $mode    = "s";    # lame options: s=stereo, j=joint, m=mono
my $dryrun  = FALSE;
my $debug   = TRUE;


# Stuff you probably don't need to monkey with
#
my $cdrom     = "/dev/cdrom";
my $CDDBHOSTS = (freedb.freedb.org,us.freedb.org,uk.freedb.org,ca.freedb.org,at.freedb.org,cz.freedb.org,de.freedb.org);
my $DB_HOST   = "freedb.freedb.org";


# Don't know what to do with these
#
my $year = "";
my $comment = "";
my (%cd, %config, $track, $i, $artist, $filename, $genre);


$debug || print "debugging turned on\n";
$config{CDDB_HOST}="$DB_HOST";
$config{CDDB_PORT}=8880;
$config{CDDB_MODE}="cddb";
$config{CD_DEVICE}="$cdrom";
$config{input}=1;
%cd=get_cddb(\%config);
unless(defined $cd{title}) { die "no cddb entry found"; }


chomp($cdparanoia = `which cdparanoia`);
if ($cdparanoia eq "") { die "I can't find cdparanoia"; }
$debug && print "cdparanoia found at <$cdparanoia>\n";
chomp($lame = `which lame`);
if ($lame eq "")       { die "I can't find lame";}
$debug && print "lame found at <$lame>\n";




# ARGUMENT PROCESSING
#
# Concatenate the argument list then split the list by commas.
#
foreach (@ARGV) {
	if ($_ =~ /--whole-cd/) { goto DOIT; }
	$arglist .= $_;
}
@array = split(/,/, $arglist);

# Step through the array, finding a range character (the -).
# We have to use foreach instead of for to avoid infinite loop in push.
#
foreach (@array) {
	if (/^(\d*)-(\d*)/) {
		if ($2 eq "" || $1 eq "") { die "Malformed range"  };
		if ($2 < $1 )             { die "Bad range"  };
		foreach ($1 .. $2) { push(@array, $_); }
	}
}

# Now we have to get rid of elements like 5-7.
#
for ($i=0; $i<=$#array; $i++) {
	if ($array[$i] =~ /-/) { splice(@array, $i, 1); }
}





DOIT:

# This is for the --whole-cd directive
if (! defined @array) {
	foreach(1 .. $cd{tno}) { push(@array, $_); }
}


foreach (@array) {
	if ($dryrun) {
		print("$_: simulated rip of $cd{track}[$_-1] ($cd{artist}).mp3\n");
	} else {
		$track  = $cd{track}[$_-1];
		$artist = $cd{artist};
		$album  = $cd{title};
		$genre  = $cd{cat};

		$filename = "$track ($artist).mp3";
		$debug && print "$cdparanoia $_\n";
		system("$cdparanoia $_");
		system("$lame -b $bitrate -h -m $mode cdda.wav \"$filename\"");
		system("rm cdda.wav");
		set_mp3tag($filename, $title, $artist, $album, $year, $comment, $genre);
 	}
}
