#!/usr/bin/perl -s
##
## cddb-ops - cddb operations for rip in peace.
##
## 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: cddb-ops,v 1.2 2000/09/07 00:14:40 root Exp root $

use Net::CDDB;
use Persistence::Object::Simple; 
use MP3::Info;

$cachedir  = "/tmp/cddb" unless $cachedir; 
$device = '/dev/cdrom' unless $device;
$discid = '/usr/bin/cd-discid' unless $discid;

if ($v) { 
    my ($version) = '$Revision: 1.2 $' =~ /\s(\d+\.\d+)\s/; 
    print "cddb-ops $version\n";
    exit 0;
}

die "syntax: $0 [-rename] [-store] [-create] [-archivedir=foo] [-unstore] 
                   [-cache] -albumdir=directory with mp3/wavs\n\n" 
    unless $albumdir || $archivedir || $cache;

$albumdir =~ s:/$::;

if ($unstore) { 
    qx{rm $albumdir/.tracks};
    qx{rm $albumdir/.discid};
    qx{rm $albumdir/.title};
    exit 0;
}

if ($cache) { 
    my $dd = `$discid $device`;
    my $query = cached_cddb_query($dd);
    exit 0;
}
 
if ($create) { 
    $archivedir =~ s:/$::;
    my $dd = `$discid $device`; my $query = cached_cddb_query($dd);
    my ($cat,$disc_id,$artist,$title) = 
       ($$query{cat},$$query{disc_id},$$query{artist},$$query{title});
    $artist =~ y/A-Z/a-z/;  $artist =~ s/\s/_/g;
    $title =~ y/A-Z/a-z/;  $title =~ s/\s/_/g;
    mkdir "$archivedir/$artist", 0770 unless -e "$archivedir/$artist";
    mkdir "$archivedir/$artist/$title", 0770 unless 
        -e "$archivedir/$artist/$title";
    print "$archivedir/$artist/$title\n";
    exit 0;
}

my (@tracks,$dd,$cat,$disc_id,$artist,$title);
if ($store||$rename) { 
       
    if (-e "$albumdir/.discid" ) { 
        open ID, "$albumdir/.discid";
        $dd = <ID>; close ID;
    } else { 
        $dd = `$discid $device` 
    }
    chomp $dd;

    if ( -e "$albumdir/.tracks" ) { 
        open TRACKS, "$albumdir/.tracks"; 
        @tracks = <TRACKS>; close TRACKS; 
        chomp @tracks;
        open TITLE, "$albumdir/.title"; 
        ($title,$artist,$cat) = <TITLE>; close TITLE;
        chomp $title; chomp $artist; chomp $cat;
    } else { 
        my $query =  cached_cddb_query($dd);
        ($cat,$disc_id,$artist,$title) = 
          ($$query{cat},$$query{disc_id},$$query{artist},$$query{title});
        die "Can't connect to CDDB" unless $title; 
        @tracks = @{$query->{tracks}};
    }
}

if ($store) { 
    open ID, ">$albumdir/.discid"; print ID "$dd\n"; close ID; 
    $" = "\n"; open TRACKS, ">$albumdir/.tracks"; 
    print TRACKS "@tracks\n"; close TRACKS;
    open TITLE, ">$albumdir/.title"; 
    print TITLE "$title\n$artist\n$cat\n";  close TITLE;
    exit 0;
}

if ($rename) { 
    my @files = `ls $albumdir/track*`;
    for ( @files ) { 
        chomp;
        if ( /(\d\d)\.cdda.(mp3|wav)/ ) { 
            local $song = $tracks[$1];
            my $nn = "$1 - $tracks[$1].$2"; 
            $nn =~ s:[/]:-:g;
            $nn =~ s:["]:':g;
            print "$_ ==> $nn\n";
            system "mv \"$_\" $albumdir/\"$nn\"";
            if ( $2 =~ /mp3/i ) { 
               set_mp3tag( "$albumdir/$nn",  
                  { TITLE => $song,  ARTIST => $artist, 
                    ALBUM => $title, GENRE => $cat }
               );
            }
        }
    }
}

sub cached_cddb_query { 

    my ($id) = shift;
    mkdir $cachedir, 0775 unless -e $cachedir;
    my $f = $id; $f =~ s/\s//g;
    if ( -e "$cachedir/$f" ) { 
       my $entry = new Persistence::Object::Simple __Fn => "$cachedir/$f"; 
       return $entry; 
    }  
    my ($cat,$disc_id,$artist,$title) = cddb_query($id);
    # print "got artist/title - $artist, $title\n" if $d;
    my ($undef,@tracks) = cddb_read("$cat $disc_id");
    # print "got trackinfo\n" if $d;
    die "Can't connect to CDDB" unless $title; 
    my $entry = new Persistence::Object::Simple __Fn => "$cachedir/$f"; 
    %$entry = ( %$entry, cat => $cat, disc_id => $disc_id, 
                artist => $artist, title => $title, tracks => [@tracks], 
              );
    $entry->commit();
    return $entry;

}

=head1 NAME

cddb-ops - CDDB operations for rip(3). 

=head1 SYNOPSIS

    cddb-ops -create -archivedir=/home/ftp/MUSIC
    cddb-ops -store -albumdir=/home/ftp/MUSIC/roxette/crash_boom_bang
    cddb-ops -unstore -albumdir=/home/ftp/MUSIC/roxette/crash_boom_bang
    cddb-ops -rename -albumdir=/home/ftp/MUSIC/roxette/crash_boom_bang
    cddb-ops -cache

=head1 DESCRIPTION

This program talks to the CDDB server to get album/artist/track information
for an audio CD.  It's usually invoked from rip(3), but also provides some
end-user functionality.

=head1 PARAMETERS

[coming soon]

=head1 AUTHOR

Vipul Ved Prakash, mail@vipul.net

=cut


