#!/usr/bin/gawk -f
#
# man2plain
#
#	Read man output from the command line and format it into
#	plain ascii.
#
#    Examples:
#
#	man ls | man2plain >ls.txt
#

BEGIN {
	#
	# First set some magic variables. These settings may differ
	# from yours.
	#

	pagebreaks = 1;

	# Pagelength used by man:
	pagelength = 66;
	
	# Number of lines before text from top of page:
	header = 6;

	# Number of lines behind text to end of page:
	footer = 6;


	#
	# Now we initialize some line/page counters.
	#
	pageno = 1;
	lineno = 0;


	argc = 1;
	if (ARGV[argc] == "-p") {
		pagebreaks = 0;
		ARGV[argc++] = "";
		}


	rap = rac = 0;
	while ((line = readahead()) != "\032") {
		if (rac > 12)
			break;

		gsub(/.\010/, "", line);
		if (substr(line, 1, 4) == "NAME") {
			title = "";
			while ((line = readahead()) != "\032"  &&  line != "")
				title = title " " line;

			sub(/^[ \t]+/, "", title);
			gsub(/[ \t]+/, " ", title);
			sub(/[ \t]+$/, "", title);
			printf ("+subject: %s\n", title);

			break;
			}
		}
		

	print "";
	emptylines = -1;
	lineno = 0;

	while ((line = readline()) != "\032") {
		if (pagebreaks != 0)
			lineno++;

		indent = match(line, /[^ \t]/);

		#
		# Is this line on a different page than the last?
		#
		if (lineno > pagelength) {
			lineno = 1;
			pageno++;
			}

		#
		# Are we inside a page header on page 2 or later? If yes
		# we skip that header.
		#
		if (lineno <= header  &&  pageno > 1)
			continue;


		#
		# Is this the first line on a new page? If yes we have
		# to do a special hack: nroff doesn't insert a blank line
		# into a page break where usually should be one. We look
		# at the line indentation to determine if there probably
		# should be one.
		#
		if (lineno == header + 1) {
			if (indent != 0  &&  previndent != 0  &&  indent != previndent)
				print "";
			}


		#
		# Are we inside a page footer?
		#
		if (lineno > (pagelength - footer))
			continue;


		#
		# Is this an blank line?  If a line is blank and it is followed
		# by more these are buffered and only printed if the block of
		# blank lines is followed by some real output.  This is to
		# avoid the empty lines on the last manual page.
		#
		if (line == "") {
			if (emptylines >= 0) {
				emptylines++;
				continue;
				}
			else {
				print "";
				emptylines = 0;
				continue;
				}
			}
		else if (emptylines > 0) {
			for (i=0; i<emptylines; i++)
				print "";
			}

		emptylines = -1; 



		#
		# Remove ``_^Hc''
		#
		while (match(line, /(_\010[^_])+/) > 0) {
			italics = substr(line, RSTART, RLENGTH);
			gsub(/_\010/, "", italics);
			line = substr(line, 1, RSTART - 1) italics substr(line, RSTART + RLENGTH);
			}
			
		#
		# Remove ``c^Hc''
		#
		while (match(line, /(.\010.)+/) > 0) {
			bold = substr(line, RSTART, RLENGTH);
			gsub(/.\010/, "", bold);
			line = substr(line, 1, RSTART - 1) bold substr(line, RSTART + RLENGTH);
			}


		print line;
		previndent = indent;
		}
	}


function readahead() {
	if (getline thisline > 0) {
		ral[rac++] = thisline;
		return (thisline);
		}
	
	return ("\032");
	}

function readline() {
	if (rap < rac)
		return (ral[rap++]);
	else if (getline thisline > 0)
		return (thisline);
	
	return ("\032");
	}

