1#!/usr/local/bin/perl
2#
3# create message-id / in-reply-to database
4#
5# $FreeBSD$
6
7sub usage { die "usage: mid-index name < filelist"; }
8
9sub id {
10    local($name, @files) = @_;
11    local($bytes, $bytes2, $headlen, $file);
12    local($counter);
13    local($from,$from2);
14    
15    $counter = 0;
16    open(MID, "| sort -u -o $name.mid") || die "open sort > $name.mid: $!\n";
17    open(IRT, "| sort -u -o $name.irt") || die "open sort > $name.irt: $!\n";
18
19    while(<>) {
20	local($/) = "\n\n";
21	chop;
22	$file = $_;
23
24	open(R, $file) || do {
25	    warn "open $file:$!\n";
26	    next;
27	};
28	$bytes = 0;
29
30	while(<R>) {    
31	    $headlen = length($_);
32	    $from2 = substr($_, 0, 6);
33	    $from =  substr($from2, 0, 5);
34
35	    # warn "xxx" . $from . "yyy\n";
36	    if ($from eq "From " || $from2 eq "\nFrom ") {
37
38		if ($from eq "From ") {
39		    $bytes2 = $bytes;
40		} else {
41		    # One bytes more for "\nFrom "
42		    $bytes2 = $bytes + 1;
43		}
44
45		$counter++;
46		s/\n[ \t]+/ /g;
47		if ($debug && $counter % $speedstep == 0) {
48		    print STDERR sprintf("\r%7d", $counter); 
49		}
50
51		foreach (split("\n")) {
52		    if (/^Message-id:\s+\<([^$idsep]+)/oi) {
53			print MID "$1 $file $bytes2\n";
54		    } elsif (/^Resent-Message-id:\s+\<([^$idsep]+)/oi) {
55			print MID "$1 $file $bytes2\n";
56		    } elsif (/^References:\s+\<([^$idsep]+)/oi) {
57			print IRT "$1 $file $bytes2\n";
58		    } elsif (/^In-Reply-to:\s+[^<]*\<([^$idsep]+)/oi) {
59			print IRT "$1 $file $bytes2\n";
60		    }
61		}
62	     }
63             $bytes += $headlen;
64	}
65	close R;
66    }
67    close MID || warn "close: MID\n";
68    close IRT || warn "close: IRT\n";
69    print STDERR sprintf("\r%7d", $counter) 
70	if $debug && $counter % $speedstep != 0;
71    print STDERR "\n" if $debug;
72}
73
74$idsep = '>';
75$idsep = '>@\s';
76$debug = 0;
77$speedstep = 100;
78
79&usage if $#ARGV != 0;
80$name = $ARGV[0]; shift @ARGV;
81&id($name);
82
83
84