Ignore:
File:
1 edited

Legend:

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

    r7cdd65d rfc8707b  
    11#!/usr/bin/perl
    22
    3 # Written by Jessica Hamrick, (C) 2010
     3# This script was largely written by Jessica Hamrick (jhamrick), with
     4# help from Kyle Brogle (broglek)
    45
    56use strict;
     
    78use Getopt::Long;
    89
    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";
     10# the usage for the program
     11my $usage = <<USAGE;
     12Usage: gutenbach-client-config [options]
    1613
     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
     20USAGE
     21
     22# initialize the variables that will hold the arguments
    1723my $list = 0;
    1824my $add = "";
     
    2026my $host = "";
    2127my $default = "";
     28my $help = 0;
    2229
     30# get the options from the command line
    2331GetOptions ('l|list' => \$list,
    2432            's|set-default=s' => \$default,
    2533            'a|add=s' => \$add,
    2634            'd|delete=s' => \$delete,
    27             'h|host=s' => \$host);
     35            'h|host=s' => \$host,
     36            'H|help' => \$help);
    2837
     38# if the -H flag was passed, then print the usage and exit
     39if ($help) {
     40    print $usage;
     41    exit 0;
     42}
     43
     44# set the path where the configuration files live
    2945my $configpath = "$ENV{'HOME'}/.gutenbach";
    3046
     47# if the configuration path doens't exist, then make it
    3148if (! -e $configpath) {
    3249    mkdir "$configpath";
    3350}
    34 #set given queue to default
     51
     52# if the 'default' option was specified, then set given queue to
     53# default
    3554if($default and !$add and !$delete and !$list) {
     55    # if the specified queue doesn't exist, then throw an error
    3656    unless(-e "$configpath/$default") {
    3757        print "Error: queue '$default' doesn't exist yet...you should add it first.\n";
    3858        exit 1;
    3959    }
     60   
     61    # if there already exists a default, then remove it so we can
     62    # replace it with the new default
    4063    if( -e "$configpath/DEFAULT"){
    4164        unlink("$configpath/DEFAULT") or die "Couldn't remove config file '$configpath/DEFAULT'";
    4265    }
     66
     67    # check to make sure we can create symlinks
    4368    my $symlink_exists = eval { symlink("",""); 1 };
     69
     70    # if so, then create the symlink and report it
    4471    if ($symlink_exists){
    4572        symlink("$configpath/$default","$configpath/DEFAULT");
    4673        print "Changed default queue to $default.\n";
    4774    }
     75
     76    # otherwise, throw an error
    4877    else
    4978    {
     
    5281    }
    5382}
    54    
    55        
    5683
    57 # list the existing queues
     84# if the 'list' option was specified, then list the existing queues
    5885elsif ($list and !$add and !$delete and !$default) {
     86    # get the config files in the configuration path -- these are the queues
    5987    my @queues = glob("$configpath/*") or die "Couldn't find configuration files at '$configpath'";
    6088
     89    # for each of the queues, load the configuration file and print
     90    # the queue name and the host it's on
    6191    print "Queue\t\tHost\n";
    6292    foreach my $q (@queues) {
     
    74104}
    75105
    76 # add a new queue
     106# if the 'add' option was specified, then add a new queue
    77107elsif (!$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)
    78111    if (!$host) {
    79112        print $usage;
     
    81114    }
    82115
     116    # if the queue already exists, then print a warning
    83117    if (-e "$configpath/$add") {
    84118        print "Warning: queue '$add' already exists\n";
    85119    }
    86120
     121    # create the configuration file
    87122    open CONFIG, "> $configpath/$add" or die "Couldn't open config file '$configpath/$add'";
    88123    print CONFIG "\$host = \"$host\";\n";
     
    93128}
    94129
    95 # delete an existing queue
     130# if the 'delete' option was specified, then delete an existing queue
    96131elsif (!$list and !$add and $delete) {
     132
     133    # if the queue doesn't exist, then print an error and exit
    97134    if (! -e "$configpath/$delete") {
    98         print "Error: queue '$delete' already exists\n";
     135        print "Error: queue '$delete' does not exist\n";
    99136        exit 1;
    100137    }
    101138
     139    # otherwise, remove the configuration file
    102140    unlink("$configpath/$delete") or die "Couldn't remove config file '$configpath/$delete'";
    103141}
    104142
     143# otherwise, it's an unrecognized option, so print the usage and exit
    105144else {
    106145    print $usage;
Note: See TracChangeset for help on using the changeset viewer.