#!/usr/ug/bin/perl     
#(uncomment and move to top for ugcs)
##!/usr/bin/perl         
#(uncomment and move to top for ofb)

#$command = "/ofb/bin/gsend ";           #for ofb
$command = "/usr/contrib/bin/gsend ";  #for ugcs

# gw, a gsend wrapper by Alan McConchie (almccon@ofb.net)
# version 0.21
#   One thing this wrapper does is allow short, convenient aliases for
# long, nasty directed categories.  This allows you to type the category 
# @u/pub/test instead of the full @ugcs.caltech.edu/pub/test.  If your 
# category starts with @, then we will look in the hash below to see if 
# it's an alias for a longer directed category.  If that key is not found
# in the hash, it is assumed that you typed a fully qualified category.

%alias = (
    '@o' => '@ofb.net',
    '@ofb' => '@ofb.net',
    '@u' => '@ugcs.caltech.edu',
    '@ugcs' => '@ugcs.caltech.edu',
    '@g' => '@gale.org',
    '@gale' => '@gale.org',
    '@s' => '@seattle.wa.us',
    '@sea' => '@seattle.wa.us',
    '@seattle' => '@seattle.wa.us',
);

#   This wrapper can also simulate a rooted directory tree structure for 
# gale categories.  To do things the normal, boring way, set $fun = 0.

$fun = 0;   # Don't worry, you can use -x, instead.

#   If you've set $fun (or used -x on the command line), you can pretend 
# you're in a happy little world where global public categories start with a 
# slash (ie: /pub/test).  (Note, you can continue to use slash-led 
# condiments--just double slash them, like so: /pub/test://test)
#   If you type a category that starts with @, then you must be referring 
# to a directed category (of the form @ofb.net/pub/test).  
#   If you type a category that does not begin with either / or @, then it 
# is assumed you are referring to a category under your "current working
# category" (not unlike a shell's current working directory).  This
# category is stored in the environment variable GALE_CURRENT.  Set and 
# change this variable as you would any other environment variable; this
# script does not try to modify it.  If this variable is not set, a 
# default (set in the code below) will be used.  Usually, you'll want this
# current working category to be your server's directed category.

#   If you've set $fun (or used -x), setting $condimenthelp spots categories 
# that it thinks were meant to be condiments, but that you forgot to prepend 
# with a double slash.  If the cat doesn't include a dot or a slash (after the
# initial slash is stripped) then the leading slash is replaced.  Still, you
# shouldn't rely on this assistance, because it's not smart enough to catch
# condiments that are supposed to include a slash, such as "/fwd/slashdot".
# In this case, the leading slash would still be stripped, and $condimenthelp
# couldn't help you.  You'd end up puffing to "fwd/slashdot" and being 
# laughed at.  If you're using $fun, learn to double-slash condiments.

$condimenthelp = 1;

$v = 1;     # Unset to be less verbose;

use Getopt::Std;

getopts('c:C:s:S:apPx'); 

# Get the "current working category".  Doesn't even have to be a directed 
# cat, just anything you want prepended to categories that don't start with
# a / or @.  Put a default here, and you don't have to worry about setting
# your environment variable.  You'll probably want a trailing slash.
#   (If you're not using $fun (or -x) then don't worry about this)

$current = $ENV{GALE_CURRENT} || '@ofb.net/';

$fun++ if $opt_x;   # If we ask for fun on the command-line

$catmess = $opt_C || $opt_c;    # The category string

@cats = split(/:/,$catmess);    # The individual categories

foreach $cat (@cats) {
    if ($cat =~ /^@/) {
        ($direction, $category) = split('/', $cat, 2);
        if ($alias{$direction}){
            print "Expanding $direction to $alias{$direction}\n" if $v and not $i{$direction}++;
            $cat = "$alias{$direction}/$category" 
        }
    } elsif ($fun) {
        unless ($cat =~ s%^/%%) {   
            print "Using current working category: $current\n" if $v and not $i++;
            $cat = "$current$cat";
        }
        # No slash or dot?  Probably a condiment.
        unless ($cat =~ m%[/.]% && $condimenthelp) {
            print "Rescuing possible condiment: /$cat\n" if $v;
            $cat = "/$cat" 
        }
    }
    push @catsout, $cat;
}

# Recreate the options for gsend.

$command .= "-a " if $opt_a;
$command .= "-p " if $opt_p;
$command .= "-P " if $opt_P;
$command .= "-s $opt_s" if $opt_s;
$command .= "-S $opt_S" if $opt_S;
if ($opt_C) {
    $command .= "-C " . join(':', @catsout);
} else {
    $command .= "-c " . join(':', @catsout) if $opt_c;
    $command .= " " . join(' ', @ARGV);
}

exec $command;
