#!/usr/bin/perl -s
##
## RIP In Peace - perl glue to automate cd ripping. 
##
## Copyright (c) 2000, Vipul Ved Prakash.  All rights reserved.
## This code is free software; you can redistribute it and/or modify
## it under the same terms as Perl itself.
##
## $Id: rip,v 1.11 2000/09/09 13:24:37 root Exp root $

($VERSION)  = '$Revision: 1.11 $' =~ /\s(\d+\.\d+)\s/; 
$archivedir = "/root/0/MUSIC/Artist" unless $archivedir;
$device     = '/dev/cdrom' unless $device;
$bitrate    = "160" unless $bitrate;
$cdparanoia = '/usr/local/bin/cdparanoia' unless $cdparanoia;
$cddbops    = '/usr/bin/cddb-ops' unless $cddbops;
$mlame      = '/usr/bin/mlame' unless $mlame;
$eject      = '/usr/bin/eject' unless $eject;
$discid     = '/usr/bin/cd-discid' unless $discid; 

for (qw/cdparanoia cddbops mlame eject discid/) { 
    die "$$_ missing.  See Requirements section in rip manual page.  Aborting.\n" unless -e $$_ 
}

if ($v) { 
    print "rip $VERSION\n";
    exit 0;
}

print "getting album info from cddb...\n" if $d; 
$albumdir = qx{$cddbops -create -archivedir=$archivedir -device=$device -discid=$discid -d=$d} unless $albumdir;
chomp $albumdir; chdir $albumdir;
print "ripping in $albumdir...\n" if $d;
die "Please specify -albumdir.\n" unless $albumdir;
system "$cddbops -albumdir=$albumdir -store -device=$device -discid=$discid";
    
if ($economic) {
    for ( 1 .. titles($albumdir) ) {  
        system "$cdparanoia -B -d $device $_"; 
        system "$mlame -r -o \"-b $bitrate\" *.wav";
        system "$cddbops -albumdir=$albumdir -rename -device=$device -discid=$discid";
    }
    system "$cddbops -albumdir=$albumdir -unstore";
    system "$eject $device";
} else {
    system "$cdparanoia -B -d $device 1-"; 
    system "$eject $device";
    system "$mlame -r -o \"-b $bitrate\" *.wav";
    system "$cddbops -albumdir=$albumdir -rename -device=$device -discid=$discid";

}

sub titles { 
    my $albumdir = shift;
    open T, "$albumdir/.tracks" || die $!;
    my @T = <T>; close T; 
    return $#T; 
}


=head1 NAME

rip - rip in peace (Audio CD archiving tool.)

=head1 SYNOPSIS

    rip -d 
    rip -d -economic
    rip -d -archivedir=/home/ftp/music
    rip -d -device=/dev/hdd
    rip -d -bitrate=128 

=head1 DESCRIPTION

rip automates the process of ripping, encoding, and archiving audio CDs as
mp3 files.  Ripping and encoding is done with cdparanoia and lame.  CDs are
ripped in artist/album/ directory under archivedir.  .wavs are deleted after
encoding and mp3s are named as "tracknum - track name.mp3".

rip talks to the cddb database to determine artist, album and track names
for the audio CD.  cddb operations are abstracted in cddb-ops, a perl script
that comes with rip distribution.  cddb-ops also ID3 tags MP3 based on track
information.

=head1 PARAMETERS

=head2 B<-economic>

Rip and encode tracks one by one.  Default behaviour is to rip the entire CD
before encoding.  You want to use this option if you are short on disk
space.

=head2 B<-archivedir>

Base directory of your music archive.  The cd will be ripped under
archivedir/artist/album.  The default won't work for you, so you must always
supply this argument, or change the default permanently in the rip script.

=head2 -device

CDROM device.  Defaults to /dev/cdrom.  

=head2 B<-bitrate>

Bitrate for MP3 encoding.  Defaults to 160 kB/s.

=head2 B<-v>

Version information.

=head2 B<-d>

Verbose reporting.

=head2 B<-cdparanoia -mlame -eject -discid>

Complete paths to these programs.  These have sensible defaults.  If
required, you can override them here or edit the rip script.

=head1 REQUIREMENTS

=head2 Perl Modules

  o Net::CDDB (http://www.wcnet.org/~jzawodn/perl/Net-CDDB/), 
  o Persistence::Object::Simple (http://search.cpan.org or CPAN shell),
  o MP3::Info (http://search.cpan.org or CPAN shell),

=head2 Applications

  o LAME (http://www.sulaco.org/mp3), 
  o cdparanoia (http://www.xiph.org/paranoia/)
  o discid (http://www.wcnet.org/~jzawodn/c/discid/)
  o eject 

=head1 DOWNLOAD 

The latest version of rip can be found at http://vipul.net/perl/sources/media/

=head1 SEE ALSO

cddb-ops(1), cdparanoia(1), eject(1), Net::CDDB(3), MP3::Info(3)

=head1 AUTHOR

Vipul Ved Prakash, mail@vipul.net

=head1 LICENSE

Artistic. 

=cut


