#!/usr/bin/perl

# ----------------------------------------------------------------------
# $Id: expand-alias,v 1.2 2002/09/24 10:59:58 dlc Exp $
# ----------------------------------------------------------------------
# expand-alias - expand an alias from /etc/aliases
# Copyright (C) 2002 darren chamberlain <darren@cpan.org>
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation; version 2.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
# 02111-1307  USA
# ----------------------------------------------------------------------

use strict;
use vars qw($VERSION $opt_h $opt_f $opt_t $opt_v
                     $opt_n $opt_c $opt_s
                     $joint);

use Getopt::Std;
use Mail::ExpandAliases;

getopts("tcnhf:sv");

if ($opt_h) {
    exec("perldoc $0");
    exit 0;
}

if (($opt_t + $opt_c + $opt_n + $opt_s) > 1) {
    die "Please specify only one of -t, -n, -c, or -s.";
}

$joint = $opt_t ? "\t" : $opt_c ? ", " : $opt_n ? "\n" : " ";

unless ($opt_f) {
    for my $possible (qw(/etc/aliases /etc/mail/aliases)) {
        if (-f $possible) {
            $opt_f = $possible;
            last;
        }
    }    
}

unless (-f $opt_f) {
    die "'$opt_f' does not exist or is not readable!";
}

my $expander = Mail::ExpandAliases->new($opt_f);

for (@ARGV) {
    my $alias = $opt_v ? "$_: " : "";
    if ($opt_s) {
        my (@expandos, $last, $comma, $num, $str);

        @expandos = $expander->expand($_);
        if (@expandos > 2) {
            my $last = pop @expandos;
            print $alias, join(", ", @expandos), ", and ", $last;
        } elsif (@expandos == 2) {
            print $alias, join " and ", @expandos;
        } else {
            print $alias, @expandos;
        }
    } else {
        print $alias, join $joint, $expander->expand($_);
    }
    print "\n";
}

exit(0);

__END__

=head1 NAME

expand-alias - expand mail aliases from /etc/aliases

=head1 SYNOPSIS

    $ expand-alias MAILER-DAEMON
    root

    $ expand-alias -c listname
    addr1, addr2, addr3

    $ expand-alias -n listname
    addr1
    addr2
    addr3

    $ expand-alias -t listname
    addr1	addr2	addr3

    $ expand-alias -f ~/my.aliases friends
    friend1@isp.net other.friend@isp2.net

    $ expand-alias -f ~/my.aliases -s friends
    friend1@isp.net and other.friend@isp2.net

=head1 DESCRIPTION

C<expand-alias> expands aliases from an aliases file, as implemented
by the Mail::ExpandAliases module.

=head1 USE

C<expand-alias> takes 0 or more aliases as arguments:

    $ expand-alias postmaster
    darren@cpan.org

    $ expand-alias foo
    foo

Note that unknown aliases expand to themselves; that is, they don't
expand.

C<expand-alias> has several command line swicthes that control the
output:

=over 4

=item -c

comma-separated output:

    $ expand-alias -c listname
    addr1, addr2, addr3

=item -t

tab-separated output

    $ expand-alias -c listname
    addr1	addr2	addr3

=item -n

newline separated output

    $ expand-alias -n listname
    addr1
    addr2
    addr3

=item -s

"sentence" form (a, b, and c).

    $ expand-alias -s listname
    addr1, addr2, and addr3

=back

The default separator is a single space:

    $ expand-alias listname
    addr1 addr2 addr3

This is useful in shell scripts:

    $ for addr in `expand-alias -f ~/my.lists friends`; do
    > mail -s 'For your eyes only!' $addr < secret-file
    > done

C<expand-alias> also takes a C<-f> option, as hinted above, which
indicates the file to be used; by default this is either /etc/aliases
or /etc/mail/aliases, tried in order until one is found.

If the C<-v> (verbose) flag is set, alias expansions are prefixed by
the alias itself.  This is useful when specifying multiple aliases on
the command line:

    $ expand-alias -vc listone listtwo listthree
    listone: addr1, addr2, addr3
    listtwo: addr4, addr3, addr2
    listthree: addr1, addr4, addr3

=head1 NOTES

C<expand-alias> works by reading the /etc/aliases file; it does not
attempt a EXPN against an SMTP server, and, for security reasons, it
does not try to read a user's .forward file.

=head1 AUTHOR

darren chamberlain E<lt>darren@cpan.orgE<gt>

=head1 SEE ALSO

L<Perl>, L<Mail::ExpandAliases>
