#!/usr/bin/eperl
###
#
# Copyright (c) Ensim Corporation 2000, 2001   All Rights Reserved.
#
# This software is furnished under a license and may be used and copied
# only  in  accordance  with  the  terms  of such  license and with the
# inclusion of the above copyright notice. This software or any other
# copies thereof may not be provided or otherwise made available to any
# other person. No title to and ownership of the software is hereby
# transferred.
#
# The information in this software is subject to change without notice
# and  should  not be  construed  as  a commitment by Ensim Corporation.
# Ensim assumes no responsibility for the use or  reliability  of its
# software on equipment which is not supplied by Ensim.
#
# ----------------------------------------------------------------------
# Usage: list_aliases
# ----------------------------------------------------------------------

push @INC, ($ENV{'OCW_SVCPATH'} || "/usr/lib/opcenter")."/sendmail";

use strict;
use Getopt::Std;
require sendmail;

my( $usage, $sendmailcf, @aliases, $alias, @filtered, $pattern, %option);

$usage = "Usage: list_aliases [-r | -f | -a]\n";

die $usage unless ( Getopt::Std::getopts('rfa',\%option) );

$sendmailcf = &sendmail::get_sendmailcf();
@aliases = &sendmail::list_aliases(&sendmail::aliases_file($sendmailcf));

# Ignore aliases that are commented out
@aliases = grep { $_->{'enabled'} == 1 } @aliases;

foreach $alias ( @aliases ) {
    if ( defined $option{r} ) {
        # List responders
        @filtered = grep { /^\|responder\.sh\s/ } @{$alias->{'values'}}; 
    } elsif ( defined $option{f} ) {
        # List files (aliases directed to files)
        @filtered = grep { /^\// } @{$alias->{'values'}}; 
    } else {
        # List normal aliases
        @filtered = grep { !/^[\|\:]/ } @{$alias->{'values'}}; 
    }
    if ( @filtered ) {
        print "$alias->{'name'} ",join(',', @filtered),"\n";
    }
}
