#!/usr/bin/perl
# $Id: texi2man,v 1.2 2003/11/28 21:22:16 mann Exp $
# Written by Adrian Mariano, additional features by Eric Backus,
# yet more features by Tim Mann.

# Script to translate a texinfo file into an nroff manual page.

$version="1.01.tpm1";

$html=0;
$ignore=0;
$tex=0;
$doman=0;
$title=0;
$diditem=0;
$justdidlp=1;
$noman=0;
$info=0;
$manprefix="";
$args=($#ARGV < 0) ? "stdin" : "@ARGV";
$itemxcomma=0;

printf(".\\\"Do not edit this file.  It was created from %s\n", $args);
printf(".\\\"using texi2man version %s on %s", $version, `date`);
printf(".\\\"If you want a typeset version, you will get better\n");
printf(".\\\"results with the original file.\n.\\\"\n");

sub parse
{
    if (s/\@c man //) { print; next; }
    if (/\@c noman/) { $noman=1; next; }
    if (/\@c end noman/) { $noman=0; next; }
    if ($noman) { next; }

    if (/\@c ifman\s*(.*)/) { $doman=1; $manprefix = $1; next; }
    if (/\@c end ifman/) { $doman=0; $manprefix = ""; next; }

    if (/^\\input/) { next; }
    if (/^\*/) { next; }
    if (/^INFO-DIR-SECTION/) { next; }
    if (/^START-INFO-DIR-ENTRY/) { next; }
    if (/^END-INFO-DIR-ENTRY/) { next; }

    if (/\@titlepage/) { $title=1; next; }
    if (/\@end titlepage/) { $title=0; next; }
    if (/\@tex/) { $tex=1; next; }
    if (/\@end tex/) { $tex=0; next; }
    if (/\@ignore/) { $ignore=1; next; }
    if (/\@end ignore/) { $ignore=0; next; }
    if (/\@ifinfo/) { $info=1; next; }
    if (/\@end ifinfo/) { $info=0; next; }
    if (/\@ifhtml/) { $html=1; next; }
    if (/\@end ifhtml/) { $html=0; next; }
    if (/\@ifnothtml/) { next; }
    if (/\@end ifhtml/) { next; }
    if (/\@iftex/) { $tex=1; next; }
    if (/\@end iftex/) { $tex=0; next; }
    if (/\@ifnottex/) { next; }
    if (/\@end iftex/) { next; }
    if (!$doman && ($ignore || $html || $title || $tex || $info)) { next; }

    s/\\/\\\\/g;
    s/\@-/\\-/g;
    s/\@cite\{([^}]*)}/\`$1\'/g;
    s/\@code\{([^}]*)}/\`$1\'/g;
    s/\@email\{([^}]*)}/\`$1\'/g;
    s/\@file\{([^}]*)}/\`$1\'/g;
    s/\@kbd\{([^}]*)}/\`$1\'/g;
    s/\@samp\{([^}]*)}/\`$1\'/g;
    s/\@url\{([^}]*)}/\`$1\'/g;
    s/\@dfn\{([^}]*)}/\"$1\"/g;
    s/\@key\{([^}]*)}/<$1>/g;
    s/\@emph\{([^}]*)}/\\fI$1\\fR/g;
    s/\@strong\{([^}]*)}/\\fB$1\\fR/g;
    s/\@var\{([^}]*)}/\U$1\E/g;
    s/\@sc\{([^}]*)}/\U$1\E/g;
    s/\@w\{([^}]*)}/$1/g;
    s/\@pxref\{([^}]*)}/See \\fI$1\\fR/g;
    s/\@xref\{([^}]*)}/See \\fI$1\\fR/g;
    s/\@ref\{([^}]*)}/\\fI$1\\fR/g;
    s/\@footnote\{([^}]*)}/[$1]/g;
    s/\@minus\{}/-/g;
    s/\@copyright\{}/(C)/g;
    s/\@noindent//;
    s/\@\{/{/g;
    s/\@}/}/g;
    s/\@\@/@/g;
    s/\\'\\'/\"/g;
    s/\\`\\`/\"/g;
    s/---/ -- /g;

    s/\@value\{([^\s]+)}/$value{$1}/eg;
    if (/\@set\s+([^\s]+)\s+(.*)$/) { $value{$1} = $2; next; }
    if (/\@clear\s+([^\s]+)\s+(.*)$/) { delete $value{$1}; next; }

    if ($itemxcomma) {
        # If multiple items have the same description, put the items
        # on one line, separated by commas
        if (/\@itemx (.*)/) { printf(", $1"); $diditem=1; next; }
        elsif ($diditem) { printf("\n"); $diditem=0; }
	if (/\@item (.*)/) {
	    printf("%s.TP\n%s.B $1", $manprefix, $manprefix);
	    $diditem=1;
	    next;
	}
    } else {
        # If multiple items have the same description, put the items
        # on separate lines
	if (/\@item (.*)/) {
	    printf("%s.TP\n%s.B $1\n", $manprefix, $manprefix);
	    next;
	}
	if (/\@itemx (.*)/) {
	    printf("%s.PD 0\n%s.TP\n%s.B $1\n%s.PD\n",
		   $manprefix, $manprefix, $manprefix, $manprefix);
	    next;
	}
    }

    if (s/\@chapter (.*)/.SH \U$1\E/)
    {
	printf("%s%s", $manprefix, $_);
	$justdidlp=1;
	next;
    }
    if (s/\@unnumbered (.*)/.SH \U$1\E/)
    {
	printf("%s%s", $manprefix, $_);
	$justdidlp=1;
	next;
    }
    if (s/\@section (.*)/$1/)
    {
	printf("%s.SS %s", $manprefix, $_);
	$justdidlp=1;
	next;
    }

    if (/\@example/) {
	printf("%s.nf\n", $manprefix);
	$manprefix = $manprefix . "    ";
	$example=1;
	next;
    }
    if (/\@end example/) {
	$manprefix = substr($manprefix, 0, -4);
	printf("%s.fi\n", $manprefix);
	$example=0;
	next;
    }

    if (/\@display/) { printf("%s.nf\n", $manprefix); $example=1; next; }
    if (/\@end display/) { printf("%s.fi\n", $manprefix); $example=0; next; }
    if (/\@format/) { printf("%s.nf\n", $manprefix); $example=1; next; }
    if (/\@end format/) { printf("%s.fi\n", $manprefix); $example=0; next; }
    if (/\@smallexample/) { printf("%s.nf\n", $manprefix); $example=1; next; }
    if (/\@end smallexample/) { printf("%s.fi\n", $manprefix); $example=0; next; }
    if (!$example && /^\s*$/ && !$doman)
    {
	if ($justdidlp) { next; }
#	printf(".PP\n");
	printf("\n");
	$justdidlp=1;
	next;
    }
    if (/\@end table/) {
	printf("%s.PP\n", $manprefix);
	$justdidlp=1;
    }

    if (/\@include (.*)/) {
	my $INCL;
        open($INCL, $1) or open($INCL, "../$1") or die "$1: $!";
        while (<$INCL>) {
            parse();
        }
        next;
    }

    if (/^\@/) { next; }

    printf("%s%s", $manprefix, $_);

    if (!$doman) { $justdidlp=0; }
}

while(<>)
{
    parse();
}
