1BEGIN {
2	linebuf = indent = ""
3	curcol = indcol = 0
4	maxcol = 78
5	progname = ""
6	firstsh = 1
7	numcol = 0
8	spacing = 1
9	wordspace = " "
10	tab = sprintf("%c",9)
11}
12
13/^\.SH / {
14	if (firstsh == 0) printf("%s\n\n", linebuf)
15	firstsh = 0
16
17	printf("From: %s\nSubject:", progname);
18	if ($2 == "NAME") {
19		getline
20		for (i = 2; i <= NF; i++) printf(" %s", $i);
21		printf("\n\n")
22		print
23	} else {
24		for (i = 2; i <= NF; i++) printf(" %s", $i);
25		printf("\n\n")
26	}
27
28	linebuf = indent = ""
29	curcol = indcol = 0
30	next
31}
32
33/^\.TH / {
34	progname = $2
35	next
36}
37
38/^\.UC / {
39	next
40}
41
42/^\.(br|sp)/ {
43	if (linebuf != indent) {
44		printf("%s\n", linebuf)
45	}
46	linebuf = indent
47	curcol = indcol
48	next
49}
50
51/^\.PP/ {
52	if (linebuf != indent) printf("%s\n", linebuf)
53
54	printf("\n")
55
56	linebuf = "   " ; curcol = 3
57	indent = "" ; indcol = 0
58	next
59}
60
61/^\.LP/ {
62	if (linebuf != indent) printf("%s\n", linebuf)
63
64	printf("\n")
65
66	linebuf = indent = ""
67	curcol = indcol = 0
68	next
69}
70
71/^\.TP/ {
72	if (linebuf != indent) printf("%s\n", linebuf)
73
74	printf("\n")
75
76	getline; linebuf = $0
77	indent = "     "
78	curcol = indcol = 5
79	if (length(linebuf) >= 5) {
80		printf("%s\n", linebuf)
81		linebuf = indent
82	} else {
83		while (length(linebuf) < 4) linebuf = linebuf " "
84	}
85	next
86}
87
88/^\.\\"ta/ {
89	for (numcol = 2; numcol <= NF; numcol++) tabcol[numcol-1] = $numcol
90	numcol = NF
91	next
92}
93
94/^\.DT/ {
95	numcol = 0
96	next
97}
98
99numcol != 0 {
100	j = length($0)
101	k = 0
102	g = 1
103	for (i = 1; i<=j; i++) {
104		while (k < tabcol[g]) {
105			printf(" ")
106			k++
107		}
108		c = substr($0,i,1)
109		if (c == tab) {
110			g++
111		} else {
112			printf("%s", c)
113			k++
114		}
115	}
116	printf("\n")
117	next
118}
119
120/^[ 	]/ {
121	if (linebuf != indent) printf("%s\n",linebuf)
122	linebuf = indent "     "
123	curcol = indcol+5
124}
125
126{
127	word = 1
128	wordspace = " "
129	spacing = 1
130}
131
132/^\.[IB] / {
133	word = 2
134}
135
136/^\.[IB]R / {
137	wordspace = ""
138	word = 2
139	spacing = 0
140}
141
142{
143 	sep = " "
144	if (linebuf == indent) sep = ""
145
146	while (word <= NF) {
147		k = length($word)
148		if ((curcol + k) > maxcol) {
149			printf("%s\n", linebuf)
150			linebuf = indent
151			curcol = indcol
152			sep = ""
153		}
154		linebuf = linebuf sep $word
155		sep = wordspace
156		curcol += spacing + k
157		word++
158	}
159}
160
161END {
162	if (linebuf != indent) printf("%s\n\n", linebuf)
163}
164