#! /bin/sh

# Copyright (c) 2003-2015
# Distributed Systems Software.  All rights reserved.
tag='$Id: mkrefindex 2786 2015-02-25 20:54:10Z brachman $'

# Usage: mkrefindex file.html [...]
#
# Locate headings within manual page HTML and emit a simple index for the page.
# We assume the very particular HTML format that is generated by docbook:
#   blah blah <a name="tag"></a><h[345]>Title Words</h[345]>blah blah
# This is sensitive to exactly how HTML is generated from XML source.
# For instance, an "<important>" or "<note>" element that immediately
# follows a "<refsect*>" must be within a "<para>", otherwise the
# section may not have an index entry.  Also, one "<refsect*>" cannot
# immediately follow another without having something in between.
# The "<title>" element should appear on the same line as the "<refsect*>"
# and the "<title>" line should not be split.
#
# Yes, this is a quick & dirty hack but it works well enough for now without
# having to change the original markup.  The correct solution is to change
# the XML markup to generate the right thing (dunno if "<indexterm>" can do
# what we need, if a new element must be created, of if a special anchor name
# syntax will do).

xecho_n="../src/conftools/echo_n-sh"

getref() {
  file="$1"

css_h3="index_h3"
css_h4="index_h4"
css_h5="index_h5"

did_init=
while true
do
  read line
  if test -z "$line"
  then
    break
  fi

  if test -z "$did_init"
  then
    $xecho_n "<br>["
    did_init="yes"
  else
    $xecho_n " // "
  fi

  # Identify which heading type this is
  m3=`expr "$line" : ".*<h3>"`
  m4=`expr "$line" : ".*<h4>"`
  m5=`expr "$line" : ".*<h5>"`

  eline=`echo $line | sed -e 's/\(.*\)<\/h[345]>.*/\1/'`

# <a name="about_dacs"></a><h3>About DACS</h3>
# <a name="about_manpages"></a><h3>About the Manual Pages</h3>
# dacs_auth_transfer:
# <a name="idp65757488"></a><h3>The Identity Transfer Protocol</h3><div class="refsect3"><a name="idp65766320"></a><h4>Overview</h4>

  if test "$m3" != 0
  then
    css_class="index_h3"
    title=`echo $eline | sed -e 's/.*<h3>//' -e 's/.*<\/a>//'`
    tag=`echo $eline | sed -e 's/.*<a name=\"\(.*\)\"><\/a><h3>.*/\1/'`
  elif test "$m4" != 0
  then
    css_class="index_h4"
    title=`echo $eline | sed -e 's/.*<h4>//' -e 's/.*<\/a>//'`
    tag=`echo $eline | sed -e 's/.*<a name=\"\(.*\)\"><\/a><h4>.*/\1/'`
  elif test "$m5" != 0
  then
    css_class="index_h5"
    title=`echo $eline | sed -e 's/.*<h5>//' -e 's/.*<\/a>//'`
    # H5 changes the order...
    # blah blah <h5><a name="tag"></a>Title Words</a></h5>blah blah
    tag=`echo $eline | sed -e 's/.*<h5><a name=\"\(.*\)\"><\/a>.*/\1/'`
  fi

  $xecho_n "<a class=\"$css_class\" href=\"$file#$tag\">$title</a>"
done

if test -n "$did_init"
then
  $xecho_n "]"
fi
}

dofile() {
  file="$1"

  grep '<h[345]>' "$file" | getref "$file"
}


for i
do
  dofile "$i"
done

