#!/bin/sh
#cut at given keyword
keyword()
{
	# I changed "0" to "\$" because my sed
	# doesn't want to understand "0".
	# -- Zoltan
	sed "1,/^%$1/ d;/^%/,\$ d"
}
menus()
{
	# I changed "0" to "\$" because my sed
	# doesn't want to understand "0".
	# -- Zoltan
	sed "1,/^%menus/ d;/^%endmenus/,\$ d"
}

#convert xshl tags into texinfo
totexinfo1()
{
	sed 's/@/@@/g
	s/^[ 	]*//g
	s/[ 	]*$//g
	s/[ 	][ 	]*/ /g
	s/{/@{/g
	s/}/@}/g
	s/^%%%*//g
	s/^\%[a-z_0-9 ]*$//g
	'
}
totexinfo()
{
	sed '
	s/<submenu>//g
	s/<noprint>/@ifinfo/g
	s/<\/noprint>/@end ifinfo/g
	/<menuitems>/, /<\/menuitems>/ s/<p><a \([^>]*\)>\([^<]*\)<\/a>/* \1::	\2/g
	s/<menuitems>/@menu /g
	s/<\/menuitems>/@end menu /g
	s/<tutor [^>]*>//g
	s/<\/tutor>//g
	/<notex>/,/<\/notex>/ d
	s/--/---/g
	s/^<p><tt>$/\
\
@example/g
	s/^<\/tt><p>$/@end example\
\
/g
	s/<math>/@math{/g
	s/<\/math>/}/g
	s/<tt>/@code{/g
	s/<\/tt>/}/g
	s/<emph>/@emph{/g
	s/<\/emph>/}/g
	s/^<p>$//
	s/<p>/\
\
/g
	s/<dl>/@table @strong/g
	s/<\/dl>/@end table/g
	s/<dt>/@item /g
	s/<br>//g
	s/^<dd>//g
	s/<dd>/\
/g
	s/<node \([^,]*\),\([^,]*\),\([^,]*\),\([^>]*\)>/@node \1, \3, \2, \4/g
        s/<a \([^>]*\)>\(.*\)<\/a>\./\2. @xref{\1}./g
        s/<a \([^>]*\)>\(.*\)<\/a>/\2/g
	'
}
#convert headers to make new chapter
chapter()
{
	sed '1,/<\/head>/ s/<head>/@chapter /g
	s/<\/head>//g
	s/<head>/@section /g
	'
}
menu()
{
	sed '
	s/<p><emph>Syntax<\/emph>:(\([^)]*\))/@defun \1 \
@end defun/g
	s/<main><head>/@appendix /g
	s/<menuhead><head>/@appendixsec /g
	s/<head>/@appendixsubsec /g
	s/<center>//g
	s/<\/center>//g
	s/<\/head>//g
	s/<\/main>//g
	s/<\/menuhead>//g
	'
	#s/<main><head>/@chapter /g
	#s/<menuhead><head>/@section /g
	#s/<head>/@subsection /g
	#s/<head>/@unnumberedsubsec /g
}
section()
{
	sed '1,/<\/head>/ s/<head>/@section /g
	s/<\/head>//g
	s/<head>/@subsection /g
	'
}
appendix()
{
	sed '1,/<\/head>/ s/<head>/@appendix /g
	s/<\/head>//g
	s/<head>/@subsection /g
	'
}
case "$1" in
chapter) keyword $2 | totexinfo1 | totexinfo | chapter ;;
section) keyword $2 | totexinfo1 | totexinfo | section ;;
appendix) keyword $2 | totexinfo1 | totexinfo | appendix ;;
menus) menus | totexinfo1 | menu | totexinfo ;;
esac
