#!/usr/bin/perl                                                                                                       

use Net::CUPS;
use Net::CUPS::Destination;
use Image::ExifTool qw(ImageInfo);

use strict;
use warnings;

use vars qw/$queue/;
require "/usr/lib/gutenbach/config/gutenbach-filter-config.pl" or die "Unable to load configuration";

my $cups = Net::CUPS->new();
my $printer = $cups->getDestination("$queue");
my @jobs = $printer->getJobs( 0, 0 );
my $job_ref;
my $jobid;
my $attr;

my $jobnum = 0;
foreach $jobid(@jobs)
{
    $job_ref = $printer->getJob($jobid);
    my $filepath = "/var/spool/cups/d0$job_ref->{'id'}-001";
    my $fileinfo = ImageInfo($filepath);
    my $magic = $fileinfo->{FileType};
    
    if ($jobnum == 0)
    {
	print $job_ref->{'user'}." is currently playing:\n";
	
	if ($magic)
	{
	    print "\t".$magic." file ".$job_ref->{'title'}."\n";
	    foreach my $key (qw/Title Artist Album AlbumArtist/)
	    {
		if (exists $fileinfo->{$key})
		{
		    print "\t$fileinfo->{$key}\n";
		}
	    }
	}
	else
	{
	    print "\t".$job_ref->{'title'}."\n";
	}
	
	print "\nComing up the queue:\n\n";
    }
    else
    {
	if ($magic)
	{
	    my $user = $job_ref->{'user'};
	    my $title = $fileinfo->{'Title'};
	    my $artist = $fileinfo->{'Artist'};
	    my $album = $fileinfo->{'Album'};
	    print "$user: \"$title\" by \"$artist\" on \"$album\"\n";
	}
	else
	{
	    my $user = $job_ref->{'user'};
	    my $file = $job_ref->{'title'};
	    print "$user: $file\n";
	}
    }
    
    $jobnum += 1;
}
