[3b403dc] | 1 | #!/usr/bin/perl |
---|
| 2 | |
---|
[6b7441a] | 3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
---|
| 4 | # help from Kyle Brogle (broglek) |
---|
[3b403dc] | 5 | |
---|
| 6 | use strict; |
---|
| 7 | use warnings; |
---|
| 8 | use Getopt::Long; |
---|
| 9 | |
---|
[6b7441a] | 10 | # the usage for the program |
---|
[814d4f8] | 11 | my $usage = <<USAGE; |
---|
[fc8707b] | 12 | Usage: gutenbach-client-config [options] |
---|
[814d4f8] | 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 |
---|
[fc8707b] | 18 | -h, --host HOST Hostname for the queue (must be used with -a) |
---|
[1c3f290] | 19 | -e, --help Print this message |
---|
[814d4f8] | 20 | USAGE |
---|
[3b403dc] | 21 | |
---|
[6b7441a] | 22 | # initialize the variables that will hold the arguments |
---|
[3b403dc] | 23 | my $list = 0; |
---|
| 24 | my $add = ""; |
---|
| 25 | my $delete = ""; |
---|
| 26 | my $host = ""; |
---|
[7cdd65d] | 27 | my $default = ""; |
---|
[fc8707b] | 28 | my $help = 0; |
---|
[3b403dc] | 29 | |
---|
[6b7441a] | 30 | # get the options from the command line |
---|
[3b403dc] | 31 | GetOptions ('l|list' => \$list, |
---|
[7cdd65d] | 32 | 's|set-default=s' => \$default, |
---|
[3b403dc] | 33 | 'a|add=s' => \$add, |
---|
| 34 | 'd|delete=s' => \$delete, |
---|
[fc8707b] | 35 | 'h|host=s' => \$host, |
---|
[1c3f290] | 36 | 'e|help' => \$help); |
---|
[fc8707b] | 37 | |
---|
| 38 | # if the -H flag was passed, then print the usage and exit |
---|
| 39 | if ($help) { |
---|
| 40 | print $usage; |
---|
| 41 | exit 0; |
---|
| 42 | } |
---|
[3b403dc] | 43 | |
---|
[6b7441a] | 44 | # set the path where the configuration files live |
---|
[3b403dc] | 45 | my $configpath = "$ENV{'HOME'}/.gutenbach"; |
---|
| 46 | |
---|
[6b7441a] | 47 | # if the configuration path doens't exist, then make it |
---|
[85a1ac1] | 48 | if (! -e $configpath) { |
---|
| 49 | mkdir "$configpath"; |
---|
| 50 | } |
---|
[6b7441a] | 51 | |
---|
| 52 | # if the 'default' option was specified, then set given queue to |
---|
| 53 | # default |
---|
[7cdd65d] | 54 | if($default and !$add and !$delete and !$list) { |
---|
[6b7441a] | 55 | # if the specified queue doesn't exist, then throw an error |
---|
[7cdd65d] | 56 | unless(-e "$configpath/$default") { |
---|
| 57 | print "Error: queue '$default' doesn't exist yet...you should add it first.\n"; |
---|
| 58 | exit 1; |
---|
| 59 | } |
---|
[6b7441a] | 60 | |
---|
| 61 | # if there already exists a default, then remove it so we can |
---|
| 62 | # replace it with the new default |
---|
[7cdd65d] | 63 | if( -e "$configpath/DEFAULT"){ |
---|
| 64 | unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'"; |
---|
| 65 | } |
---|
[6b7441a] | 66 | |
---|
| 67 | # check to make sure we can create symlinks |
---|
[7cdd65d] | 68 | my $symlink_exists = eval { symlink("",""); 1 }; |
---|
[6b7441a] | 69 | |
---|
| 70 | # if so, then create the symlink and report it |
---|
[7cdd65d] | 71 | if ($symlink_exists){ |
---|
| 72 | symlink("$configpath/$default","$configpath/DEFAULT"); |
---|
| 73 | print "Changed default queue to $default.\n"; |
---|
| 74 | } |
---|
[6b7441a] | 75 | |
---|
| 76 | # otherwise, throw an error |
---|
[7cdd65d] | 77 | else |
---|
| 78 | { |
---|
| 79 | print "Error creating symlink to $configpath/$default"; |
---|
| 80 | exit 1; |
---|
| 81 | } |
---|
| 82 | } |
---|
[85a1ac1] | 83 | |
---|
[6b7441a] | 84 | # if the 'list' option was specified, then list the existing queues |
---|
[7cdd65d] | 85 | elsif ($list and !$add and !$delete and !$default) { |
---|
[6b7441a] | 86 | # get the config files in the configuration path -- these are the queues |
---|
[3b403dc] | 87 | my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'"; |
---|
| 88 | |
---|
[6b7441a] | 89 | # for each of the queues, load the configuration file and print |
---|
| 90 | # the queue name and the host it's on |
---|
[3b403dc] | 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 | |
---|
[6b7441a] | 106 | # if the 'add' option was specified, then add a new queue |
---|
[3b403dc] | 107 | elsif (!$list and $add and !$delete) { |
---|
[6b7441a] | 108 | |
---|
| 109 | # make sure there was a host specified as well (otherwise, we |
---|
| 110 | # won't know where to print to) |
---|
[3b403dc] | 111 | if (!$host) { |
---|
| 112 | print $usage; |
---|
| 113 | exit 1; |
---|
| 114 | } |
---|
| 115 | |
---|
[6b7441a] | 116 | # if the queue already exists, then print a warning |
---|
[3b403dc] | 117 | if (-e "$configpath/$add") { |
---|
| 118 | print "Warning: queue '$add' already exists\n"; |
---|
| 119 | } |
---|
| 120 | |
---|
[6b7441a] | 121 | # create the configuration file |
---|
[3b403dc] | 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 | |
---|
[6b7441a] | 130 | # if the 'delete' option was specified, then delete an existing queue |
---|
[3b403dc] | 131 | elsif (!$list and !$add and $delete) { |
---|
[6b7441a] | 132 | |
---|
| 133 | # if the queue doesn't exist, then print an error and exit |
---|
[3b403dc] | 134 | if (! -e "$configpath/$delete") { |
---|
[6b7441a] | 135 | print "Error: queue '$delete' does not exist\n"; |
---|
[3b403dc] | 136 | exit 1; |
---|
| 137 | } |
---|
| 138 | |
---|
[6b7441a] | 139 | # otherwise, remove the configuration file |
---|
[3b403dc] | 140 | unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'"; |
---|
| 141 | } |
---|
| 142 | |
---|
[6b7441a] | 143 | # otherwise, it's an unrecognized option, so print the usage and exit |
---|
[3b403dc] | 144 | else { |
---|
| 145 | print $usage; |
---|
| 146 | exit 1; |
---|
| 147 | } |
---|