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