#!/bin/sh
#
# Script to add an alert to Spool queue

# Ensure ZEUSHOME is setup to point to our installation
if [ -z "$ZEUSHOME" ]
then
    # Have a good guess, we live in $ZEUSHOME/zxtm/bin
    OLD_PWD=`pwd`
    cd `dirname $0`/../..
    ZEUSHOME=`pwd`
    export ZEUSHOME
    cd $OLD_PWD
fi

if [ ! -d "$ZEUSHOME/zxtm" ]
then
   echo "add-email-alert: FATAL: ZEUSHOME not set"
   exit 1
fi

# Try to get networking commands into our PATH
PATH=/sbin:/usr/etc/sbin:/usr/etc:/usr/sbin:$PATH

# Restart ourselves in perl
exec $ZEUSHOME/perl/miniperl -x $0 ${1+"$@"}

#!perl -w #"
#line 30

# ZXTM perl modules
BEGIN { unshift @INC, "$ENV{ZEUSHOME}/zxtm/lib/perl",
     "$ENV{ZEUSHOME}/zxtmadmin/lib/perl", "$ENV{ZEUSHOME}/perl" }

use Zeus::ZXTM::Spool;
use ZeusOS;
use strict;

# Prototypes
sub getParams(@);
sub usage(;$);

# Important Definitions
my @months = qw( Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec );
   
## Parse Commandline parameters ##
my $params = getParams( @ARGV );

my $action = $params->{action} || $params->{'arg!action'};

my $to = $params->{to} || $params->{'arg!to'};
usage( "Destination email address(es) or an action configuration required.\n" ) unless( $to || $action );
my @toArray = split( /[, ;]/, $to ) unless $action;
$to = \@toArray unless $action;

my $sender = $params->{from} || $params->{'arg!from'};
usage( "Sender email address required.\n" ) unless( $sender || $action );

my $server = $params->{server} || $params->{'arg!server'};
usage( "SMTP Server required.\n" ) unless( $server || $action );

my $entry = $params->{data} || $params->{'arg!data'};

my $alertName = $params->{eventtype};

usage( "No data passed in, nothing to send!\n" ) 
   unless( $entry );

my $port = $params->{port};

## Add message to spool queue ##
my $spool = new Zeus::ZXTM::Spool;

my $time = time;

my $error = $spool->addAlert($entry, $time, $to, $sender, $server, $port, $alertName, $action);

die( "Alert not added to spool queue: $error\n" ) if( $error );

exit( 0 );

=item getParams(@)

Extract the paramaters passed in as a easily accessible hash table.
Will only work for --foo=bar type messages. The list passed in should
be the @ARGV list. Returns a reference to a hash.

=cut

sub getParams(@)
{
   my %params = ();
   my( $quotes, $quoteParam ) = ( 0, 0 );
   for my $p ( @_ ) {
      if( $quotes ) {
         $quotes .= " " . $p;
         if( $quotes =~ /\"$/ ) {
            $params{$quoteParam} = substr( $quotes, 0, -1 );
            ( $quotes, $quoteParam ) = ( 0, 0 );
         }
      } elsif( $p =~ /^--?h(elp)?$/ ) {
         usage();
         exit(0);

      } else {
         if( $p =~ /^[^-]/s ) {
            $p = "--data=$p"; 
         }
         if( $p =~ /^--?([^\s=]+)=(.*)/s ) {
            my( $param, $val ) = ( $1, $2 );
            if( $val =~ /^\"/ && $val !~ /\"$/ ) {
               $quotes = substr( $val, 1 );
               $quoteParam = $param; 
            } else {
               $val =~ s/^\"|\"$//sg;
               $params{$param} = $val;
            }
         }
      }
   }

   return \%params;
}

=item 

Prints the usage + dies if passed an error.

=cut

sub usage(;$) 
{
   my( $error ) = @_;

   print <<EOF;
Usage: add-email-alert args data

This script will add emails to the spool queue.

REQUIRED Arguments:

-to=val    Comma separated list of people to send
             the alert to.
-from=val  The email address to send from.
-server=val  The SMTP server to send email request to.
OR
-action=val  The action configuration to get the above information from.

-data=val    The alert message to send.

OPTIONAL Arguments:

-port=num    The port of the SMTP server.
-eventtype=val  The name of the event type that triggered this e-mail.

EOF

   die( $error ) if( $error );
}

