+B<fa-rip> [B<-d> I<device>] [B<-p> I<post-processor> [B<-t> I<track-count>]
+
+=cut
+
+use strict;
+use warnings;
+
+use Env qw(
+ CDDEV
+);
+
+use Fcntl qw(O_CREAT O_WRONLY);
+use File::Temp;
+use Getopt::Long qw(:config gnu_getopt no_ignore_case);
+use POSIX ':sys_wait_h';
+use Pod::Usage;
+
+use MusicBrainz::Client::Simple;
+
+sub run_or_die {
+ my $command = shift;
+ my $status;
+
+ $status = system($command);
+
+ if (WIFEXITED($status)) {
+ if (($status = WEXITSTATUS($status)) != 0) {
+ die("$command exited with status $status");
+ }
+ } elsif (WIFSIGNALED($status)) {
+ die("$command killed with signal ", WTERMSIG($status));
+ } elsif (WIFSTOPPED($status)) {
+ die("$command stopped with signal ", WSTOPSIG($status));
+ } else {
+ die("Major horkage on system($command): \$? = $? \$! = $!");
+ }
+}
+
+sub mkcue {
+ my $device = shift;
+ my $trackcount = shift;
+ my @command;
+
+ push(@command, 'mkcue');
+
+ if (defined($trackcount)) {
+ push(@command, "-t $trackcount");
+ }
+
+ if (defined($device)) {
+ push(@command, $device);
+ }
+
+ push(@command, '> cue');
+ run_or_die(join(' ', @command));
+
+ if (not defined($trackcount)) {
+ open(F, 'cue') or die("open(cue): $!");
+ $trackcount = grep(/TRACK.*AUDIO/, <F>);
+ close(F);
+ }
+
+ return $trackcount;
+}
+
+sub tags_file {
+ my $fn = shift;
+ my $trackcount = shift;
+ my $various = shift;
+ my $artist = shift;
+ my $album = shift;
+ my $fh;
+ my $i;
+ my $track;
+ my $name;
+
+ open($fh, '>', $fn) or die("open('>$fn'): $!");
+ print($fh 'ARTIST=', (defined($artist) and $artist or ''), "\n");
+ print($fh 'ALBUM=', (defined($album) and $album or ''), "\n");
+ # MusicBrainz doesn't have dates yet; these are usually wrong anyway.
+ print($fh "DATE=\n");
+
+ for $i (1 .. $trackcount) {
+ $various and print($fh "ARTIST[$i]=\n");
+ if ($track = shift(@_)) {
+ $name = $track->get_name;
+ } else {
+ $name = '';
+ }
+ print($fh "TITLE[$i]=$name\n");
+ }
+
+ close($fh) or die("close(>$fn): $!");
+}
+
+sub tags {
+ my $device = shift;
+ my $trackcount = shift;
+ my $mb;
+ my @results;
+ my $album;
+ my $i;
+ my $various;
+ my $seen_various;
+
+ if (defined($device)) {
+ $mb = new MusicBrainz::Client::Simple (device=>$device);
+ } else {
+ $mb = new MusicBrainz::Client::Simple;
+ }
+
+ @results = $mb->lookup_cd;
+ if (not $mb->success) {
+ die($mb->get_error);
+ }
+
+ tags_file('candidate-tags-0', $trackcount, 0);
+
+ for $album (@results) {
+ $i++;
+
+ if ($various = $album->has_various_artists) {
+ if (not $seen_various) {
+ $seen_various = 1;
+ tags_file('candidate-tags-0v', $trackcount, 1);
+ }
+ }
+
+ tags_file("candidate-tags-$i", $trackcount, $various,
+ $album->get_artist->get_name, $album->get_name,
+ $album->get_tracks);
+ }
+}
+
+sub rip {
+ my $device = shift;
+ my $trackcount = shift;
+
+ $device ||= '/dev/cdrom';
+
+ exec('cdparanoia', '-d', $device, "1-$trackcount", 'wav');
+ # exec prints its own error message so just
+ die;
+}
+
+sub make_post_processor {
+ my $command = shift;
+
+ defined($command) or return;
+
+ sysopen(F, 'post-processor', O_CREAT | O_WRONLY, 0555)
+ or die("sysopen(post-processor, O_CREAT | O_WRONLY, 0555): $!");
+ print(F $command, ' "$@"', "\n");
+ close(F) or die("close(post-processor, O_CREAT | O_WRONLY, 0555): $!");