Ignore:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • client/bin/gutenbach-client-config

    rfc8707b r7cdd65d  
    11#!/usr/bin/perl
    22
    3 # This script was largely written by Jessica Hamrick (jhamrick), with
    4 # help from Kyle Brogle (broglek)
     3# Written by Jessica Hamrick, (C) 2010
    54
    65use strict;
     
    87use Getopt::Long;
    98
    10 # the usage for the program
    11 my $usage = <<USAGE;
    12 Usage: gutenbach-client-config [options]
     9my $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";
    1316
    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         -H, --help              Print this message
    20 USAGE
    21 
    22 # initialize the variables that will hold the arguments
    2317my $list = 0;
    2418my $add = "";
     
    2620my $host = "";
    2721my $default = "";
    28 my $help = 0;
    2922
    30 # get the options from the command line
    3123GetOptions ('l|list' => \$list,
    3224            's|set-default=s' => \$default,
    3325            'a|add=s' => \$add,
    3426            'd|delete=s' => \$delete,
    35             'h|host=s' => \$host,
    36             'H|help' => \$help);
     27            'h|host=s' => \$host);
    3728
    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
    4529my $configpath = "$ENV{'HOME'}/.gutenbach";
    4630
    47 # if the configuration path doens't exist, then make it
    4831if (! -e $configpath) {
    4932    mkdir "$configpath";
    5033}
    51 
    52 # if the 'default' option was specified, then set given queue to
    53 # default
     34#set given queue to default
    5435if($default and !$add and !$delete and !$list) {
    55     # if the specified queue doesn't exist, then throw an error
    5636    unless(-e "$configpath/$default") {
    5737        print "Error: queue '$default' doesn't exist yet...you should add it first.\n";
    5838        exit 1;
    5939    }
    60    
    61     # if there already exists a default, then remove it so we can
    62     # replace it with the new default
    6340    if( -e "$configpath/DEFAULT"){
    6441        unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'";
    6542    }
    66 
    67     # check to make sure we can create symlinks
    6843    my $symlink_exists = eval { symlink("",""); 1 };
    69 
    70     # if so, then create the symlink and report it
    7144    if ($symlink_exists){
    7245        symlink("$configpath/$default","$configpath/DEFAULT");
    7346        print "Changed default queue to $default.\n";
    7447    }
    75 
    76     # otherwise, throw an error
    7748    else
    7849    {
     
    8152    }
    8253}
     54   
     55       
    8356
    84 # if the 'list' option was specified, then list the existing queues
     57# list the existing queues
    8558elsif ($list and !$add and !$delete and !$default) {
    86     # get the config files in the configuration path -- these are the queues
    8759    my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'";
    8860
    89     # for each of the queues, load the configuration file and print
    90     # the queue name and the host it's on
    9161    print "Queue\t\tHost\n";
    9262    foreach my $q (@queues) {
     
    10474}
    10575
    106 # if the 'add' option was specified, then add a new queue
     76# add a new queue
    10777elsif (!$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)
    11178    if (!$host) {
    11279        print $usage;
     
    11481    }
    11582
    116     # if the queue already exists, then print a warning
    11783    if (-e "$configpath/$add") {
    11884        print "Warning: queue '$add' already exists\n";
    11985    }
    12086
    121     # create the configuration file
    12287    open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'";
    12388    print CONFIG "\$host = \"$host\";\n";
     
    12893}
    12994
    130 # if the 'delete' option was specified, then delete an existing queue
     95# delete an existing queue
    13196elsif (!$list and !$add and $delete) {
    132 
    133     # if the queue doesn't exist, then print an error and exit
    13497    if (! -e "$configpath/$delete") {
    135         print "Error: queue '$delete' does not exist\n";
     98        print "Error: queue '$delete' already exists\n";
    13699        exit 1;
    137100    }
    138101
    139     # otherwise, remove the configuration file
    140102    unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'";
    141103}
    142104
    143 # otherwise, it's an unrecognized option, so print the usage and exit
    144105else {
    145106    print $usage;
Note: See TracChangeset for help on using the changeset viewer.