#!/usr/bin/perl

# This script was largely written by Jessica Hamrick (jhamrick), with
# help from Kyle Brogle (broglek)

use strict;
use warnings;

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

# the usage for this script
my $usage = <<USAGE;
Usage: gbr [options] [-q QUEUE] FILES

        -q, --queue             Specify a queue other than the default
	-d, --dryrun		Just list what would be done
        -h, --help              Print this message
USAGE

# initialize the variables for the options
my $q = "";
my $dryrun = 0;
my $help = 0;

# parse the options
GetOptions ('q|queue=s' => \$q,
            'd|dryrun' => \$dryrun,
            'h|help' => \$help);

# if the -h flag was passed, then print the usage and exit
if ($help) {
    print $usage;
    exit 0;
}

my @files = @ARGV[0 .. $#ARGV];

# if the -q option is not specified, then assume we're using the
# default queue
if (!$q){
    $q = "DEFAULT";
}

# if there are no files specified to print, then show the usage,
# because the user is Doing It Wrong
if (!@files) {
    print $usage;
    exit 1
}

# set configuration path, and complain if it doesn't exist
my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
if (! -e $configpath) {
    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
    exit 1;
}

# initialize the host and queue variables: host holds the address for
# the machine on which the remote queue runs, and queue holds the name
# of the printer
my ($host, $queue);

# load the configuration file (this will set $host and $queue)
if (-r $configpath) {
    local $/;
    my $fh;
    open $fh, $configpath;
    eval <$fh>;
}

# initialize a new CUPS session
my $cups = Net::CUPS->new();
# set the server to the one specified in the config file
$cups->setServer("$host");
# set the printer name to the one specified in the config file
my $printer = $cups->getDestination("$queue");

# if $printer is not defined, then throw an error
unless( $printer){
    print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n";
    exit 1;
}

# initialize the job id and title variables for use below
my ($jobid, $title);

# for each file that the user wants to print
foreach my $file(@files) {

    # check to see if the file is a youtube video.  If it is, then
    # write the URL to a temporary file, and set the number of copies
    # on the print job to 42 (this is the dirty hack we have in place
    # to indicate that the job is a youtube file instead of a normal
    # file)
    if ($file =~ m|http://www\.youtube\.com/watch\?v=|) {
	open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file";
	print FILE $file;
	$title = $file;
	$file = "/tmp/gutenbach-youtube";
	$printer->addOption("copies", 42);
    }

    # otherwise, we assume it's a normal file.  Try to use exiftool's
    # ImageInfo to find out the tag information about the file (i.e.,
    # title, artist, and album).  If you can, then rename the job to
    # reflect those tags.  Otherwise, keep the normal title.
    else {
	my $fileinfo = ImageInfo($file);
	my $magic = $fileinfo->{FileType};

	if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) {
	    $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'};
	}
	else {
	    $title = $file;
	}
    }

    # unless it's a dry run, send the print job, given the file and
    # the job title
    unless ($dryrun) {
	$jobid = $printer->printFile($file, $title);

	# if the printFile command returned a job id, then print that out
	# for the user to see
	if ($jobid) {
	    print "Sent job '$title' (id $jobid)\n";
	}

	# otherwise, let them know that an error occurred
	else {
	    print "Error sending job '$title'\n";
	}

    # otherwise, just print what we would do
    } else {
	print "Would send file '$file' with title '$title'\n";
    }    
}
