#!/usr/bin/perl

# EB is an event browser for the EMU event management package.
# VERSION 1.0 
#  Copyright 1999
#  by Jarrix Systems Pty Ltd.  All rights reserved.  Some individual
#  files in this distribution may be covered
#  by other copyrights, as noted in their embedded comments.
#
#  Redistribution and use in source and binary forms are permitted
#  provided that this entire copyright notice is duplicated in all such
#  copies, and that any documentation, announcements, and other
#  materials related to such distribution and use acknowledge that the
#  software was developed at Jarrix Systems Pty Ltd by Jarra and Anna
#  Voleynik.
#
#  No charge, other than an "at-cost" distribution fee, may be charged
#  for copies, derivations, or distributions of this material without
#  the express written consent of the copyright holder.
#
#  THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
#  EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR ANY PARTICULAR
#  PURPOSE.

$refresh = 0;
$DEBUG = 0;

sub get_options
{
     open OPT, "@_" or die "\nCannot open @_\n\n";
     while(<OPT>) {
          if(/password\s+(.*)/) {
               $gpassword = $1;
          }
          if(/dbfile\s+(.*)/) {
               $dbfile = $1;
         	}
          if(/binterval\s+(.*)/) {
               $binterval = $1;
         	}
          if(/emsgcmd\s+(.*)/) {
               $emsgcmd = $1;
         }
     }
}

sub readstr {
	my $str = $_[0];
	my $ans = "";
	while ($ans !~ /[a-zA-z]+/) {
		print $str;
		$ans = <STDIN>;
		chop($ans);
	}
	return $ans;
}

sub readnum {
	my $str = $_[0];
	my $ans = "";
	while ($ans !~ /\d+/) {
		print $str;
		$ans = <STDIN>;
		chop($ans);
	}
	return $ans;
}

sub INT_handler {
	print ">>";
	my $line = <STDIN>;
	if($line =~ /^d\s+(\d+)$/) {
		my $msgtodelete = $1;
		system("$emsgcmd -o delete -p $port -w $gpassword  -m \"$buf{$msgtodelete}\"");
		$refresh = 1;
		return;
	}
	if($line =~ /^[qe]+.*/) {
		system("tput sgr0");
		exit 0;
	}
	if($line =~ /^a\s+(\d+)$/) {
		my $msgtoannotate = $1;
		my $icomment = readstr "comment ?";
		system("$emsgcmd -o comment -p $port -w $gpassword -m \"$buf{$msgtoannotate} $icomment\"");
		$refresh = 1;
		return;
	}
	if($line =~ /^[m]+.*/) {
		my $isev = readnum "severity ?";
		my $iclass = readstr "class ?";
		my $ittl = readstr "time to live (3s|3m|3h|12:30)?";
		my $imsg = readstr "message ?";
		print "$emsgcmd -o normal -p $port -c $iclass -w $gpassword -t $ittl -s $isev -m $imsg\n" if ($DEBUG);
		sleep 5 if ($DEBUG);
		system("$emsgcmd -o normal -p $port -c $iclass -w $gpassword -t $ittl -s $isev -m \"$imsg\"");
		$refresh = 1;
		return;
	}
	else {
		print "available commands: d[el] <msgnum>, m[essage], a[nnotate] <msgnum>, q[uit], e[xit]\n";
		sleep(2);
	}
}

if($#ARGV != 0) {
     die "\neb <port>\n\n";
}
$port = $ARGV[0];

$confdir = $ENV{EMUBASE};
if ($confdir) {
     $conffile = "$confdir/conf/$port".".cfg";
} else {
     $conffile = "/etc/eemu/$port".".cfg";
}                                                      

get_options $conffile;

if( $binterval eq "" || $gpassword eq "" || $dbfile eq "" || $emsgcmd eq "") {
     die "\nbinterval,password,dbfile or emsgcmd are not specified in the config file\n\n";
}

$SIG{INT} = 'INT_handler'; 
			
$textfile = $dbfile.".txt";
$lockfile = $dbfile.".lck";

if($ENV{EMUSELECT}) {
	$SELECTCMD = $ENV{EMUSELECT};
} else
{
	$SELECTCMD = " sort -t'^' +1 ";	
}

while ( 1 ) {
	
	# take file modification time
	my $j;
	my $filemtime; # file modification time
	my ($j,$min,$hour,$mday,$mon);

	($j,$j,$j,$j,$j,$j,$j,$j,$j,$filemtime) = stat $textfile;
	system("clear");
	($j,$min,$hour,$mday,$mon) = localtime();
	printf "%02d/%02d %02d:%02d       (EMU Browser v1.0)  Hit Ctrl-C for command mode \n",$mday,$mon+1,$hour,$min;
	printf "%-4s:%-11s:%-16s:%-36s:%1s -> %s\n        :%s\n","NUM","TIME","SYSTEM","CLASS","S","MSG","COMMENT";
	print "-------------------------------------------------------------------------------\n";
	while( -f $lockfile) {
		sleep 1 ;
	}
	open TXT, "cat $textfile | $SELECTCMD |";
	undef %buf;
	%buf = {}; # maintain msgnum/key conversion for message deletions
	while (<TXT>) {
		$line = $_;
		($msgnum,$key,$send_time,$send_host,$send_user,$ttl,$severity,$class,$message,$type,$comment,$first_timestamp,$update_timestamp,$update_count) = split(/\^/,$line);
		# ttl of infinity is displayed in reverse
		system("tput smso") if($ttl < 0);
		# the first occurence of a message is displayed in bold
		system("tput bold") if($update_count == 1);
		$buf{$msgnum}="$key";
		($host,) = split(/\./,$send_host);
     	printf "%-4s %-11s %-16s %-36s %1s %s\n", $msgnum, $send_time, $host, $class, $severity,$message;
		if ($comment ne "") { # display comment only if there is any
			printf "        %s\n", $comment;
		}
		system("tput sgr0");
		system("tput rmso");
	}
	close TXT;
	while( 1 ) { 
		if($refresh == 1) {
			$refresh = 0;
			last;
		}
		sleep $binterval;
		my $nfilemtime; # file modification time
		($j,$j,$j,$j,$j,$j,$j,$j,$j,$nfilemtime) = stat $textfile;
		if ( $nfilemtime != $filemtime ) { #has the file mod. time changed ?
			last;
		}
	}
}
