#!/usr/bin/perl -I/usr/lib/bs/bin -I/usr/lib/bs/uxmon
#
# Big Sister Network Monitor Event Notification Relay
#
# Copyright (C) 1999  Roland B Roberts
#
# 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; either version 2 of the License, or
# (at your option) any later version.
#
# 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., 675 Mass Ave, Cambridge, MA 02139, USA.
#

use Getopt::Long;
use Cwd 'abs_path';
use common;

# Find the program name and path.
($bindir, $prog) = ($0 =~ m=^(.*)/([^/]+)=);
$bindir = abs_path("$bindir");

# Save @ARGV for later reuse.
@save_ARGV = @ARGV;
$Getopt::Long::ignorecase = 0;
GetOptions (\%option, "C=s", "D:i", "H", "s=s",
	    "S=i", "o=s", "m=s", "t=s", "c=s", "h=s", "M=s")
    or &Usage;

proginit();

$host = $main::hostname;
$root = $main::root;

$dl = $option{D};

$cfg = $option{C} ? $option{C} : "$root/adm/notify.cfg";
$default_pager = "$root/bin/log_mail";

# We should really preprocess the message data to replace all those
# silly "|>" symbols with newlines.  Additionally, the subject should
# not contain any of those (no multi-line subjects), but that may
# require modifying our caller....

# If the open fails, just pass everything through to the default pager.
if (! open (CFG, "<$cfg")) {
    Platform::system( $default_pager, @save_ARGV );
} else {

    @lines = grep !/^\s*#/, <CFG>;
    close CFG;
    foreach $line ( @lines ) {
	( $key, @rest ) = split /:/, $line;
	$alias{$key} = [ @rest ];
	if ($alias{$key}[3] =~ m/^\s*$/) {
	    undef $alias{$key}[3];
	}
    }
    
    # Put back the switches (sort of).
    @switches = ();
    foreach $key ( keys %option ) { 
	push @switches, "-$key", "$option{$key}";
    }

    # This is the easy solution; it ignores the BATCHABLE flag and
    # sends each notice independently.
    foreach $user ( @ARGV ) {
	if (defined $alias{$user}) {
	    $pager = $alias{$user}[1];
	    if ( ! -x $pager ) {
		$pager = "$bindir/$pager";
	    }
	    if ( ! -x $pager ) {
		print STDERR "$default_pager ... $user\n" if ($dl > 2);
		Platform::system( $default_pager, @switches, $user );
	    } else {
		print STDERR "$pager ... $alias{$user}->[0] $alias{$user}->[3]\n" if ($dl > 2);
		if (defined $alias{$user}->[3]) {
		    Platform::system( $pager, @switches, $alias{$user}->[0], $alias{$user}->[3] );
		} else {
		    Platform::system( $pager, @switches, $alias{$user}->[0] );
		}
	    }
	} else {
	    print STDERR "$default_pager ... $user\n" if ($dl > 2);
	    Platform::system( $default_pager, @switches, $user );
	}
    }
}
   
sub Usage {
    print STDERR "Usage: $prog [options] userID [userID ...]\n";
    print STDERR "oops, you made a boo-boo\n";
    return 1;
}
