#!/usr/bin/perl

$mode = 0;

$number="";
$area="";
$from="";
$fromaddr="";
$to="";
$toaddr="";
$subject="";
$date="";
$recv="";

main: while (<STDIN>) {
	if (/^###MESSAGE_START###/) {$mode=1; next main;}
	if (/^##HEADER##$/ && $mode) {$mode=2; next main;}
	if (/^##TEXT##$/ && $mode) {
		print <<EOF;
------------------------------------------------------------------------------
Message Number $number from area $area
------------------------------------------------------------------------------
EOF
		if ($fromaddr eq "") {
			print "From: $from\n";
		} else {
			print "From: $from ($fromaddr)\n";
		}
		if ($toaddr eq "") {
			print "From: $to\n";
		} else {
			print "From: $to ($toaddr)\n";
		}
		print <<EOF;
Subj: $subject
Send: $date
EOF
		if ($recv ne "") {print "Recv: $recv\n";}
		print <<EOF;
------------------------------------------------------------------------------
EOF
		$mode=3;
		next main;
	}
	if (/^##KLUDGES##$/ && $mode) {$mode=4; next main;}
	if (/^###MESSAGE_END###$/ && $mode) {$mode=0; next main;}
	if ($mode==2) {
		if (/^#Number: (.*)/) {$number=$1;}
		if (/^#Area: (.*)/) {$area=$1;}
		if (/^#From: (.*)/) {$from=$1;}
		if (/^#\@From: (.*)/) {
			$fromaddr=$1;
			if ($fromaddr eq "0:0/0.0") {$fromaddr="";}
		}
		if (/^#To: (.*)/) {$to=$1;}
		if (/^#\@To: (.*)/) {
			$toaddr=$1;
			if ($toaddr eq "0:0/0.0") {$toaddr="";}
		}
		if (/^#Subject: (.*)/) {$subject=$1;}
		if (/^#Date: (.*)/) {$date=$1;}
	}
	if ($mode==3) {
		while (length $_ >78) {
			if (/^(.{0,78})(\s+)/) {
				print "$1\n";
				$_ = substr($_,length $1.$2);
			} else {
				print substr($_,0,78)."\n";
				$_ = substr($_,78);
			}
		}
		print;
	}
}
