#!/usr/bin/perl -s

@delete = qw(
	Received[-\w]*:
	In-Reply-To:
	User-Agent:
	X-[-\w]*:
	Mailing-List:
	Delivered-To:
	List-Archive: List-Help: List-Id: List-Post:
	List-Subscribe: List-Unsubscribe:
	Thread-[-\w]*:
	Importance: Precedence: Priority: Sensitivity:
	Auto-Submitted:
	Disposition-Notification-To:
	Status:
	DomainKey-Signature:
	Comment: Comments:
	HOP-COUNT:
	Expiry-Date:
	DKIM-[-\w]*:
	Seal-Send-Time:
	Accept-Language: acceptlanguage:
	Face:
	Mail-Copies-To:
	Old-Return-Path:
	Authentication-Results:
	Bounces-to:
	Spam[-\w]*:
	ARC-[-\w]*:\S*
	IronPort-[-\w]*:
	Resent-[-\w]*:
	Feedback-ID:

	Path:
	Organization:
	Lines:
	ACategory:
	Slugword:
	Threadword:
	Approved:
	NNTP-[-\w]*:
	Originator:
);

@mime = qw( MIME-[-\w]*: Content-[-\w]*: );

@repref = qw( Reply-To: Return-Path: References: );

# minhdr - filter out commonly unwanted message header lines
# Steve Kinzler, steve@kinzler.com, Jun 03/Jun 09/Sep 11
# https://kinzler.com/me/home.html#unix

$repref = do { $_ = join(' ', @repref); s/: /\//g; s/://; $_ };
$usage  = "usage: $0 [ -m ] [ -r ] [ -b ] [ file ... ]
	-m	also filter out MIME header lines
	-r	also filter out $repref lines
	-b	take input as a mbox and filter all message headers\n";
die $usage if $h;

$delete = '^(' . join('|', @delete, ($m) ? @mime   : (),
				    ($r) ? @repref : ()) . ')$';
study $delete;

AGAIN:

$fld = '';
while (<>) {
	print, last if /^$/;

	1 while s/^(\t*) {8}/$1\t/;

	$fld = $& if /^\S+/;
	print unless $fld =~ /$delete/io;
}

while (<>) {
	print;
	goto AGAIN if $b && /^From /;
}
