#!/usr/bin/perl -w

# full format module
#
# Version: $Revision: 0.3 $
# Author: Benjamin Oshrin
# Date: $Date: 2003/10/06 22:24:18 $
#
# Copyright (c) 2003
# The Trustees of Columbia University in the City of New York
# Academic Information Systems
# 
# License restrictions apply, see doc/license.html for details.

use strict;
use FindBin;
use lib "$FindBin::Bin/../common";
use Survivor::Format;

# Create a new Format object.

my $sf = new Survivor::Format();

my $r = $sf->ParseAlert();

if($r == MODEXEC_OK)
{
    my @addrlist = ();
    my $subject = "";
    my $message = "";
    my $cls = "";
    my $hosts = "";
    
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) =
	localtime($sf->Data("Time"));

    # There can be multiple Modules stored within a RecipientSet, each
    # of which can have multiple addresses and calllists (used to generate
    # the addresses).
    
    foreach my $x (@{$sf->Data("RecipientSet")}) {
	# The module name is available at $x->{"name"}
	push(@addrlist, @{$x->{"addresses"}});
	$cls = join(',', @{$x->{"calllists"}}, $cls);
    }

    # Toss the trailing comma
    chop($cls);

    # There can also be multiple hosts.

    $hosts = join(',', @{$sf->Data("Host")});

    # Generate a subject line

    if(defined $sf->Data("FixSummary") && defined $sf->Data("FixReturnCode"))
    {
	$subject = "FIXALERT: ";
    }
    else
    {
	$subject = "ALERT: ";
    }
    
    $subject .= $sf->Data("Service") . "@" . $hosts;

    if($sf->Data("ReturnCode") == 0)
    {
	$subject .= " (cleared)";
    }
    
    # Generate a message body consisting of all the other fields.

    $message = "Notifying " . join(',', @addrlist);

    if($sf->Data("ReturnCode") != 0)
    {
	# For notify on clear calllists are not remembered
	
	$message .= " via " . $cls . " calllist(s)";
    }

    $message .= "\n\n";
    $message .= " Summary: " . $sf->Data("Summary") . "\n\n";

    if(defined $sf->Data("FixSummary") && defined $sf->Data("FixReturnCode"))
    {
	$message .= "  A fix was attempted for this problem.\n";
	$message .= "  Fix Summary:\t\t" . $sf->Data("FixSummary") . "\n";
	$message .= "  Fix Return Code:\t" . $sf->Data("FixReturnCode")
	    . "\n\n";
    }

    $message .= " Host:\t\t" . $hosts . "\n";
    $message .= " Service:\t" . $sf->Data("Service") . "\n";

    $message .= sprintf(" Time:\t\t%.2d:%.2d:%.2d %.2d/%.2d/%.2d\n",
			$hour, $min, $sec, $mon+1, $mday, $year + 1900);

    $message .= " ReturnCode:\t" . $sf->Data("ReturnCode") . " ("
	. $sf->Data("Instances") . " instances)\n";
    $message .= " Token:\t\t" . $sf->Data("Token") . "\n\n";
    
    if($sf->Data("ReturnCode") > 0 && defined $sf->Data("HelpFile")
       && open(HELP, $sf->Data("HelpFile")))
    {
	# If open fails, just ignore.  Don't send helpfiles on clear.

	while(<HELP>)
	{
	    $message .= $_;
	}

	close HELP;

	$message .= "\n";
    }

    $message .= "This message is one-way and may not be replied to.\n";
    
    $sf->GenerateFormattedAlert(\@addrlist, $subject, $message, "no");
}
# else we can't really do anything since we don't have any addresses
# to notify.

exit $r;
