#!/usr/bin/perl -w
#
#--------------------------------------------------------------------------
# COPYRIGHT INFORMATION - DO NOT REMOVE
# "Portions Copyright (c) 2000-2001 LinuxMagic Inc. All Rights Reserved.
#
# This file contains Original Code and/or Modifications of Original Code as
# defined in and that are subject to the Free Source Code License Version
# 1.0 (the 'License'). You may not use this file except in compliance with
# the License. Please obtain a copy of the License at:
#
# http://www.linuxmagic.com/opensource/licensing/
#
# and read it before using this file.
#
# The Original Code and all software distributed under the License are
# distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
# EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
# INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY, FITNESS
# FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT. Please see
# the License for the specific language governing rights and limitations
# under the License."
#
# Please read the terms of this license carefully. By using or downloading
# this software or file, you are accepting and agreeing to the terms of this
# license with LinuxMagic Inc. If you are agreeing to this license on behalf
# of a company, you represent that you are authorized to bind the company to
# such a license. If you do not meet this criterion or you do not agree to
# any of the terms of this license, do NOT download, distribute, use or alter
# this software or file in any way.
#
#  Authors: Josh Wilsdon <josh@wizard.ca>
#
#  DO NOT MODIFY WITHOUT CONSULTING THE LICENSE
#
#  $Id: qmail-deliverwatch.pl,v 1.1 2004/09/04 21:15:06 michael Exp $
#--------------------------------------------------------------------------
#
# This program is intended for use in watching qmail
# deliveries via the qmail log.  You can run it in realtime
# by doing:
#
#   tail -f /var/log/qmail | qmail-deliverwatch.pl 
#
# or you can do it on old logs by:
#
#   cat /var/log/qmail | qmail-deliverwatchwatch.pl | less
#
# It expects to have a qmail logfile on stdin and writes it's
# output to stdout.
#
#    $Id: qmail-deliverwatch.pl,v 1.1 2004/09/04 21:15:06 michael Exp $
#

$DEBUG = 0;	# 1 or 0 == on or off

%deliveries = ();
%times = ();

LINE: while($line = <STDIN>) {
	chomp($line);
	if ($line !~ m/qmail\:/) {
		next LINE;
	}
	$line =~ s/^.*qmail\://;
	$line =~ s/^\s+//;
	if ($line =~ m/(\d+)\.\d+\sstarting\sdelivery\s(\d+)\:\s(.*)$/) {
	    	$delstart = $1;
		$delno = $2;
		$delstr = $3;
		$deliveries{$delno} = $delstr;
		$times{$delno} = $delstart;
	} elsif ($line =~ m/(\d+)\.\d+\sdelivery\s(\d+)\:\s(.*)$/) {
	        $delstop = $1;
		$delno = $2;
		$delstr = $3;
		if (defined $deliveries{$delno}) {
			print "$delno: " . $deliveries{$delno} . "\n";
			if (defined $times{$delno}) {
			    print "\t\\_ " . ($delstop - $times{$delno}) . " sec(s) ";
			    print "started " . localtime($times{$delno}) . "\n";
			    delete $times{$delno};
			}
			print "\t\\_ " . $delstr . "\n"; 
			if ($DEBUG == 1) {
 	        		print "\t\\_ " . scalar(keys %deliveries) . " remain\n";
			}
			delete $deliveries{$delno};
		}
	}
}
exit(0);
