source: client/bin/gutenbach-client-config @ 6b7441a

debianmacno-cups
Last change on this file since 6b7441a was 6b7441a, checked in by Jessica B. Hamrick <jhamrick@…>, 14 years ago

Add comments to the client scripts.

  • Property mode set to 100755
File size: 3.8 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;
8use Getopt::Long;
9
10# the usage for the program
11my $usage = <<USAGE;
12Usage: gutenbach-client-config [-l|--list|-a|--add|-d|--delete] [QUEUE] [--host=HOST]
13
14        -l, --list              List available queues
15        -a, --add QUEUE         Add a queue (must be used with -h)
16        -d, --delete QUEUE      Delete a queue
17        -s, --set-default QUEUE Set the default queue
18        -h, --host HOST         Hostname for the queue
19USAGE
20
21# initialize the variables that will hold the arguments
22my $list = 0;
23my $add = "";
24my $delete = "";
25my $host = "";
26my $default = "";
27
28# get the options from the command line
29GetOptions ('l|list' => \$list,
30            's|set-default=s' => \$default,
31            'a|add=s' => \$add,
32            'd|delete=s' => \$delete,
33            'h|host=s' => \$host);
34
35# set the path where the configuration files live
36my $configpath = "$ENV{'HOME'}/.gutenbach";
37
38# if the configuration path doens't exist, then make it
39if (! -e $configpath) {
40    mkdir "$configpath";
41}
42
43# if the 'default' option was specified, then set given queue to
44# default
45if($default and !$add and !$delete and !$list) {
46    # if the specified queue doesn't exist, then throw an error
47    unless(-e "$configpath/$default") {
48        print "Error: queue '$default' doesn't exist yet...you should add it first.\n";
49        exit 1;
50    }
51   
52    # if there already exists a default, then remove it so we can
53    # replace it with the new default
54    if( -e "$configpath/DEFAULT"){
55        unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'";
56    }
57
58    # check to make sure we can create symlinks
59    my $symlink_exists = eval { symlink("",""); 1 };
60
61    # if so, then create the symlink and report it
62    if ($symlink_exists){
63        symlink("$configpath/$default","$configpath/DEFAULT");
64        print "Changed default queue to $default.\n";
65    }
66
67    # otherwise, throw an error
68    else
69    {
70        print "Error creating symlink to $configpath/$default";
71        exit 1;
72    }
73}
74
75# if the 'list' option was specified, then list the existing queues
76elsif ($list and !$add and !$delete and !$default) {
77    # get the config files in the configuration path -- these are the queues
78    my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'";
79
80    # for each of the queues, load the configuration file and print
81    # the queue name and the host it's on
82    print "Queue\t\tHost\n";
83    foreach my $q (@queues) {
84        my ($host, $queue);
85
86        if (-r $q) {
87            local $/;
88            my $fh;
89            open $fh, $q;
90            eval <$fh>;
91        }
92
93        print "$queue\t\t$host\n";
94    }
95}
96
97# if the 'add' option was specified, then add a new queue
98elsif (!$list and $add and !$delete) {
99
100    # make sure there was a host specified as well (otherwise, we
101    # won't know where to print to)
102    if (!$host) {
103        print $usage;
104        exit 1;
105    }
106
107    # if the queue already exists, then print a warning
108    if (-e "$configpath/$add") {
109        print "Warning: queue '$add' already exists\n";
110    }
111
112    # create the configuration file
113    open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'";
114    print CONFIG "\$host = \"$host\";\n";
115    print CONFIG "\$queue = \"$add\";\n";
116    close CONFIG;
117
118    print "Added queue '$add' on host '$host'\n"
119}
120
121# if the 'delete' option was specified, then delete an existing queue
122elsif (!$list and !$add and $delete) {
123
124    # if the queue doesn't exist, then print an error and exit
125    if (! -e "$configpath/$delete") {
126        print "Error: queue '$delete' does not exist\n";
127        exit 1;
128    }
129
130    # otherwise, remove the configuration file
131    unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'";
132}
133
134# otherwise, it's an unrecognized option, so print the usage and exit
135else {
136    print $usage;
137    exit 1;
138}
Note: See TracBrowser for help on using the repository browser.