| 1 | #!/usr/bin/perl |
|---|
| 2 | |
|---|
| 3 | # This script was largely written by Jessica Hamrick (jhamrick), with |
|---|
| 4 | # help from Kyle Brogle (broglek) |
|---|
| 5 | |
|---|
| 6 | use strict; |
|---|
| 7 | use warnings; |
|---|
| 8 | use Getopt::Long; |
|---|
| 9 | |
|---|
| 10 | # the usage for the program |
|---|
| 11 | my $usage = <<USAGE; |
|---|
| 12 | Usage: 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 | -e, --help Print this message |
|---|
| 20 | USAGE |
|---|
| 21 | |
|---|
| 22 | # initialize the variables that will hold the arguments |
|---|
| 23 | my $list = 0; |
|---|
| 24 | my $add = ""; |
|---|
| 25 | my $delete = ""; |
|---|
| 26 | my $host = ""; |
|---|
| 27 | my $default = ""; |
|---|
| 28 | my $help = 0; |
|---|
| 29 | |
|---|
| 30 | # get the options from the command line |
|---|
| 31 | GetOptions ('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 | 'e|help' => \$help); |
|---|
| 37 | |
|---|
| 38 | # if the -H flag was passed, then print the usage and exit |
|---|
| 39 | if ($help) { |
|---|
| 40 | print $usage; |
|---|
| 41 | exit 0; |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | # set the path where the configuration files live |
|---|
| 45 | my $configpath = "$ENV{'HOME'}/.gutenbach"; |
|---|
| 46 | |
|---|
| 47 | # if the configuration path doens't exist, then make it |
|---|
| 48 | if (! -e $configpath) { |
|---|
| 49 | mkdir "$configpath"; |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | # if the 'default' option was specified, then set given queue to |
|---|
| 53 | # default |
|---|
| 54 | if($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 |
|---|
| 85 | elsif ($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 |
|---|
| 107 | elsif (!$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 |
|---|
| 131 | elsif (!$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 |
|---|
| 144 | else { |
|---|
| 145 | print $usage; |
|---|
| 146 | exit 1; |
|---|
| 147 | } |
|---|