#! /usr/bin/awk -f
# Copyright (C) Alessandro Rubini 1995

# Warning: this uses gnu-awk features

BEGIN {IN=MENU=NODELINE=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}
SKIP==1         {next}
/^%M$/		{next}
/^@ignore/	{next}
/^@end ign/     {next}
/^@node/        {NODELINE=1; next}
NODELINE==1     {NODELINE=0; next} # skip chapter and so on
/^@cindex/      {next}

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

#now perform all the substitutions needed


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

/@[a-z]+{/      { # texinfo macros
                gsub("@xref{.*}\.",""); # xrefs are removed altogether
                gsub("@[a-z]+{","");
		gsub("}","");
		}

/^@deftypefun/  { # functions
                gsub(/^@def[a-z]* */,"");
                printf ".TP\n%s\n",$0 > NAME;
                NEEDLP=0; next;
                }

/^@table/       { TABLE=1; }

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

/^@end table/   {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}

	        {print > NAME}


