#!/usr/bin/env perl use Getopt::Std; getopts("mjthn:"); if ($opt_h) { print "Usage: cd2m [-mjth] [-n X]\n"; exit; } sub query { ($id = $_[0]) =~ s/ /+/g; return "wget -O - -q \"http://www.freedb.org/~cddb/cddb.cgi?cmd=cddb+$id\&hello=a+b+c+d\&proto=2\""; } open DISCID, "; chomp; close DISCID; open QUERY, &query("query $_") . "|" or die "couldn't do cddb query"; $_ = ; chop; chop; /^(\d+) (\S+) (\S+) (.*)$/ or die "cddb query failed: $_"; RC: { if ($1 eq "211" and $opt_n eq "") { print "no exact match found: invoke cd2m with \"-n N\" where N is:\n"; $n = 0; while () { chop; chop; last if /^\.$/; /^(\S+) (\S+) (.*)$/; print $n++ . ": $3\n"; } exit; } if ($1 eq "211") { while ($opt_n--); $_ = ; /^(\S+) (\S+) (.*)$/ or die "cddb query failed: $_"; $cmd = "read $1 $2"; $name = $3; last RC; } if ($1 eq "200") { $cmd = "read $2 $3"; $name = $4; last RC; } die "cddb query failed: $_"; } print "name = $name\n"; close QUERY; open QUERY, &query($cmd) . "|" or die "couldn't do cddb read"; ($group = $name) =~ s/ \/.*$//; $_ = ; chop; chop; /^(\d+) / or die "cddb read failed: $_"; ($1 eq "210") or die "cddb read failed: $_"; while () { chop; chop; $t{1+$1} .= $2 if (/^TTITLE(\d+)=(.*)$/i); } close QUERY; foreach $x (keys %t) { $xx = sprintf "%02d", $x; $_ = "$xx $t{$x} - $group.ogg"; s/\//:/g; $c = "mv track$xx.cdda.ogg \"$_\""; if ($opt_t) { print "$c\n"; } else { system $c; } }