#!/usr/bin/perl -wT

use strict;
use Getopt::Std;

sub usage 
  {
    print "Usage: $0 -f <filename>\n";
    exit 0;
  }

my %opts = ();
usage() unless getopts("f:", \%opts) && $opts{f};
my $filename = $opts{f};

open(FH, $filename) or die "Could not open \"$filename\" ($!)";

my %probdesc;

#Read the file into our hash.
#Set input record separator
$/ = "<=;=>\n";
while (<FH>)
  {
    chomp; 

    #Untaint input.  This should be safe; since these get passed to "system"
    #in a list, they won't be interpreted as shell metacharacters.
    /(.*)/s; 

    my ($key, $value) = split(/<==>/s, $1);
    $probdesc{$key} = $value;
  }
#Reset input record separator
$/ = "\n";

close(FH);

if ($probdesc{EventType} =~ /(open|close)/i)
  {
    #Reformat the date from "mm/dd/yyyy hh:mm:ss" to "yyyymmddhhmmss.mmmmmm".
    if ($probdesc{CreatedTimeStamp} =~ 
	  m#(\d{2})/(\d{2})/(\d{4}) (\d{2}):(\d{2}):(\d{2})#)
      {
	$probdesc{CreatedTimeStamp} = "$3$1$2$4$5$6.000000";
      }

    #Send the service indication.
    $ENV{PATH} = "/opt/tog-pegasus/bin";

    my $desc;
    if (exists($probdesc{EventText}) and defined($probdesc{EventText}))
      {
        $desc = $probdesc{EventText};
      }
    else
      {
        $desc = $probdesc{Description};
      }

    my $sev = "1";
    if ($probdesc{EventType} =~ /open/i)
      {
         $sev = "3";
      }
    elsif ($probdesc{EventType} =~ /close/i)
      {
         $sev = "2";
      }


    system "sendsei", 
      $probdesc{ProblemNumber},                             #EventID
      $desc,                                                #Description
      $sev,                                                 #PerceivedSeverity
      $probdesc{CreatedTimeStamp},                          #EventTime
      $probdesc{FDMachineType},                             #Type
      $probdesc{FDMachineModel},                            #Model
      $probdesc{FDMachineSerial};                           #SerialNumber
  }

exit 0;
