#!/usr/bin/perl -w
#
# Reads CDDB info, allow modifications and send back to CDDB server
#
# $Id$
#
# Copyright B. Cornec 2009
# Provided under the GPL v2

# Syntax: see at end

use strict 'vars';
use Getopt::Long qw(:config auto_abbrev no_ignore_case);
use Data::Dumper;
use English;
use ProjectBuilder::Base;
use ProjectBuilder::Conf;
use Mail::Sendmail;
use POSIX  qw(strftime :sys_wait_h);
use Newt qw(NEWT_FLAG_WRAP NEWT_ENTRY_SCROLL NEWT_ANCHOR_LEFT NEWT_ANCHOR_RIGHT :entry :textbox :macros);
use lib qw (lib);
use locale;
# > 2.27
use CDDB_get qw( get_cddb get_discids );
use Encode;
#use Linux::CDROM;
use Device::Cdio::Device;
use Device::Cdio::Track;
use File::Copy;
use File::Basename;

# Global variables
my %opts;					# CLI Options
my $action;					# action to realize

my @date = pb_get_date();
my $pbdate = strftime("%Y-%m-%d", @date);

pb_temp_init();

=pod

=head1 NAME

CDDBeditor, read, modidy and send your CDDB contrinutions

=head1 DESCRIPTION

CDDBeditor helps you read, modidy and send your CDDB contrinutions.

=head1 SYNOPSIS

CDDBeditor [-vhmq][-t|-g]

CDDBeditor [--verbose][--help][--man][--quiet][--text|--graphic]

=head1 OPTIONS

=over 4

=item B<-v|--verbose>

Print a brief help message and exits.

=item B<-q|--quiet>

Do not print any output.

=item B<-h|--help>

Print a brief help message and exits.

=item B<--man>

Prints the manual page and exits.

=item B<-t|--text>
=item B<-g|--graphic>

Interface is either text mode or graphical mode (newt based)

=back 

=head1 ARGUMENTS

=head1 WEB SITES

The main Web site of the project is available at L<http://www.project-builder.org/>. Bug reports should be filled using the trac instance of the project at L<http://trac.project-builder.org/>.

=head1 USER MAILING LIST

None exists for the moment.

=head1 CONFIGURATION FILES

=head1 AUTHORS

The CDDBeditor team L<http://trac.project-builder.org/> lead by Bruno Cornec L<mailto:bruno@project-builder.org>.

=head1 COPYRIGHT

CDDBeditor is distributed under the GPL v2.0 license
described in the file C<COPYING> included with the distribution.

=cut

# ---------------------------------------------------------------------------

#Avoids an error from Newt
no AutoLoader;


# Initialize the syntax string

my $projver = "PBVER";
my $projrev = "PBREV";

pb_syntax_init("CDDBeditor Version $projver-$projrev\n");

GetOptions("help|?|h" => \$opts{'h'}, 
		"man" => \$opts{'man'},
		"verbose|v+" => \$opts{'v'},
		"quiet|q" => \$opts{'q'},
		"text|t" => \$opts{'t'},
		"graphic|g" => \$opts{'g'},
		"version|V=s" => \$opts{'V'},
) || pb_syntax(-1,0);

if (defined $opts{'h'}) {
	pb_syntax(0,1);
}
if (defined $opts{'man'}) {
	pb_syntax(0,2);
}
if (defined $opts{'v'}) {
	$pbdebug = $opts{'v'};
}
if (defined $opts{'q'}) {
	$pbdebug=-1;
}
pb_log_init($pbdebug, $pbLOG);


Newt::Init();

# String definitions
my $ce_title = "CDDBeditor";
my $ce_help = "(c) Bruno Cornec 2009 - All right reversed under the GPL v2";
my $artist_label = Newt::Label("Artist: ");
my $empty_label = Newt::Label("      ");
my $title_label = Newt::Label("Title: ");
my $year_label = Newt::Label("Year: ");
my $id_label = "Id: ";
my $rev_label = "Revision: ";
my $genre_label = Newt::Label("Genre: ");
my $category_label = Newt::Label("Category: ");
my $info_label = Newt::Label("Info: ");
my $tr_track_label = Newt::Label("Track");
my $tr_frames_label = Newt::Label("Frames");
my $tno_label = Newt::Label("Track #: ");
my $tr_title_label = Newt::Label("Title");
my $tr_author_label = Newt::Label("Author");
my $tr_length_label = Newt::Label("Length");

my $host_label = Newt::Label("Host: ");
my $port_label = Newt::Label("Port: ");
my $mode_label = Newt::Label("Mode: ");
my $dev_label = Newt::Label("Device: ");
my $proxy_label = Newt::Label("Proxy: ");

my $wait_label = Newt::Label("Please wait while searching ...");

# To be put in conf file
my %ce_config;
$ce_config{"CDDB_HOST"}="freedb.freedb.org";
$ce_config{"CDDB_USER"}="freedb-submit\@freedb.org";
$ce_config{"CDDB_CC"}="bruno\@victoria.frmug.org";
$ce_config{"CDDB_PORT"}=888;
#$ce_config{"CDDB_PORT"}=80;
my %ce_cddb_mode;
$ce_cddb_mode{"0"} = "cddb";
$ce_cddb_mode{"1"} = "http";

$ce_config{"CDDB_MODE"}=$ce_cddb_mode{"1"};
$ce_config{"CD_DEVICE"}="/dev/sr1";
$ce_config{"HTTP_PROXY"}=$ENV{http_proxy} if $ENV{http_proxy};
$ce_config{"input"}=0;
$ce_config{"multi"}=1;
$ce_config{"PROTO_VERSION"} = 6;
$ce_config{"DOMAIN"} = "musique-ancienne.org";
$ce_config{"USER"} = "bruno";
$ce_config{"HELLO_ID"} = "$ce_config{\"USER\"} $ce_config{\"DOMAIN\"} fastrip 0.77";

# Copied from CDDB.pm
# Determine whether we can submit changes by e-mail.

my ($ce_sl, $ce_sh) = Newt::GetScreenSize();

# First panel for configuration parameters
Newt::Cls();
Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
Newt::PushHelpLine($ce_help);
Newt::Refresh();

my $flag = NEWT_ENTRY_SCROLL;
# Need 23 char for track and time
my $width = $ce_sl - 23;
# Othe fields are limited to 5
my $width2 = 5;
my $host_entry = Newt::Entry($width, $flag, $ce_config{"CDDB_HOST"});
my $port_entry = Newt::Entry($width2, $flag, $ce_config{"CDDB_PORT"});
my $mode_group;
my $proxy_entry = undef;
$mode_group = Newt::HRadiogroup($ce_cddb_mode{"0"}, $ce_cddb_mode{"1"});
if ($ce_config{"CDDB_MODE"} eq "http" ) {
	$proxy_entry = Newt::Entry($width, $flag, $ce_config{"HTTP_PROXY"});
}
my $dev_entry = Newt::Entry($width, $flag, $ce_config{"CD_DEVICE"});
my $user_group;
my $quit_button = Newt::Button("Quit");
$quit_button->Tag("Quit");
my $next_button = Newt::Button("Next");
$next_button->Tag("Next");

my $ce_panel = Newt::Panel(2, 20, "CDDBeditor Configuration");
my $flage = NEWT_ANCHOR_RIGHT;
$ce_panel->Add(0, 0, $host_label, $flage);
$flage = NEWT_ANCHOR_LEFT;
$ce_panel->Add(1, 0, $host_entry, $flage);
$flage = NEWT_ANCHOR_RIGHT;
$ce_panel->Add(0, 1, $port_label, $flage);
$flage = NEWT_ANCHOR_LEFT;
$ce_panel->Add(1, 1, $port_entry, $flage);
$flage = NEWT_ANCHOR_RIGHT;
$ce_panel->Add(0, 2, $mode_label, $flage);
$flage = NEWT_ANCHOR_LEFT;
$ce_panel->Add(1, 2, $mode_group, $flage);
if ($ce_config{"CDDB_MODE"} eq "http" ) {
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 3, $proxy_label, $flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 3, $proxy_entry, $flage);
	}
$flage = NEWT_ANCHOR_RIGHT;
$ce_panel->Add(0, 4, $dev_label, $flage);
$flage = NEWT_ANCHOR_LEFT;
$ce_panel->Add(1, 4, $dev_entry, $flage);
$flage = NEWT_ANCHOR_LEFT;
$ce_panel->Add(0, 6, $empty_label);
$ce_panel->Add(0, 7, $next_button);
$ce_panel->Add(1, 7, $quit_button);

Newt::Refresh();
my ($reason, $data) = $ce_panel->Run();

Newt::Cls();
exit 0 if ($data->Tag() eq "Quit");

if ($ce_config{"CDDB_MODE"} eq "http" ) {
	$ce_config{"HTTP_PROXY"}=$proxy_entry->Get();
	}
$ce_config{"CDDB_HOST"}=$host_entry->Get();
$ce_config{"CDDB_PORT"}=$port_entry->Get();
$ce_config{"CDDB_MODE"}=$ce_cddb_mode{$mode_group->Get()};
$ce_config{"CD_DEVICE"}=$dev_entry->Get();

# Components
my $reload_button = Newt::Button("Reload");
$reload_button->Tag("Reload");
my $send_button = Newt::Button("Send");
$send_button->Tag("Send");

my $firsttime = 1;
my @cecd;
my $ce_cd;
my $artist_entry;
my $title_entry;
my $category_entry;
my $genre_entry;
my $tno_entry;
my $year_entry;
my @track_entry;

# Entering the loop
for (my $i=0 ; ; $i++) {
	# Second panel to make user wait
	Newt::Cls();
	Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
	Newt::PushHelpLine($ce_help);
	$ce_panel = Newt::Panel(3, 20, "CDDB Search");
	$ce_panel->Add(0, 0, $wait_label);

	($reason, $data) = $ce_panel->Draw();
	Newt::Refresh();

	# CDDB query if first time
	if ($firsttime == 1) {
		@cecd = get_cddb(\%ce_config);
		# by default use first one
		$ce_cd = $cecd[0] if (defined $cecd[0]);
	}
	$firsttime = 0;

	my @vradio;
	my $ind = 0;
	foreach my $entry (@cecd) {
		my $cat = "";
		my $tno = "";
		my $title = "";
		$cat = $entry->{cat} if (defined $entry->{cat});
		$tno = $entry->{tno} if (defined $entry->{tno});
		$title = $entry->{title} if (defined $entry->{title});
		my $chain = undef;
		$chain = sprintf("%02d (%s - %d tracks) - %s",$ind,$cat,$tno,$title) if ((defined $cat) && (defined $tno) && (defined $title));
		push @vradio,$chain if (defined $chain);
		$ind++;
	}

	if ($ind gt 1) {
		# We have multiple entries chose the right one
		# Second panel for configuration parameters
		Newt::Cls();
		Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
		Newt::PushHelpLine($ce_help);
		Newt::Refresh();

		my $user_group = Newt::VRadiogroup(@vradio);
		my $quit_button = Newt::Button("Quit");
		$quit_button->Tag("Quit");
		my $next_button = Newt::Button("Next");
		$next_button->Tag("Next");
		
		my $ce_panel = Newt::Panel(2, 20, "CD chooser");
		$flage = NEWT_ANCHOR_LEFT;
		$ce_panel->Add(0, 5, $user_group, $flage);
		$ce_panel->Add(0, 10, $empty_label);
		$ce_panel->Add(0, 11, $next_button);
		$ce_panel->Add(1, 11, $quit_button);
		
		Newt::Refresh();
		my ($reason, $data) = $ce_panel->Run();

		Newt::Cls();
		exit 0 if ($data->Tag() eq "Quit");
		
		$ind=$user_group->Get();
	}
	# Point now to the right CD
	$ce_cd = $cecd[$ind-1];

	if (not defined $ce_cd->{title}) {
		# Get the disc id first
		my $id = get_discids($ce_config{"CD_DEVICE"});
		$ce_cd->{id} = sprintf "%08x", $id->[0];

		# Try to find it locally
		my @d = glob("$ENV{'HOME'}/.cddb/*/$ce_cd->{id}");
		foreach my $d (@d) {
			$ce_cd->{cat} = basename(dirname($d));
			open(DISC,$d) || die "Unable to open $d";
			# Idea taken from CDDB_get
			while (<DISC>) {
				push @{$ce_cd->{raw}},$_;
				if (/^TTITLE(\d+)\=\s*(\S.*)/) {
					chomp($2);
					$ce_cd->{track}[$1]=$2;
				} elsif(/^DTITLE=\s*(\S.*)/) {
					my $t = $1;
					if ($t =~ /\//) {
						($ce_cd->{artist},$ce_cd->{title}) = split(/\s\/\s/,$t);
					} else {
						$ce_cd->{artist}=$t;
						$ce_cd->{title}=$t;
					}
					chomp($ce_cd->{title});
				} elsif(/^DYEAR=\s*(\d+)/) {
					$ce_cd->{year} = $1;
				} elsif(/^DGENRE=\s*(\S.*)/) {
					$ce_cd->{genre} = $1;
				} elsif(/^\#\s+Revision:\s+(\d+)/) {
					$ce_cd->{revision} = $1;
				}
			}
			close(DISC);
		}

		# Pre allocate from the physical media
		my $device = Device::Cdio::Device->new(-source=>$ce_config{"CD_DEVICE"});
		my $track  = $device->get_last_track();
		$ce_cd->{tno} = $track->{track};
		my $n = 0;

		# If they were not found
		$ce_cd->{artist} = "" if (not defined $ce_cd->{artist});
		$ce_cd->{title} = "" if (not defined $ce_cd->{title});
		$ce_cd->{genre} = "" if (not defined $ce_cd->{genre});
		$ce_cd->{cat} = "" if (not defined $ce_cd->{cat});
		$ce_cd->{year} = "" if (not defined $ce_cd->{year});
		$ce_cd->{revision} = 0 if (not defined $ce_cd->{revision});

		# Going one track further for frame computation.
		# Cf: http://www.gnu.org/software/libcdio/doxygen/track_8h.html
		while ( $n <= $ce_cd->{tno}) {
			$track->set_track($n+1);
			# Again only if not found previously
			$ce_cd->{track}[$n] = "" if (not defined $ce_cd->{track}[$n]);
			my ($m, $s, $f) = split(/:/,$track->get_msf());
			$ce_cd->{frames}[$n] = $f + 75*$s + 75*60*$m;
			$n++;
		}
	}
	# Modify enconding of some fields if proto < 5
	#$ce_cd->{artist} = decode("iso8859-1",$ce_cd->{artist});
	#$ce_cd->{title} = decode("iso8859-1",$ce_cd->{title});

	# Third panel to display CD Infos
	Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
	Newt::PushHelpLine($ce_help);

	# Components
	$flag = NEWT_ENTRY_SCROLL;
	$artist_entry = Newt::Entry($width, $flag, $ce_cd->{artist});
	$title_entry = Newt::Entry($width, $flag, $ce_cd->{title});
	# http://www.freedb.org/en/faq.3.html
	# Category list is data, folk, jazz, misc, rock, country, blues, newage, reggae, classical, and soundtrack
	$category_entry = Newt::Entry($width, $flag, $ce_cd->{cat});
	$genre_entry = Newt::Entry($width, $flag, $ce_cd->{genre});
	$tno_entry = Newt::Label($ce_cd->{tno}."   -   ".$id_label.$ce_cd->{id}."   -   ".$rev_label.$ce_cd->{revision});
	$year_entry = Newt::Entry($width2, $flag, $ce_cd->{year});
	
	# Build interface
	($ce_sl, $ce_sh) = Newt::GetScreenSize();
	$ce_panel = Newt::Panel(4, $ce_sh, "CDDB Info");
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 0, $artist_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 0, $artist_entry, $flage);
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 1, $title_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 1, $title_entry, $flage);
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 2, $year_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 2, $year_entry, $flage);
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 3, $category_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 3, $category_entry, $flage);
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 4, $genre_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 4, $genre_entry, $flage);
	$flage = NEWT_ANCHOR_RIGHT;
	$ce_panel->Add(0, 5, $tno_label,$flage);
	$flage = NEWT_ANCHOR_LEFT;
	$ce_panel->Add(1, 5, $tno_entry, $flage);

	my $n=0;
	while ( $n < $ce_cd->{tno} ) {
		last if ($n > $ce_sh - 9);
		my $from=$ce_cd->{frames}[$n];
		my $to=$ce_cd->{frames}[$n+1]-1;
		my $dur=$to-$from;
		my $min=int($dur/75/60);
		my $sec=int($dur/75)-$min*60;
		my $tr = sprintf "Track %2d:", $n+1;
		my $track_label = Newt::Label($tr);
		$tr = sprintf " %2d':%.2d", $min, $sec;
		# Adapt encoding of track if proto < 5
		#$ce_cd->{track}[$n] = decode("iso8859-1",$ce_cd->{track}[$n]);

		my $dur_label = Newt::Label($tr);
		$flag = NEWT_FLAG_WRAP|NEWT_ENTRY_SCROLL;
		$track_entry[$n] = Newt::Entry($width, $flag, $ce_cd->{track}[$n]);
		$ce_panel->Add(0, 9+$n, $track_label);
		$ce_panel->Add(1, 9+$n, $track_entry[$n], $flage);
		$ce_panel->Add(2, 9+$n, $dur_label, $flage);
		$n++;
	}
	$ce_panel->Add(0, 9+$n, $empty_label);

	$ce_panel->Add(0, 9+$n+1, $send_button);
	$ce_panel->Add(1, 9+$n+1, $reload_button);
	$ce_panel->Add(2, 9+$n+1, $quit_button);
	
	Newt::Cls();
	Newt::Refresh();

	# Build interface
	($reason, $data) = $ce_panel->Run();

	$ce_cd->{artist} = $artist_entry->Get();
	$ce_cd->{title} = $title_entry->Get();
	$ce_cd->{year} = $year_entry->Get();
	$ce_cd->{cat} = $category_entry->Get();
	$ce_cd->{genre} = $genre_entry->Get();
	$n=1;
  	while ( $n <= $ce_cd->{tno} ) {
		$ce_cd->{track}[$n-1] = $track_entry[$n-1]->Get();
		$n++;
	}
	Newt::Refresh();
	Newt::Cls();
	exit 0 if ($data->Tag() eq "Quit");

	# Send the data
	if ($data->Tag() eq "Send") {
		$flag = NEWT_FLAG_WRAP;
		my $height = 10;

		Newt::DrawRootText(ce_center_string($ce_title), 1, $ce_title);
		Newt::PushHelpLine($ce_help);

		my $txt = "Please wait while sending disc $ce_cd->{id} ($ce_cd->{cat}) to $ce_config{\"CDDB_USER\"}";
		my $info_tb = Newt::Textbox($width, $height, $flag, $txt);

		Newt::Cls();
		$ce_panel = Newt::Panel(3, $width, "CDDB Sending Infos");
		$ce_panel->Add(0, 0, $info_tb);
		$ce_panel->Add(0, 1, $quit_button);
		Newt::Refresh();
		# TODO: Mail::Sendmail ?
		ce_prepare_mail($ce_cd);
		pb_system("mutt -s \"cddb $ce_cd->{cat} $ce_cd->{id}\" -c $ce_config{\"CDDB_CC\"} $ce_config{\"CDDB_USER\"} < $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}", "quiet");
		($reason, $data) = $ce_panel->Run();

		# Force reloading info from cache
		$ce_cd->{title} = undef;
	}
	if ($data->Tag() eq "Reload") {
		# Then force CDDB query
		$firsttime = 1;
	}
}
Newt::Finished();
exit 0;

# return the char to start printing a centered string based on screen size
sub ce_center_string {

	my $string = shift || "";

	my $res = ($ce_sl / 2 ) - (length($string) / 2);
	if ($res < 0 ) {
		return 0;
	} else {
		return ($res);
	}
}

sub ce_prepare_mail() {

my $ce_cd = shift;

pb_mkdir_p("$ENV{'HOME'}/.cddb/$ce_cd->{cat}");
open(MAIL, "> $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}") || die "Unable to create $ENV{'HOME'}/.cddb/$ce_cd->{cat}/$ce_cd->{id}";
print MAIL "# xmcd CD database file\n#\n# Track frame offsets:\n";
my $n=0;
while ( $n < $ce_cd->{tno} ) {
	print MAIL "#       ".$ce_cd->{frames}[$n]."\n";
	$n++;
}
#my $from=int($ce_cd->{frames}[0]/75);
my $sec=int($ce_cd->{frames}[$ce_cd->{tno}]/75);
#my $sec=$to-$from;
my $rev = $ce_cd->{revision} + 1;
# Taken from CDDB.pm
print MAIL "#\n# Disc length: $sec seconds\n#\n";
print MAIL "# Revision: $rev\n";
print MAIL "# Processed by: cddbd v1.5PL3 Copyright (c) Steve Scherf et al.\n";
print MAIL "# Submitted via: CDDBEditor $projver-$projrev\n";
print MAIL "DISCID=$ce_cd->{id}\n";
print MAIL "DTITLE=$ce_cd->{artist} / $ce_cd->{title}\n";
print MAIL "DYEAR=$ce_cd->{year}\n";
print MAIL "DGENRE=$ce_cd->{genre}\n";
$n=0;
while ( $n < $ce_cd->{tno} ) {
	print MAIL "TTITLE$n=$ce_cd->{track}[$n]\n";
	$n++;
}
print MAIL "EXTD=\n";
$n=0;
while ( $n < $ce_cd->{tno} ) {
	print MAIL "EXTT$n=\n";
	$n++;
}
print MAIL "PLAYORDER=\n";
close(MAIL);
}

1;
