#!/usr/local/bin/perl

$ignore=0;
$info=0;
$doman=0;
$title=0;
$diditem=0;
$justdidlp=1;
$line=0;
$noman=0;
while(<>){
  $line++;
  if (/\@c noman/) {  $noman=1; next;}
  if (/\@c end noman/) { $noman=0; next;}
  if ($noman) {next;}
  if (/\@itemx (.*)/) {  printf(", $1"); $diditem=1;next;}
  elsif ($diditem) { printf("\n");  $diditem=0;}
  if (/\@item (.*)/) { printf(".TP\n.B $1"); $diditem=1; next;}
  if (/^\\input/) {next;}
  if (/^\*/) {next;}
  if (/\@titlepage/) { $title=1; next;}
  if (/\@end titlepage/) {$title=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 (/\@c ifman (.*)/) { $doman=1; $manprefix = $1;next;}
  if (/\@c end ifman/) { $doman=0;next;}
  if ($doman) { printf("%s%s",$manprefix,$_); next;}    
  if ($ignore ||  $info || $title) {next;}
  s/\@code\{([^}]*)}/\\'$1'/g;
  if (s/\@chapter (.*)/.SH \U$1\E/) { print;$justdidlp=1;next;}
  if (s/\@c man //) {print;next;}
  s/\@samp\{([^}]*)\}/\\'$1'/g;
  s/\@kbd\{([^}]*)}/\\'$1'/g;
  s/\@var\{([^}]*)}/\U$1\E/g;
  s/\@emph\{([^}]*)}/$1/g;
  s/\@file\{([^}]*)}/'$1'/g;
  s/\@noindent//;
  if (/\@example/) { printf(".nf\n"); $example=1; next;}
  if (/\@end example/) {printf(".fi\n"); $example=0; next;}
  if (/^\@/) { next; }
  if (!($example) && /^\s*$/) { 
     if ($justdidlp)  {next;}
     printf(".LP\n"); $justdidlp=1; next;
  }
  print;
#  printf("%d: %s",$line,$_);
  $justdidlp=0;
}
  
  





