source: client/bin/gbr @ fc8707b

debianmacno-cups
Last change on this file since fc8707b was fc8707b, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago
Add '-hhelp' flags to all the client scripts
Add a '-ddryrun' option to gbr, to just list what would be done instead of doing it
  • Property mode set to 100755
File size: 3.9 KB
RevLine 
[fe74c7c]1#!/usr/bin/perl
2
[6b7441a]3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
[fe74c7c]5
6use strict;
7use warnings;
8
[b58aada]9use Net::CUPS;
10use Net::CUPS::Destination;
[7cdd65d]11use Getopt::Long;
[b58aada]12use Image::ExifTool qw(ImageInfo);
13
[fc8707b]14# the usage for this script
15my $usage = <<USAGE;
16Usage: gbr [options] [-q QUEUE] FILES
[fe74c7c]17
[fc8707b]18        -q, --queue             Specify a queue other than the default
19        -d, --dryrun            Just list what would be done
20        -h, --help              Print this message
21USAGE
22
23# initialize the variables for the options
[7cdd65d]24my $q = "";
[fc8707b]25my $dryrun = 0;
26my $help = 0;
27
28# parse the options
29GetOptions ('q|queue=s' => \$q,
30            'd|dryrun' => \$dryrun,
31            'h|help' => \$help);
32
33# if the -h flag was passed, then print the usage and exit
34if ($help) {
35    print $usage;
36    exit 0;
37}
[fe74c7c]38
[7cdd65d]39my @files = @ARGV[0 .. $#ARGV];
40
[6b7441a]41# if the -q option is not specified, then assume we're using the
42# default queue
[7cdd65d]43if (!$q){
44    $q = "DEFAULT";
45}
[6b7441a]46
47# if there are no files specified to print, then show the usage,
48# because the user is Doing It Wrong
[7cdd65d]49if (!@files) {
[fe74c7c]50    print $usage;
51    exit 1
52}
53
[6b7441a]54# set configuration path, and complain if it doesn't exist
[fe74c7c]55my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
[85a1ac1]56if (! -e $configpath) {
[0d02eca]57    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
[85a1ac1]58    exit 1;
59}
60
[6b7441a]61# initialize the host and queue variables: host holds the address for
62# the machine on which the remote queue runs, and queue holds the name
63# of the printer
[fe74c7c]64my ($host, $queue);
65
[6b7441a]66# load the configuration file (this will set $host and $queue)
[fe74c7c]67if (-r $configpath) {
68    local $/;
69    my $fh;
70    open $fh, $configpath;
71    eval <$fh>;
72}
73
[6b7441a]74# initialize a new CUPS session
[b58aada]75my $cups = Net::CUPS->new();
[6b7441a]76# set the server to the one specified in the config file
[b58aada]77$cups->setServer("$host");
[6b7441a]78# set the printer name to the one specified in the config file
[b58aada]79my $printer = $cups->getDestination("$queue");
[6b7441a]80
81# if $printer is not defined, then throw an error
[9fdf4a1]82unless( $printer){
[fc8707b]83    print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n";
[9fdf4a1]84    exit 1;
85}
[6b7441a]86
87# initialize the job id and title variables for use below
[b58aada]88my ($jobid, $title);
[c5a98db]89
[6b7441a]90# for each file that the user wants to print
[38388ac]91foreach my $file(@files) {
[6b7441a]92
93    # check to see if the file is a youtube video.  If it is, then
94    # write the URL to a temporary file, and set the number of copies
95    # on the print job to 42 (this is the dirty hack we have in place
96    # to indicate that the job is a youtube file instead of a normal
97    # file)
[38388ac]98    if ($file =~ m|http://www\.youtube\.com/watch\?v=|) {
[600e713]99        open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file";
[b58aada]100        print FILE $file;
101        $title = $file;
102        $file = "/tmp/gutenbach-youtube";
103        $printer->addOption("copies", 42);
[38388ac]104    }
[6b7441a]105
106    # otherwise, we assume it's a normal file.  Try to use exiftool's
107    # ImageInfo to find out the tag information about the file (i.e.,
108    # title, artist, and album).  If you can, then rename the job to
109    # reflect those tags.  Otherwise, keep the normal title.
[38388ac]110    else {
[b58aada]111        my $fileinfo = ImageInfo($file);
112        my $magic = $fileinfo->{FileType};
113
[9fdf4a1]114        if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) {
[b58aada]115            $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'};
116        }
117        else {
118            $title = $file;
[38388ac]119        }
120    }
121
[fc8707b]122    # unless it's a dry run, send the print job, given the file and
123    # the job title
124    unless ($dryrun) {
125        $jobid = $printer->printFile($file, $title);
[6b7441a]126
[fc8707b]127        # if the printFile command returned a job id, then print that out
128        # for the user to see
129        if ($jobid) {
130            print "Sent job '$title' (id $jobid)\n";
131        }
132
133        # otherwise, let them know that an error occurred
134        else {
135            print "Error sending job '$title'\n";
136        }
137
138    # otherwise, just print what we would do
139    } else {
140        print "Would send file '$file' with title '$title'\n";
141    }   
[c5a98db]142}
Note: See TracBrowser for help on using the repository browser.