#!/bin/sh

# This script takes the file of index entries emitted to stderr by
# troff when processing the output of texi2roff -i and creates up
# to 6 indices (concept, function, keystroke, program, data type,
# variable) as troff input.  If there are no entries for a particular
# type of index, it won't be generated.

# Run the output of this script through nroff or troff with one of
# -me, -mm or -ms.  The macros are necessary to provide pagination.

# Credit for the creative use of tbl to a posting by jpr@dasys1.
# The tricky part with sed is to: (1) protect index entries beginning with
# characters that make troff drop lines and (2) preserve sort order
# for index entries containing typesetting commands or transliterated
# characters.

FILE="$*"
SED=/bin/sed
SQUOTE="'"
DQUOTE='"'

$SED -n "s/^../&\\\\\\&/p" $FILE \
| $SED "
	s/.*/&~+~&/
	:repeat
	s/\\\\f[BIR]\(.*~+~\)/\1/
	s/\\\\f(CW\(.*~+~\)/\1/
	s/\\\\(pd\(.*~+~\)/"$DQUOTE"\1/
	s/\\\\(is\(.*~+~\)/"$SQUOTE"\1/
	s/\\\\(if\(.*~+~\)/\`\1/
	t repeat
" | sort -fd | $SED -e 's/.*~+~//' -e 's/[0-9][0-9]*$/	&/'\
| awk '
	BEGIN	{
		titles["c"] = "Concept Index"
		titles["f"] = "Function Index"
		titles["k"] = "Keystroke Index"
		titles["p"] = "Program Index"
		titles["t"] = "Data Type Index"
		titles["v"] = "Variable Index"
		print ".nf"
		print ".tr \\\(pd\'$DQUOTE'"
		print ".tr \\\(is'$SQUOTE'"
		print ".tr \\\(if`"
	}
	{
		if ($1 != prev) {
			if (prev != "")
				print ".TE"
			print ".bp"
			print ".TS"; print "c s"; print "l r."
			print titles[$1]
			print ""
			prev = $1
		}
		print substr($0, 3, length($0)-2)
	}
	END {
		if (prev != "")
			print ".TE"
	} ' #| tbl

