1 | #!/usr/bin/perl |
---|
2 | |
---|
3 | # Written by Jessica Hamrick, (C) 2010 |
---|
4 | |
---|
5 | use strict; |
---|
6 | use warnings; |
---|
7 | use Getopt::Long; |
---|
8 | |
---|
9 | my $usage = |
---|
10 | "Usage: gutenbach-client-config [-l|--list|-a|--add|-d|--delete] [QUEUE] [--host=HOST]\n" . |
---|
11 | "\n" . |
---|
12 | "\t-l, --list\t\tList available queues\n" . |
---|
13 | "\t-a, --add QUEUE\t\tAdd a queue (must be used with -h)\n" . |
---|
14 | "\t-d, --delete QUEUE\tDelete a queue)\n" . |
---|
15 | "\t-h, --host HOST\t\tHostname for the queue\n"; |
---|
16 | |
---|
17 | my $list = 0; |
---|
18 | my $add = ""; |
---|
19 | my $delete = ""; |
---|
20 | my $host = ""; |
---|
21 | |
---|
22 | GetOptions ('l|list' => \$list, |
---|
23 | 'a|add=s' => \$add, |
---|
24 | 'd|delete=s' => \$delete, |
---|
25 | 'h|host=s' => \$host); |
---|
26 | |
---|
27 | my $configpath = "$ENV{'HOME'}/.gutenbach"; |
---|
28 | |
---|
29 | if (! -e $configpath) { |
---|
30 | mkdir "$configpath"; |
---|
31 | } |
---|
32 | |
---|
33 | # list the existing queues |
---|
34 | if ($list and !$add and !$delete) { |
---|
35 | my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'"; |
---|
36 | |
---|
37 | print "Queue\t\tHost\n"; |
---|
38 | foreach my $q (@queues) { |
---|
39 | my ($host, $queue); |
---|
40 | |
---|
41 | if (-r $q) { |
---|
42 | local $/; |
---|
43 | my $fh; |
---|
44 | open $fh, $q; |
---|
45 | eval <$fh>; |
---|
46 | } |
---|
47 | |
---|
48 | print "$queue\t\t$host\n"; |
---|
49 | } |
---|
50 | } |
---|
51 | |
---|
52 | # add a new queue |
---|
53 | elsif (!$list and $add and !$delete) { |
---|
54 | if (!$host) { |
---|
55 | print $usage; |
---|
56 | exit 1; |
---|
57 | } |
---|
58 | |
---|
59 | if (-e "$configpath/$add") { |
---|
60 | print "Warning: queue '$add' already exists\n"; |
---|
61 | } |
---|
62 | |
---|
63 | open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'"; |
---|
64 | print CONFIG "\$host = \"$host\";\n"; |
---|
65 | print CONFIG "\$queue = \"$add\";\n"; |
---|
66 | close CONFIG; |
---|
67 | |
---|
68 | print "Added queue '$add' on host '$host'\n" |
---|
69 | } |
---|
70 | |
---|
71 | # delete an existing queue |
---|
72 | elsif (!$list and !$add and $delete) { |
---|
73 | if (! -e "$configpath/$delete") { |
---|
74 | print "Error: queue '$delete' already exists\n"; |
---|
75 | exit 1; |
---|
76 | } |
---|
77 | |
---|
78 | unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'"; |
---|
79 | } |
---|
80 | |
---|
81 | else { |
---|
82 | print $usage; |
---|
83 | exit 1; |
---|
84 | } |
---|