#! /usr/bin/awk -f

# Warning: this uses gnu-awk features

BEGIN {IN=0}

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

/^%MSKIP/	{SKIP=1;continue}
/^%M/		{SKIP=0}
SKIP==1     {continue}
/^%M$/		{continue}
/^@ignore/	{continue}
/^@end ign/ {continue}

#now perform all the substitutions needed

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

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

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

/^@end table/ {TABLE=0}

# discard other texinfo commands

/^@/		{continue}

# manage comments and '%'

/^%/		{continue}

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

# remove leading blanks

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

# put a .LP at blank lines

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

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


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

/^.TH/		{USELP=1}

			{print > NAME}


