#!/usr/bin/perl

use strict;
use Getopt::Long;
use NOCpulse::Log::Logger;
use NOCpulse::Notif::Alert;
use NOCpulse::Notif::EscalatorInterface;
use NOCpulse::Notif::Send;
use NOCpulse::Notif::SendInfo;

umask(002);

my $np_cfg       = new NOCpulse::Config;
my $tmp_dir      = $np_cfg->get('notification','tmp_dir');
my $SOCKET_FILENAME= "$tmp_dir/escsock";   ## TBD -- replace with config file entry

my @VALID_OPS = qw(clear show ack nak);
my @VALID_ITEMS = qw(send alert);

my ($help);
&GetOptions ( 'help' => \$help );

my $Log = NOCpulse::Log::Logger->new(__PACKAGE__,9);

if ($help) {
  &help();
  exit(0);
}

my ($op,$item,$id)=@ARGV;
$op=lc($op);
$item=lc($item);
$id=lc($id);

unless (grep (/^$op$/, @VALID_OPS)) {
  print STDERR "invalid operation\n";
  &help();
  exit(1);
}

unless (grep (/^$item$/, @VALID_ITEMS)) {
  print STDERR "invalid item\n";
  &help();
  exit(1);
}

my $bad=0;
unless ($id =~ /^all$/i) {
  if ($item =~ /send/) {
    unless ($id =~ /^[0-9a-z]{6}$/) {
      $bad=1;
    }
  } else {
    unless ($id =~ /^[0-9]{5}$/) {
      $bad=1;
    }
  }
}

if ($bad) {
  print STDERR "invalid id\n";
  &help();
  exit(1);
}

if ($op eq 'clear' && $item eq 'alert') {
    my $escalator_if = NOCpulse::Notif::EscalatorInterface->new(
      socket_filename => $SOCKET_FILENAME);
    my $result=$escalator_if->clear_alert_id($id);
    if ($result) {
      print STDERR "Unable to find alert $id\n";
      exit(1);
    }
    print "Success!\n";
    exit(0);
}

if ($op eq 'show') {
    my $escalator_if = NOCpulse::Notif::EscalatorInterface->new(
      socket_filename => $SOCKET_FILENAME);

    my ($filename,@files);

    if ($id eq 'all') {
      @files=glob("$tmp_dir/[0-9][0-9]*");
    } elsif ($item eq 'alert') {
      push(@files,$escalator_if->filename_for_alert_id($id));
    } else {
      push(@files,$filename = $escalator_if->filename_for_send_id($id));
    }

    unless (@files) {
      print STDERR "$item id $id not found\n";
      exit(1);
    }
    foreach $filename (@files) { 
      my $alert=NOCpulse::Notif::Alert->from_file($filename);
      unless ($alert) {
        print STDERR "Unable to open alert file\n";
        next;
      }

      if ($item eq 'alert') {
        print $alert->show, "\n";
      } else {
        my $send = $alert->send_named($id);
        print $send->show, "\n" if $send;
      }
    }
  exit(0);
}

if ($id eq 'all') {
  print STDERR "Operation not implemented\n";
  exit(1);
}

if ($item eq 'alert') {
  print STDERR "Cannot ack or nak and alert\n";
  exit(1);
}

# Standard ack or nak
my $escalator_if = NOCpulse::Notif::EscalatorInterface->new(
  socket_filename => $SOCKET_FILENAME);
my $result = $escalator_if->ack($op,$id);
if ($result) {
  print STDERR "Acknowledgement failed, perhaps the send no longer exists\n";
  exit(1);
} else {
  print "Success!\n";
}

sub help {
  print "$0 [(clear|show|ack|nak) (send|alert) (<id>|all)] [--help]\n";
  print "ack:    Confirm that you have received the message and\naccepted responsibility.  Stops escalation, if applicable,\nin most cases except allack.\n";
  print "nak:    Refuse responsibility for the message.  Causes escalation to the next recipient, if applicable.\n"; 
  print "clear:  Stops escalation, if applicable.\n";
  print "show:   Show details.\n";
}
