#!/usr/bin/perl -w
#
# This script is pretty ridiculous. ie. It uses shell calls
# when I'm sure that there is a perl equivalent.

use strict;
use LWP;
use HTTP::Request::Common;
use String::ShellQuote;

$|=1;

# Location of the 'discid' program
# http://lly.org/~rcw/abcde/page/
my $discid = '/usr/local/bin/discid';
my $wget = '/usr/bin/wget';


# Get disc info
my $discinfo = `$discid`;
$discinfo =~ s/ /+/g;

my $useragent = LWP::UserAgent->new();
#my $request = POST("http://us.cddb.com/~cddb/cddb.cgi",
#	{
#		cmd=>"cddb query $discinfo",
#		hello=>"josh my.host.com cd2mp3 0.1",
#		proto=>5
#	});

my $request = GET("http://us.cddb.com/~cddb/cddb.cgi?cmd=cddb+query+$discinfo&hello=joe+my.host.com+xmcd+2.1&proto=5");

#my @query = `$wget -q -o /dev/null -O /dev/stdout 'http://us.cddb.com/~cddb/cddb.cgi?cmd=cddb+query+$discinfo&hello=joe+my.host.com+xmcd+2.1&proto=5'`;
my $response = $useragent->request($request);
my @query = $response->content;

if ($query[0] =~ /^211 /) {
	$query[0] =~ s/^211 Found inexact matches.*?\n//g;
	$query[0] =~ s/\r\n//g;
	$query[0] =~ s/\.$//g;
}

my ($discid_id,$tracks) = split(/\+/,$discinfo);
#print "QUERY: $query[0]\nDISCID: $discid_id\nTRACKS: $tracks\n";
#
print "DISCID: $discid_id\n";

my $input = '';
my $choice = 0;
print "====== Here's what we got:\n";
my $x = 0;

while ($x <= $#query) {
	unless ($query[$x] eq '.') {
		print "  $x: $query[$x]\n";
	}
	$x++;
}

print "Make a choice (0-$#query or q): ";

read(STDIN,$input,2);

$input =~ s/\n//g;

if ($input eq 'q') {
	print "Later dude.\n";
	exit 0;
} elsif ($input >= 0 && $input <= $#query) {
	$choice = $input;
} else {
	print "Invalid input ($input). Quiting...\n";
	exit;
}

# Which array peice to use?
my @artist_info = split(/ /,$query[$choice]);

print "ARTIST INFO: @artist_info\n";

# Don't need server response at this point.
if ($choice == 0 && $artist_info[0] == 200) {
	splice(@artist_info,0,1);
}
my $cat = $artist_info[0];
splice(@artist_info,0,1);
my $cdid = $artist_info[0];
splice(@artist_info,0,1);
my $artist = '';
foreach my $l (@artist_info) {
	$artist .= "$l ";
}
$artist =~ s/ $//;

print "CAT: $cat\n";
print "CDID: $cdid\n";

#my @disc_info = `$wget -q -o /dev/null -O /dev/stdout 'http://us.cddb.com/~cddb/cddb.cgi?cmd=cddb+read+$cat+$cdid&hello=joe+my.host.com+xmcd+2.1&proto=5'`;
`$wget -q -o /dev/null -O tmp.file 'http://us.cddb.com/~cddb/cddb.cgi?cmd=cddb+read+$cat+$cdid&hello=joe+my.host.com+xmcd+2.1&proto=5'`;
`dos2unix tmp.file`;
open(F,"tmp.file") or die "couldn't open tmp.file $!";
my @disc_info = <F>;
close(F) or die "couldn't close tmp.file $!";

print "CD Info:\n";
foreach my $l (@disc_info) {
	$l =~ s/[\r\n\cM]//g;
	print "$l\n";
}
print "\nContinue?(y/n): ";
$input = 'n';
read(STDIN,$input,1);
if ($input eq 'n') {
	print "Exiting...\n";
	exit;
} else {
	print "Going forward ($input)";
} 

# At this point we will assume we can get the info.
splice(@disc_info,0,1);
my $nartist = '';
my $nalbum = '';
my $nsong = '';
my $nyear = '';
my $title = '';
my %tracks = ();
foreach my $l (@disc_info) {
	$l =~ s/\n//g;
	$l =~ s/\r//g;
	$l =~ s/\r\n//g;
	$l =~ s/\cM//g;
	if ($l =~ /^DTITLE/) {
		warn "L IS: $l\n";
		$l =~ s/^DTITLE=(.*)$/$1/;
		$nsong = $1;
		warn "NSONG IS: $nsong\n";
		#$nsong =~ s/'/\\'/g;
	} elsif ($l =~ /^DYEAR/) {
		$l =~ s/^DYEAR=//;
		$nyear = $l;
	} elsif ($l =~ /^TTITLE/) {
		$l =~ s/^TTITLE([0-9]*)=(.*)$/$1 $2/;
		my $trackno = sprintf("%02d",$1);
		$trackno++;
		#push(@tracks,"- $trackno - $2");
		$tracks{$trackno} = $2;
		$tracks{$trackno} =~ s/[\n\r\cM]//g;
		#$tracks{$trackno} =~ s/'/\\\\'/g;
		$tracks{$trackno} =~ s/\//-/g;
	}
	# else: we don't care.
}

$nyear ||= '0000';
if ($nyear eq '0000') {
	print "\nYear is $nyear. Continue?(y/n): ";
	$input = 'n';
	read(STDIN,$input,1);
	if ($input eq 'n') {
		print "Exiting...\n";
		exit;
	} else {
		print "Going forward ($input)";
	}
}
($nartist,$nalbum) = split(/ \/ /,$nsong);
print "nsong: $nsong -- artist: $nartist -- Album: $nalbum\n";
#sleep 100;
`mkdir -p '$nartist/$nyear - $nalbum'`; # or die "Couldn't mkdir $!";


foreach my $key (sort keys %tracks) {
	#warn "CDP cdparanoia -v -d /dev/cdrom \"$key\" - |lame - '$ttitle.mp3'";
	# cdparanoia -Z will disable any correction-attempts. Handy for some
	# burned cd's.
	# -v will be absurdly verbous
	my $output_file = shell_quote("$nartist/$nyear - $nalbum/$key - $tracks{$key}.mp3");
	print "===============================\n";
	print "$output_file\n";
	print "===============================\n";
	`cdparanoia -d /dev/cdrom "$key" - |lame - $output_file`;
}

exit;

