#! /usr/bin/awk -f

# Warning: this uses gnu-awk features

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 ?",""); }

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

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


