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 = <<USAGE; |
---|
10 | Usage: gutenbach-client-config [-l|--list|-a|--add|-d|--delete] [QUEUE] [--host=HOST] |
---|
11 | |
---|
12 | -l, --list List available queues |
---|
13 | -a, --add QUEUE Add a queue (must be used with -h) |
---|
14 | -d, --delete QUEUE Delete a queue |
---|
15 | -s, --set-default QUEUE Set the default queue |
---|
16 | -h, --host HOST Hostname for the queue |
---|
17 | USAGE |
---|
18 | |
---|
19 | my $list = 0; |
---|
20 | my $add = ""; |
---|
21 | my $delete = ""; |
---|
22 | my $host = ""; |
---|
23 | my $default = ""; |
---|
24 | |
---|
25 | GetOptions ('l|list' => \$list, |
---|
26 | 's|set-default=s' => \$default, |
---|
27 | 'a|add=s' => \$add, |
---|
28 | 'd|delete=s' => \$delete, |
---|
29 | 'h|host=s' => \$host); |
---|
30 | |
---|
31 | my $configpath = "$ENV{'HOME'}/.gutenbach"; |
---|
32 | |
---|
33 | if (! -e $configpath) { |
---|
34 | mkdir "$configpath"; |
---|
35 | } |
---|
36 | #set given queue to default |
---|
37 | if($default and !$add and !$delete and !$list) { |
---|
38 | unless(-e "$configpath/$default") { |
---|
39 | print "Error: queue '$default' doesn't exist yet...you should add it first.\n"; |
---|
40 | exit 1; |
---|
41 | } |
---|
42 | if( -e "$configpath/DEFAULT"){ |
---|
43 | unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'"; |
---|
44 | } |
---|
45 | my $symlink_exists = eval { symlink("",""); 1 }; |
---|
46 | if ($symlink_exists){ |
---|
47 | symlink("$configpath/$default","$configpath/DEFAULT"); |
---|
48 | print "Changed default queue to $default.\n"; |
---|
49 | } |
---|
50 | else |
---|
51 | { |
---|
52 | print "Error creating symlink to $configpath/$default"; |
---|
53 | exit 1; |
---|
54 | } |
---|
55 | } |
---|
56 | |
---|
57 | |
---|
58 | |
---|
59 | # list the existing queues |
---|
60 | elsif ($list and !$add and !$delete and !$default) { |
---|
61 | my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'"; |
---|
62 | |
---|
63 | print "Queue\t\tHost\n"; |
---|
64 | foreach my $q (@queues) { |
---|
65 | my ($host, $queue); |
---|
66 | |
---|
67 | if (-r $q) { |
---|
68 | local $/; |
---|
69 | my $fh; |
---|
70 | open $fh, $q; |
---|
71 | eval <$fh>; |
---|
72 | } |
---|
73 | |
---|
74 | print "$queue\t\t$host\n"; |
---|
75 | } |
---|
76 | } |
---|
77 | |
---|
78 | # add a new queue |
---|
79 | elsif (!$list and $add and !$delete) { |
---|
80 | if (!$host) { |
---|
81 | print $usage; |
---|
82 | exit 1; |
---|
83 | } |
---|
84 | |
---|
85 | if (-e "$configpath/$add") { |
---|
86 | print "Warning: queue '$add' already exists\n"; |
---|
87 | } |
---|
88 | |
---|
89 | open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'"; |
---|
90 | print CONFIG "\$host = \"$host\";\n"; |
---|
91 | print CONFIG "\$queue = \"$add\";\n"; |
---|
92 | close CONFIG; |
---|
93 | |
---|
94 | print "Added queue '$add' on host '$host'\n" |
---|
95 | } |
---|
96 | |
---|
97 | # delete an existing queue |
---|
98 | elsif (!$list and !$add and $delete) { |
---|
99 | if (! -e "$configpath/$delete") { |
---|
100 | print "Error: queue '$delete' already exists\n"; |
---|
101 | exit 1; |
---|
102 | } |
---|
103 | |
---|
104 | unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'"; |
---|
105 | } |
---|
106 | |
---|
107 | else { |
---|
108 | print $usage; |
---|
109 | exit 1; |
---|
110 | } |
---|