mail-uplift fix lines before and after message separator
[ipreg/sccs2rcs2cvs2git.git] / ipreg-mail-uplift
1 #!/usr/bin/perl
2
3 # reformat from MH to mbox
4
5 use warnings;
6 use strict;
7
8 sub shite { die "ipreg-mail-uplift: @_: $!\n"; }
9 sub shit { die "ipreg-mail-uplift: @_\n"; }
10
11 for my $f (<ipreg-mail/Institutions/*>) {
12 next unless -f $f;
13 next if $f =~ m{\.new$};
14 print "processing $f\n";
15 open my $in, '<', $f
16 or shite "open < $f";
17 my @f = <$in>;
18 for (my $i = 0; $i < @f; $i++) {
19 next unless $f[$i] =~ s{
20 ^[+][+][+]\s+
21 ([a-z]+[0-9]*[ ] # user
22 [A-Z][a-z][a-z][ ] # day
23 [A-Z][a-z][a-z])\s+ # mon
24 ([0-9]+)\s+ # date
25 ([0-9][0-9]:[0-9][0-9]:[0-9][0-9][ ] # time
26 [A-Z][A-Z][A-Z][ ] # zone
27 [12][0-9][0-9][0-9])\s*$ # year
28 }{sprintf "From %s %2d %s\n", $1, $2, $3}xe;
29 splice @f, $i++, 0, "\n" if $i > 0 && $f[$i] !~ m{^\n$};
30 my $j = $i + 1;
31 $j++ while $f[$j] =~ m{^$|^From };
32 splice @f, $i + 1, $j - $i - 1;
33 }
34 open my $out, '>', "$f.new"
35 or shite "open > $f.new";
36 print $out @f;
37 }