#!/usr/bin/perl
#
#	$Id: cache2pdf.pl,v 1.6 2004/05/11 02:32:21 kevin Exp $
#
#	Author: Kevin Walsh <kevin@cursor.biz>
#
#	Copyright (c) 2004 Cursor Software Limited.
#	All rights reserved.
#
#	----------------------------------------------------------------------
#
#	SlimServer PDF music catalogue creator.
#
#	This script will read your SlimServer music cache file and create
#	a basic catalogue in PDF format.
#
#	You need to have the "PDF::Create" and "Storable" installed before
#	this script will run.  You may also need to change the value of the
#	$dbname and $pdfname variables, below.
#
#	----------------------------------------------------------------------
#
#	This program is free software; you can redistribute it and/or modify
#	it under the terms of 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.
#
#	This program is distributed in the hope that it will be useful,
#	but WITHOUT ANY WARRANTY; without even the implied warranty of
#	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#	GNU General Public License for more details.
#
#	You should have received a copy of the GNU General Public License
#	along with this program; if not, write to the Free Software
#	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
#	02111-1307 USA
#
use PDF::Create;
use Storable;
use strict;

my $version = substr(q$Revision: 1.6 $,10);
my $pname = $0;
$pname =~ s:^.*/::g;

#
#	set the $dbname to the name of the SlimServer's music cache file
#	set the $pdfname to the name of the PDF to create or overwrite
#
my $dbname = '/src/cvs/slim/server/Cache/.slimserver.db';
my $pdfname = '/home/kevin/slimserver_index.pdf';

#
#	page size (in pixels)
#
my $page_x = 612;
my $page_y = 792;

#
#	various indices into the tag info array
#
my $index_title = 1;
my $index_track = 5;
my $index_artist = 9;
my $index_album = 10;

unless (-r $dbname) {
    print qq{$pname: Cannot read "$dbname"\n};
    exit 255;
}

#
#	read the SlimServer music cache into memory
#
my $db = retrieve($dbname);

#
#	loop through the album names listed in the cache and associate
#	each track with its album
#
#	TODO: we don't handle the case where multiple albums have the
#	same name but different artists (i.e. "The Greatest Hits").
#
my %albums;
foreach (values %{$db->{infoCache}}) {
    my $ref = \$albums{$_->[$index_album]};
    $$ref ||= [];
    push(@$$ref,$_);
}

#
#	create a new PDF file
#
my $pdf = new PDF::Create(
    filename    => $pdfname,
    Version     => 1.2,
    PageMode    => 'UseOutlines',
    Author      => 'Kevin Walsh (Cursor Software)',
    Title       => 'SlimServer music catalogue',
    Subject     => 'SlimServer music catalogue',
    Keywords    => 'Music, Jukebox, SlimServer',
    Creator     => "$pname version $version",
);

#
#	start a new page to define the document
#	other pages will be children of this page
#
my $root = $pdf->new_page(
    'MediaBox'  => [ 0,0,$page_x,$page_y ],
);

#
#	define two typefaces to use within the body of the
#	document
#
my $f1 = $pdf->font(
    Subtype     => 'Type1',
    Encoding    => 'WinAnsiEncoding',
    BaseFont    => 'Helvetica-Bold',
);

my $f2 = $pdf->font(
    Subtype     => 'Type1',
    Encoding    => 'WinAnsiEncoding',
    BaseFont    => 'Helvetica',
);

#
#	loop through the album names, in name order
#
#	note that album names such as "The Best of..." will be sorted next
#	to other album names starting with "B" (for "Best"), rather than
#	with album names starting with "T" (for "The")
#
foreach my $album (sort keys %{$db->{caseCache}}) {
    $album = $db->{caseCache}->{$album} if $album;
    next unless $album;

    my $tracks = $albums{$album};
    next unless (ref($tracks) eq 'ARRAY' && scalar(@$tracks));

    #
    #	create a new page and set up its entry in the table of contents
    #
    my $page = $root->new_page;
    $pdf->new_outline(
	Title => $album,
	Destination => $page,
    );

    #
    #	write the album name at the top of the page (centered)
    #
    $page->stringc($f1,20,$page_x / 2,$page_y - 40,$album);

    my $y = $page_y - 100;

    #
    #	loop through the track names, writing the details as we go
    #
    foreach (sort { $a->[$index_track] <=> $b->[$index_track] } @$tracks) {
	$page->string($f2,12,20,$y,sprintf(
	    "%02d. %s  (%s)",
	    $_->[$index_track],
	    $_->[$index_title],
	    $_->[$index_artist],
	));
	$y -= 20;
    }
}

#
#	close the PDF file
#
$pdf->close();

#
#	that's all folks!
#
exit 0;

