#!/usr/bin/perl
#
# Wrap an Exim log for readability in email.
#
# $Cambridge: hermes/conf/exim/sbin/logwrap,v 1.4 2011/08/10 14:07:35 fanf2 Exp $

use warnings;
use strict;

sub wrap_for {
	my ($f,$a) = @_;
	$a =~ s/ /\n\t    /g;
	return $f.$a;
}

while (<>) {

	next unless / [<*=-][-=*>] /;

	print "\n" if / <= /;

	s{[ ](H=
	     |I=
	     |F=
	     |R=
	     |C=
	     |X=
	     |id=
	     |[<*=-][-=*>][ ]
	     |(?<!Queued[ ]mail[ ])for[ ]
	     )}
	 {\n\t$1}gx;

	s{(\tfor )(.*)}{wrap_for($1,$2)}e;

	print;
}
