136252Swosch#!/usr/local/bin/perl
236252Swosch#
336252Swosch# create message-id / in-reply-to database
436252Swosch#
550477Speter# $FreeBSD$
636252Swosch
736252Swoschsub usage { die "usage: mid-index name < filelist"; }
836252Swosch
936252Swoschsub id {
1036252Swosch    local($name, @files) = @_;
1136252Swosch    local($bytes, $bytes2, $headlen, $file);
1236252Swosch    local($counter);
1336252Swosch    local($from,$from2);
1436252Swosch    
1536252Swosch    $counter = 0;
1636252Swosch    open(MID, "| sort -u -o $name.mid") || die "open sort > $name.mid: $!\n";
1736252Swosch    open(IRT, "| sort -u -o $name.irt") || die "open sort > $name.irt: $!\n";
1836252Swosch
1936252Swosch    while(<>) {
2036252Swosch	local($/) = "\n\n";
2136252Swosch	chop;
2236252Swosch	$file = $_;
2336252Swosch
2436252Swosch	open(R, $file) || do {
2536252Swosch	    warn "open $file:$!\n";
2636252Swosch	    next;
2736252Swosch	};
2836252Swosch	$bytes = 0;
2936252Swosch
3036252Swosch	while(<R>) {    
3136252Swosch	    $headlen = length($_);
3236252Swosch	    $from2 = substr($_, 0, 6);
3336252Swosch	    $from =  substr($from2, 0, 5);
3436252Swosch
3536252Swosch	    # warn "xxx" . $from . "yyy\n";
3636252Swosch	    if ($from eq "From " || $from2 eq "\nFrom ") {
3736252Swosch
3836252Swosch		if ($from eq "From ") {
3936252Swosch		    $bytes2 = $bytes;
4036252Swosch		} else {
4136252Swosch		    # One bytes more for "\nFrom "
4236252Swosch		    $bytes2 = $bytes + 1;
4336252Swosch		}
4436252Swosch
4536252Swosch		$counter++;
4636252Swosch		s/\n[ \t]+/ /g;
4736252Swosch		if ($debug && $counter % $speedstep == 0) {
4836252Swosch		    print STDERR sprintf("\r%7d", $counter); 
4936252Swosch		}
5036252Swosch
5136252Swosch		foreach (split("\n")) {
5236252Swosch		    if (/^Message-id:\s+\<([^$idsep]+)/oi) {
5336252Swosch			print MID "$1 $file $bytes2\n";
5436252Swosch		    } elsif (/^Resent-Message-id:\s+\<([^$idsep]+)/oi) {
5536252Swosch			print MID "$1 $file $bytes2\n";
5636252Swosch		    } elsif (/^References:\s+\<([^$idsep]+)/oi) {
5736252Swosch			print IRT "$1 $file $bytes2\n";
5836252Swosch		    } elsif (/^In-Reply-to:\s+[^<]*\<([^$idsep]+)/oi) {
5936252Swosch			print IRT "$1 $file $bytes2\n";
6036252Swosch		    }
6136252Swosch		}
6236252Swosch	     }
6336252Swosch             $bytes += $headlen;
6436252Swosch	}
6536252Swosch	close R;
6636252Swosch    }
6736252Swosch    close MID || warn "close: MID\n";
6836252Swosch    close IRT || warn "close: IRT\n";
6936252Swosch    print STDERR sprintf("\r%7d", $counter) 
7036252Swosch	if $debug && $counter % $speedstep != 0;
7136252Swosch    print STDERR "\n" if $debug;
7236252Swosch}
7336252Swosch
7436252Swosch$idsep = '>';
7536252Swosch$idsep = '>@\s';
7636252Swosch$debug = 0;
7736252Swosch$speedstep = 100;
7836252Swosch
7936252Swosch&usage if $#ARGV != 0;
8036252Swosch$name = $ARGV[0]; shift @ARGV;
8136252Swosch&id($name);
8236252Swosch
8336252Swosch
84