#!/usr/bin/perl
use strict;
use warnings;


my $glob = "testdata/*";
if ( defined $ARGV[0] ) {
	$glob = $ARGV[0];
}


foreach my $file (glob $glob) {
	open TD, '<', $file or die "FATAL - can't open \"$file\": $!\n";
	if ( $file =~ /\.raw$/ ) {
		print <TD>;
		close TD;
		next;
	}
	my $tmpline = '';
	while ( defined ( my $line = <TD> ) ) {
		if ( $line =~ /^##/ ) { next; }
		chomp $line;
		if ( $line =~ /^\/[^\s]+\.pm \d{8}:\d+/
			or $line =~ /^\/[^\s]+\.pm:\d+ version \d{8}/ ) {
			# new line - output old
			processline ( $tmpline );
			$tmpline = $line;
		} else {
			$tmpline =~ s/\s$//;
			$tmpline .= " $line";
		}
	}
	close TD;
	processline ( $tmpline );
}



sub processline {
	my ( $line ) = @_;
	$line =~ s/\s+$//;
	if ( $line =~ s/^\/[^\s]+postfix\.pm \d{8}:\d+ \/[^:]+:\d+ unknown:\s+// ) {
		print "$line\n\n";
	} elsif ( $line =~ s/^\/[^\s]+postfix\.pm:\d+ version \d{8}\s+/Mon 00 00:00:00 hostname / ) {
		if ( $line =~ /^\w+ \d+ \d+:\d+:\d+ \w+ \$message:\s/ ) {
			# ignore $message lines
		} else {
			$line =~ s/\[\\w\\\.\\-\]\+\\\[\[\\w\\\.:\]\+\\\]/host.domain.tld[254.254.254.254]/g;	# normal
			$line =~ s/\[\\w\.-\]\+\[\[\\w\.:\]\+\]/host.domain.tld[254.254.254.254]/g;	# mangled by mailing
			$line =~ s/<\.\+>/<user\@domain.tld>/g;
			$line =~ s/postfix\/smtp\\\[\\d\+\\\]:\\s\*/postfix\/smtp[9999]: /;	# normal
			$line =~ s/postfix\/smtp\[\\d\+\]:\\s\*/postfix\/smtp[9999]: /;	# mangled by mailing
			# check
			if ( $line =~ /\\[dw]/ ) {
				warn __FILE__." Line not completely converted: $line\n\n";
				next;
			}
			print "$line\n\n";
		}
	} elsif ( $line =~ s/^\/[^\s]+dovecot\.pm \d{8}:\d+ \/[^:]+:\d+ unknown dovecot:\s+// ) {
		print "$line\n\n";
	} elsif ( $line =~ s/^\/[^\s]+dovecot\.pm \d{8}:\d+ \"doveadm who\" unknown dovecot:\s+// ) {
		print "$line\n";
	} elsif ( $line =~ s/^\/[^\s]+fail2ban\.pm \d{8}:\d+ \/[^:]+:\d+ unknown (class "[^"]+" )?fail2ban:\s+// ) {
		print "$line\n\n";
	} elsif ( $line =~ s/^\/[^\s]+dkim\.pm \d{8}:\d+ \/[^:]+:\d+ unknown:\s+// ) {
		print "$line\n\n";
	} elsif ( $line eq '' ) {
		# ignore
	} elsif ( $line =~ s/^ *Deferral reasons: postfix-loganalyser \d{8} ==============================================// ) {
		# ignore
	} else {
		warn __FILE__." UNKNOWN $line\n\n";
	}



}



