#!/usr/bin/perl -w

eval 'exec /usr/bin/perl -w -S $0 ${1+"$@"}'
    if 0; # not running under some shell

my $RCS_Id = '$Id: cddb2cue.pl,v 1.4 2006/06/21 19:39:45 jv Exp $ ';

# Author          : Johan Vromans
# Created On      : Sun Jul 13 17:37:17 2003
# Last Modified By: Johan Vromans
# Last Modified On: Wed Jun 21 21:39:43 2006
# Update Count    : 18
# Status          : Unknown, Use with caution!

################ Common stuff ################

use strict;

# Package name.
my $my_package = 'Sciurix';
# Program name and version.
my ($my_name, $my_version) = $RCS_Id =~ /: (.+).pl,v ([\d.]+)/;
# Tack '*' if it is not checked in into RCS.
$my_version .= '*' if length('$Locker:  $ ') > 12;

use FindBin;
use lib $FindBin::Bin;

################ Command line parameters ################

my $verbose = 0;		# more verbosity

# Development options (not shown with --help).
my $debug = 0;			# debugging
my $trace = 0;			# trace (show process)
my $test = 0;			# test mode.

# Process command line options.
app_options();

# Post-processing.
$trace |= ($debug || $test);

################ Presets ################

my $TMPDIR = $ENV{TMPDIR} || $ENV{TEMP} || '/usr/tmp';

################ The Process ################

use CDDB::File;
# use MP3Tools;

foreach my $cddbfile ( @ARGV ) {
    my $disc = new CDDB::File($cddbfile);

    my $cue = trackfile(undef, $disc->title, ".cue");
    open (my $cf, ">$cue");

    print $cf ("PERFORMER \"", $disc->artist, "\"\n");
    print $cf ("TITLE \"", $disc->title, "\"\n");
    print $cf ("FILE \"", trackfile(undef, $disc->title, ".mp3"), "\" WAVE\n");

    my $t = 1;
    my $off;
    foreach my $track ($disc->tracks) {

	printf $cf ("  TRACK %02d AUDIO\n", $t);
	print $cf ("    TITLE \"", $track->title, "\"\n");
	print $cf ("    PERFORMER \"", $disc->artist, "\"\n");
	$off = $track->offset unless defined $off;
	my $index = $track->offset - $off;
	printf $cf ("    INDEX 01 %02d", $index / 4500);
	$index %= 4500;
	printf $cf (":%02d",  $index / 75);
	$index %= 75;
	printf $cf (":%02d\n", $index);
	$t++;
    }
}

################ Copied from MP3Tools ################

use constant VFAT => 1;

# Sanitize a filename for use on disk.
sub fsanitize {
    local($_) = shift;
    tr{/}{;};
    s/_+;/;/g;
    s/;_*/;_/g;
    if ( VFAT ) {
	s/:/_/g;
	s/["\`?!|]//g;
	s/\.$//g;
    }
    $_;
}

# Generate 'preferred filename' for a track.
sub trackfile {
    my ($track, $title, $exttmpl) = @_;
    my $nn = "";
    if ( defined $track ) {
	$nn .= $track;
	$nn = "0".$nn if length($nn) < 2;
	$nn .= "_" . join("_", split(' ',$title));
    }
    else {
	$nn = join("_", split(' ',$title));
    }
    $nn .= $1 if $exttmpl && $exttmpl =~ /(\.[^.]+)$/;
    fsanitize($nn);
}

################ Command Line Options ################

use Getopt::Long 2.33;		# will enable help/version

sub app_options {

    GetOptions(ident	=> \&app_ident,
	       verbose	=> \$verbose,

	       # application specific options go here

	       # development options
	       test	=> \$test,
	       trace	=> \$trace,
	       debug	=> \$debug)
      or Getopt::Long::HelpMessage(2);
}

sub app_ident {
    print STDOUT ("This is $my_package [$my_name $my_version]\n");
}

__END__

=head1 NAME

cddb2cue - create cue sheet from cddb file

=head1 SYNOPSIS

cddb2cue [options] [file ...]

Options:

   --ident		show identification
   --help		brief help message
   --verbose		verbose information

=head1 OPTIONS

=over 8

=item B<--verbose>

More verbose information.

=item B<--version>

Print a version identification to standard output and exits.

=item B<--help>

Print a brief help message to standard output and exits.

=item B<--ident>

Prints a program identification.

=item I<file>

Input file(s).

=back

=head1 DESCRIPTION

B<This program> will read the given input file(s), which must be CDDB files, and
generates cue sheets from them.

The name of the cue sheet will be derived from the album title, with spaces
changed to underscores, and ".cue" added. The name of the associated MP3 file
will be assumed to be the same, with ".mp3" instead of .cue".

=head1 AUTHOR

Johan Vromans <jvromans@squirrel.nl>

=head1 COPYRIGHT

This programs is Copyright 2003, 2006 Squirrel Consultancy.

This program is free software; you can redistribute it and/or modify
it under the terms of the Perl Artistic License or the GNU General
Public License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.

=cut
