--- /dev/null
+#!/usr/bin/env perl
+
+use strict;
+use warnings;
+
+my %hdrs;
+my $hdr;
+my @hdrs_order;
+my %hdrs_dup = (
+ "Content-Disposition", 1,
+ "Date" => 1,
+ "From" => 1,
+ "Message-ID" => 1,
+ "Subject" => 1,
+ "To" => 1,
+);
+
+while (<>) {
+ chomp;
+ last if ($_ eq "");
+ /^([^:]+): (.*)$/;
+ if (/^\s/) {
+ @{$hdrs{$hdr}}[-1] .= "\n$_";
+ next;
+ };
+ $hdr = $1;
+ my $val = $2;
+ (defined $hdrs{$hdr}) ? (push @{$hdrs{$hdr}}, $val) : ($hdrs{$hdr} = [$val]);
+ push @hdrs_order, $hdr;
+};
+
+my %seen;
+foreach (@hdrs_order) {
+ next if (defined $seen{$_});
+ $seen{$_} = 1;
+ if (defined $hdrs_dup{$_}) {
+ print "$_: @{$hdrs{$_}}[-1]\n";
+ next;
+ };
+ foreach my $val (@{$hdrs{$_}}) {
+ print "$_: $val\n";
+ };
+};
+
+print "\n";
+while (<>) { print };
+++ /dev/null
-#!/usr/bin/env python3.6
-
-import sys
-
-fn = sys.argv[1]
-out = sys.argv[2]
-with open(fn, "rb") as fd:
- lines = fd.read().split(b"\n")
-
-def dup(lines, what):
- idx = []
- met = False
- for i, line in enumerate(lines):
- if line == "":
- break
- if met:
- if line.startswith(b" "):
- idx.append(i)
- continue
- else:
- met = False
- if line.startswith(what):
- if len(idx) == 0:
- idx.append(i)
- met = True
- else:
- return [l for n, l in enumerate(lines) if n not in idx]
- return lines
-
-lines = dup(lines, b"To")
-lines = dup(lines, b"From")
-lines = dup(lines, b"Subject")
-with open(out, "wb") as fd:
- fd.write(b"\n".join(lines))