source: client/bin/gbrm @ 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.1 KB
RevLine 
[2a2f76c]1#!/usr/bin/perl
2
[6b7441a]3# This script was largely written by Jessica Hamrick (jhamrick), with
4# help from Kyle Brogle (broglek)
[2a2f76c]5
6use strict;
7use warnings;
8
[a81397c]9use Net::CUPS;
10use Net::CUPS::Destination;
[7cdd65d]11use Getopt::Long;
[a81397c]12
[fc8707b]13# usage
14my $usage = <<USAGE;
15Usage: gbq [options] [-q QUEUE] ID
16
17        -q, --queue             Specify a queue other than the default
18        -h, --help              Print this message
19USAGE
20
21# initialize the variables for the options
[7cdd65d]22my $q = "";
[fc8707b]23my $help = 0;
24
25# parse the options
26GetOptions ('q|queue=s' => \$q,
27            'h|help', => \$help);
28
29# if the -h flag was passed, then print the usage and exit
30if ($help) {
31    print $usage;
32    exit 0;
33}
[2a2f76c]34
[7cdd65d]35my @ids = @ARGV[0 .. $#ARGV];
[2a2f76c]36
[6b7441a]37# if the -q option is not specified, then assume we're using the
38# default queue
[fc8707b]39if (!$q) {
[7cdd65d]40    $q = "DEFAULT";
41}
[6b7441a]42
43# if there are no ids specified to remove, then print the usage
[7cdd65d]44if (!@ids) {
[2a2f76c]45    print $usage;
46    exit 1
47}
48
[6b7441a]49# set configuration path, and complain if it doesn't exist
[2a2f76c]50my $configpath = "$ENV{'HOME'}/.gutenbach/$q";
[85a1ac1]51if (! -e $configpath) {
[0d02eca]52    print "Queue '$q' does not exist!  Did you forget to add it with 'gutenbach-client-config'?\n";
[85a1ac1]53    exit 1;
54}
55
[6b7441a]56# initialize the host and queue variables: host holds the address for
57# the machine on which the remote queue runs, and queue holds the name
58# of the printer
[2a2f76c]59my ($host, $queue);
60
[6b7441a]61# load the configuration file (this will set $host and $queue)
[2a2f76c]62if (-r $configpath) {
63    local $/;
64    my $fh;
65    open $fh, $configpath;
66    eval <$fh>;
67}
68
[6b7441a]69# initialize a new CUPS session
[b58aada]70my $cups = Net::CUPS->new();
[6b7441a]71# set the server to the one specified in the config file
[b58aada]72$cups->setServer("$host");
[6b7441a]73# set the printer name to the one specified in the config file
[b58aada]74my $printer = $cups->getDestination("$queue");
[6b7441a]75
76# if $printer is not defined, then throw an error
[9fdf4a1]77unless( $printer){
78    print "Cannot access queue $q...do you have network connectivity and permission to view the queue?\n";
79    exit 1;
80}
[6b7441a]81
82# get the list of jobs from the printer
[b58aada]83my @jobs = $printer->getJobs(0, 0);
[6b7441a]84
85# for each id that we want to remove
[335786f]86foreach my $id(@ids){
[6b7441a]87
88    # if the id is 'all', then we remove all jobs
89    if ($id eq "all") {
90        foreach $id(@jobs) {
91            cancel_job($id, $printer);
92        }
93    }
94
95    # if the id is 'current', then we remove just the currently
96    # playing job
97    elsif ($id eq "current") {
98        $id = $jobs[0];
[d768767]99        cancel_job($id, $printer);
[b58aada]100    }
[6b7441a]101
102    # if the id is 'last', then we remove just the last job
103    elsif ($id eq "last") {
104        $id = $jobs[-1];
105        cancel_job($id, $printer);
106    }
107
108    # otherwise, remove the job based on its actual (numeric) id
109    else {
110        foreach my $item(@jobs) {
111            if($item =~ /$id/){
112                cancel_job($item, $printer);
113            }
[335786f]114        }
115    }
116}
[d768767]117
[6b7441a]118# helper function to remove a job
[d768767]119sub cancel_job {
[6b7441a]120    # get the id and printer from the arguments
[d768767]121    my ($id, $printer) = @_;
[6b7441a]122    # get the reference to the job
[d768767]123    my $job_ref = $printer->getJob($id);
[6b7441a]124    # find the job title (so we can print it out for the user)
[d768767]125    my $title = $job_ref->{'title'};
[6b7441a]126    # cancel the job
[b58aada]127    $printer->cancelJob($id);
[d768767]128
[6b7441a]129    # print out that we canceled the job
[d768767]130    print "Canceled job '$title' (id $id)\n";
[2a2f76c]131}
Note: See TracBrowser for help on using the repository browser.