#!/usr/bin/perl

# emucleaner -  expire emu messages
# VERSION 2.34
#  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.


use SDBM_File;
use Fcntl;

$DEBUG = 0;

# default for cbootwait is  10 seconds
$cbootwait = 10;

#############
# get_options - get command line options
#############
sub get_options
{
     open OPT, "@_" or die "\nCannot open @_\n\n";
     while(<OPT>) {
		if(/^\#/) {
               next;
          }
          if(/dbfile\s+(.*)/) {
               $dbfile = $1;
               print "dbfile $dbfile\n" if ($DEBUG);
          }
          if(/cinterval\s+(.*)/) {
               $cinterval = $1;
               print "cinterval $cinterval\n" if ($DEBUG);
          }
          if(/password\s+(.*)/) {
               $gpassword = $1;
               print "log $gpassword\n" if ($DEBUG);
          }
          if(/emsgcmd\s+(.*)/) {
               $emsgcmd = $1;
               $f_dc = 1;
               print "emsgcmd $emsgcmd\n" if ($DEBUG);
          }
          if(/logdir\s+(.*)/) {
               $logdir = $1;
               print "logdir $logdir\n" if ($DEBUG);
          }
          if(/cbootwait\s+(.*)/) {
               $cbootwait = $1;
               print "cbootwait $cbootwait\n" if ($DEBUG);
          }


     }
	close OPT;
}

if($#ARGV != 0) {
     die "\nemucleaner <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( $emsgcmd eq "" || $gpassword eq "" || $dbfile eq "" || $cinterval eq "") {
     die "\nemsgcmd,password,dbfile or cinterval are not specified in the config file\n\n";
}

# get the current process ID for later killing
$pidfile = $logdir."/"."emucleaner.pid";
open PID, ">$pidfile";
print PID "$$\n";
close PID;

sleep $cbootwait;

while ( 1 ) {
    
     tie(%DB, SDBM_File, "$dbfile",O_RD, 0660) or die "cannot tie\n";
     foreach (sort keys %DB) {
          $key = $_;
          if($key eq "MSGNUM") {
               next;
          }
		($msgnum,$send_time,$send_host,$send_user,$ttl,$severity,$type,$first_timestamp,$update_timestamp,$update_count) = unpack("A6A12A64A24A12A12A24A16A16A12",$DB{$key});

          $currenttime=time();
		if ($type eq "sleep") {
			$mtype = "wakeup";
		}
		else {
			$mtype = "delete";
		} 
		if ($ttl =~ /(.*):(.*)/) { # ttl in form of HH:MM
			($sec,$min,$hour,$mday,$mon) = localtime(); # get current time
			$hour2 = sprintf ("%02d", $hour);
			$min2 = sprintf ("%02d", $min);
			if ($hour2 eq $1 && $min2 eq $2) {
				printf("$1:$2 $emsgcmd -o $mtype -p $port -c none -w $gpassword  -t 0 -s 0 -m \'$key\' \n") if ($DEBUG);
                    system("$emsgcmd -o $mtype -p $port -c none -w $gpassword  -t 0 -s 0 -m \'$key\' ");
			}

		} 
		else {
          	$age = ($currenttime - $ttl);
          	if ( $age > 0  && $ttl > 0) {
                    printf("$emsgcmd -o $mtype -p $port -c none -w $gpassword  -t 0 -s 0 -m \'$key\' \n") if ($DEBUG);
                    system("$emsgcmd -o $mtype -p $port -c none -w $gpassword  -t 0 -s 0 -m \'$key\' ");
			}
          }
     }
     untie %DB;
     sleep($cinterval);
}

