#!/bin/sh
#-----------------------------------------------------------------------------#
# Copyright (C) 1997-1998, 2002, 2006 The University of Melbourne. 
# This file may only be copied under the terms of the GNU General
# Public Licence - see the file COPYING in the Mercury distribution.
#-----------------------------------------------------------------------------#
#
# make_manpage: create Unix man page from help message.
#
# Usage: make_manpage <program>
#
# This file takes the output of `<program> --help'
# and turns it into a Unix-style man page by
# massaging it a bit and inserting some extra stuff that is
# the same for all the Mercury man pages.

# Section headings must match /[\/A-Za-z ]*:/.
# We insert the `.SH' command for them,
# convert them to uppercase, delete the ":",
# and replace "USAGE" with "SYNOPSIS".

# Section sub-headings start with spaces.
# We insert the `.SH' command for them.

# Option headings start with a tab and then a `-'.
# We put them in bold, and indent them differently.

# Each line of the main text must start with a tab.
# We change the tab to "\&".

# NOTE: some of the sed expressions below occur on very long lines.
# This is deliberate, BSD sed has problems handling line continuations in some
# cases.

program="$1"
name="`basename $program `"

trap 'rm -f /tmp/make_manpage$$' 1 2 3 13 15
$program --help 2>&1 \
	| sed	\
		-e '/^[\/A-Za-z ]*:[ 	]/s//.SH "&"\
/'			\
		-e '/^[\/A-Za-z ]*:/s//.SH "&"\
/'			\
		-e '/^ .*/s//.SH "&"\
/'			\
		-e '/^	[^-	].*:/s/^	.*$/	.Ve\
.B\
.Vb 1\
\\\& &\
.Ve\
.Vb 2/' 					\
		-e '/^	-/s/^	.*$/	.Ve\
.B\
.Vb 1\
\\\& &\
.Ve\
.Vb 2/' 					\
		-e 's/\\& 	/\\\& /' \
		-e 's/^		/	\\\&	/g' 	\
		-e 's/^	//' \
	| sed	\
		-e '/^.SH "[\/A-Za-z ]*:[ 	]*"/y/abcdefghijklmnopqrstuvwxyz:/ABCDEFGHIJKLMNOPQRSTUVWXYZ /' \
		-e 's/^.SH "USAGE[ 	]*"/.SH "SYNOPSIS"/' \
	> /tmp/make_manpage$$ 
manpage="`cat /tmp/make_manpage$$`"
rm -f /tmp/make_manpage$$
uppername="$name"

if echo "$manpage" | grep '^.SH "ARGUMENTS' > /dev/null; then
	first_half="`echo \"$manpage\" | sed '/^.SH \"ARGUMENTS *\"/,$d'`"
	last_half="`echo \"$manpage\" | sed -n '/^.SH \"ARGUMENTS *\"/,$p'`"
else
	first_half="`echo \"$manpage\" | sed '/^.SH \"OPTIONS *\"/,$d'`"
	last_half="`echo \"$manpage\" | sed -n '/^.SH \"OPTIONS *\"/,$p'`"
fi

cat <<EOF
.de Vb
.nf
.ne \\\$1
..
.de Ve

.fi
..
.TH $uppername 1 "`date`" "" "Mercury Programmer's Manual"
.AT 3
.Vb 2
$first_half
.fi
.SH NOTES
.I $name
is one of the development tools
that are part of the Mercury distribution.
.PP
This manual page is limited to a brief summary.
For further information see the Mercury User's Guide.
.Vb 2
$last_half
.fi
.SH AUTHORS
The Mercury team.
.P
See <http://www.mercurylang.org/contact/people.html>.
.SH COPYRIGHT
This program and its documentation are copyright by the University of Melbourne.
They may be copied only under the terms of the GNU General Public License \-
see the file COPYING in the Mercury distribution.
.SH "SEE ALSO"
<http://www.mercurylang.org/information/documentation.html>
.P
The Mercury User's Guide.
.P
The GNU General Public License.
.ex
EOF
