[3b403dc] | 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 | # list the existing queues |
---|
| 30 | if ($list and !$add and !$delete) { |
---|
| 31 | my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'"; |
---|
| 32 | |
---|
| 33 | print "Queue\t\tHost\n"; |
---|
| 34 | foreach my $q (@queues) { |
---|
| 35 | my ($host, $queue); |
---|
| 36 | |
---|
| 37 | if (-r $q) { |
---|
| 38 | local $/; |
---|
| 39 | my $fh; |
---|
| 40 | open $fh, $q; |
---|
| 41 | eval <$fh>; |
---|
| 42 | } |
---|
| 43 | |
---|
| 44 | print "$queue\t\t$host\n"; |
---|
| 45 | } |
---|
| 46 | } |
---|
| 47 | |
---|
| 48 | # add a new queue |
---|
| 49 | elsif (!$list and $add and !$delete) { |
---|
| 50 | if (!$host) { |
---|
| 51 | print $usage; |
---|
| 52 | exit 1; |
---|
| 53 | } |
---|
| 54 | |
---|
| 55 | if (-e "$configpath/$add") { |
---|
| 56 | print "Warning: queue '$add' already exists\n"; |
---|
| 57 | } |
---|
| 58 | |
---|
| 59 | open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'"; |
---|
| 60 | print CONFIG "\$host = \"$host\";\n"; |
---|
| 61 | print CONFIG "\$queue = \"$add\";\n"; |
---|
| 62 | close CONFIG; |
---|
| 63 | |
---|
| 64 | print "Added queue '$add' on host '$host'\n" |
---|
| 65 | } |
---|
| 66 | |
---|
| 67 | # delete an existing queue |
---|
| 68 | elsif (!$list and !$add and $delete) { |
---|
| 69 | if (! -e "$configpath/$delete") { |
---|
| 70 | print "Error: queue '$delete' already exists\n"; |
---|
| 71 | exit 1; |
---|
| 72 | } |
---|
| 73 | |
---|
| 74 | unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'"; |
---|
| 75 | } |
---|
| 76 | |
---|
| 77 | else { |
---|
| 78 | print $usage; |
---|
| 79 | exit 1; |
---|
| 80 | } |
---|