#!/usr/bin/perl
# $Id: $
# Written by Adrian Mariano, additional features by Eric Backus

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

$version="1.01";

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

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 probably get better\n");
printf(".\\\"results with the original file.\n.\\\"\n");

while(<>)
{
    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 (/^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 (/\@ifhtml/) { $html=1; next; }
    if (/\@end ifhtml/) { $html=0; next; }
    if (!$doman && ($ignore || $html || $title || $tex)) { next; }

    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/\@footnote\{([^}]*)}/[$1]/g;
    s/\@minus\{}/-/g;
    s/\@copyright\{}/(C)/g;
    s/\@noindent//;
    s/\@\{/{/g;
    s/\@}/}/g;
    s/\@\@/@/g;
    s/\'\'/\"/;
    s/\`\`/\"/;
    s/---/--/;

    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 (/\@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;
    }

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

    if (/\@example/) { printf("%s.nf\n", $manprefix); $example=1; next; }
    if (/\@end example/) { 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");
	$justdidlp=1;
	next;
    }

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

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

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

