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
Line 
1#!/usr/bin/perl
2
3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
5
6use strict;
7use warnings;
8
9use Net::CUPS;
10use Net::CUPS::Destination;
11use Getopt::Long;
12use Image::ExifTool qw(ImageInfo);
13
14# the usage for this script
15my $usage = <<USAGE;
16Usage: gbr [options] [-q QUEUE] FILES
17
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
24my $q = "";
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}
38
39my @files = @ARGV[0 .. $#ARGV];
40
41# if the -q option is not specified, then assume we're using the
42# default queue
43if (!$q){
44    $q = "DEFAULT";
45}
46
47# if there are no files specified to print, then show the usage,
48# because the user is Doing It Wrong
49if (!@files) {
50    print $usage;
51    exit 1
52}
53
54# set configuration path, and complain if it doesn't exist
55my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
56if (! -e $configpath) {
57    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
58    exit 1;
59}
60
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
64my ($host, $queue);
65
66# load the configuration file (this will set $host and $queue)
67if (-r $configpath) {
68    local $/;
69    my $fh;
70    open $fh, $configpath;
71    eval <$fh>;
72}
73
74# initialize a new CUPS session
75my $cups = Net::CUPS->new();
76# set the server to the one specified in the config file
77$cups->setServer("$host");
78# set the printer name to the one specified in the config file
79my $printer = $cups->getDestination("$queue");
80
81# if $printer is not defined, then throw an error
82unless( $printer){
83    print "Cannot access queue $q... do you have network connectivity and permission to view the queue?\n";
84    exit 1;
85}
86
87# initialize the job id and title variables for use below
88my ($jobid, $title);
89
90# for each file that the user wants to print
91foreach my $file(@files) {
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)
98    if ($file =~ m|http://www\.youtube\.com/watch\?v=|) {
99        open FILE, ">", "/tmp/gutenbach-youtube" or die "Couldn't create temporary file";
100        print FILE $file;
101        $title = $file;
102        $file = "/tmp/gutenbach-youtube";
103        $printer->addOption("copies", 42);
104    }
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.
110    else {
111        my $fileinfo = ImageInfo($file);
112        my $magic = $fileinfo->{FileType};
113
114        if ($magic && exists($fileinfo->{Title}) && exists($fileinfo->{Artist}) && exists($fileinfo->{Album})) {
115            $title = $fileinfo->{'Title'}." - ".$fileinfo->{'Artist'}." - ".$fileinfo->{'Album'};
116        }
117        else {
118            $title = $file;
119        }
120    }
121
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);
126
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    }   
142}
Note: See TracBrowser for help on using the repository browser.