#! /usr/bin/awk -f
# Copyright (c) 1998-2001 Alessandro Rubini

BEGIN {IN=0}

/^%MANPAGE END/ {IN=0; next}
/^%MANPAGE/     {IN=1; USELP=NEEDLP=INTABLE=0; NAME=$2; next}
IN==0           {next}

/^%MSKIP/	{SKIP=1;next}
/^%M/		{SKIP=0}

/^@menu/        {SKIP=1;next}
/^@end menu/    {SKIP=0;next}

SKIP==1         {next}
/^%M$/		{next}
/^@ignore/	{next}
/^@end ign/     {next}

#now perform all the substitutions needed

	        { gsub("^%M ?",""); }

# Use gensub for converting tags: itz Sep 30 1998
#
# However, the gensub function is gawk-specific, and we want things
# to work with original-awk too (for portability).
# Therefore, use a normal gsub, even though it's a subobptimal solution
# as it may step in extra braces. The good solution will be piping to sed,
# or match, extract subesxpression, replace, reinsert -- bleah...
# (ARub, Oct 10 2000)
/@b\{/ {
  #$0 = gensub(/@b\{([^}]+)\}/, "\\\\fB\\1\\\\fP","g");
  gsub("@b\{","\\fB");
  gsub("\}","\\fP");
} 

/@var\{/ {
  #$0 = gensub(/@var\{([^}]+)\}/, "\\\\fI\\1\\\\fP","g");
  gsub("@var\{","\\fB");
  gsub("\}","\\fP");
} 

/@(samp|code|file)\{/ {
  #$0 = gensub(/@(samp|code|file)\{([^}]+)\}/, "`\\2'","g");
  gsub("@(samp|code|file)\{","");
  gsub("\}","");
}


/@xref\{.*\}\./ {
  gsub(/@xref\{.*\}\./,"");
}

/@ref\{.*\}/ {
  gsub("@ref\{","");
  gsub("\}","");
}

/@\*/ {
  gsub(/@\* */,"\n.br\n");
}

/@[a-z]+\{/      {
                gsub("@[a-z]+\\{","");
		gsub("}","");
		}

/^@table/       { TABLE=1; }
/^@itemize/     { TABLE=1; next}

/^@item/        {
	        gsub("^@item *","");
		printf ".TP\n%s\n",$0 > NAME;
		NEEDLP=0; next;
		}

/^@end table/   {TABLE=0}
/^@end itemize/ {TABLE=0}

# discard other texinfo commands

/^@/		{next}

# manage comments and '%'

/^%/		{next}


		{
		gsub("[^\\\\]%.*$","");
		gsub("\\%","%");
		}


# remove leading blanks

/^[ \t]/        {gsub("^[ \t]","");}

# put a .LP at blank lines

/^.nf/          {USELP=0}
/^.fi/          {USELP=1}

/^$/	        {if (USELP) {NEEDLP++; next;} }


/./	        { if (NEEDLP) { printf "\n.LP\n" > NAME; NEEDLP=0; } }

/^.TH/	        {USELP=1}

# Escape single slashes (e.g. in documentation for `-l' command line option)

		{gsub("\\\\ ", "\\\\ ");}

                {gsub("~", "~~");}

	        {print > NAME}


