#!/usr/bin/perl -w

# Postprocess grib_api's help messages, which are almost man pages

my $name = shift @ARGV;

sub escapeslashes($)
{
	my $str = shift;
	$str =~ s/-/\\-/g;
	return $str;
}

print ".TH ".uc($name)." \"1\" \"April 2009\" \"".lc($name)."\" \"User Commands\"\n";

while (<>)
{
	if (/^[A-Z]/)
       	{
		# Sections
		s/\s+(.+)$/\n$1/;
		s/^/.SH /;
		print;
	} elsif (/^\t-/) {
		# Options
		print ".TP\n";
		s/^\s*//;
		s/(\S+)/"\\fB".escapeslashes($1)."\\fR"/e ;
		print;
	} elsif (/^\t/) {
		s/^\s*//;
		print;
	} else {
		print;
	}
}

print ".SH AUTHOR\n";
print "This manpage has been autogenerated by Enrico Zini <enrico\@debian.org>";
print "from the command line help of $name.\n";

