#!/bin/sh

# Copyright (c) 2003-2015
# Distributed Systems Software.  All rights reserved.
tag='$Id: mkindex 2787 2015-03-03 21:26:09Z brachman $'

# Make an HTML index page for the DACS man pages
# The version/release information ultimately comes from ../include/dacs-stamp-h
# Takes one argument, a filename prefix
# If the filename prefix is "foo", then foo.head contains prologue HTML,
# and foo.tail contains terminating HTML.
# Output would overwrite foo.html.

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

mkrefindex="./mkrefindex"

# Stoopid incompatibility workarounds...
if test -x /bin/test
then
  xtest="/bin/test"
elif test -x /usr/bin/test
then
  xtest="/usr/bin/test"
else
  xtest="test"
fi

# We need the GNU cpp and this is the easiest way to ensure that
xcpp="gcc -E"

xsort="sort"

use_gnu_sort=0
os_type=`uname -s | tr "[A-Z]" "[a-z]"`
if $xtest "${os_type}" = "linux"
then
  is_linux=1
  use_gnu_sort=1
elif $xtest "${os_type}" = "freebsd"
then
  is_freebsd=1
elif $xtest "${os_type}" = "darwin"
then
  is_darwin=1
  use_gnu_sort=1
elif $xtest "${os_type}" = "sunos"
then
  is_sunos=1
elif $xtest "${os_type}" = "cygwin_nt-5.0"
then
  is_cygwin=1
fi

# The background colour for highlighting the section name
getNAME=./getNAME

if $xtest ! -x ${getNAME}
then
  echo "I need ${getNAME}; try \"make getNAME\""
  exit 1
fi

if $xtest $# != 1
then
  echo "Usage: mkindex file-prefix"
  exit 1
fi

release=`echo DACS_VERSION_RELEASE | $xcpp -P -include ../include/dacs_version.h - | sed -e 's/\"//g' | tail -1`
reldate=`echo DACS_VERSION_DATE | $xcpp -P -include ../include/dacs_version.h - | sed -e 's/\"//g' | tail -1`
relrevid=`echo DACS_VERSION_REVID | $xcpp -P -include ../include/dacs_version.h - | sed -e 's/\"//g' | tail -1`

f="${1}"

sed -e "s/DACS_VERSION_RELEASE/${release}/g" \
	-e "s/DACS_VERSION_REVID/${relrevid}/g" \
	-e "s/DACS_VERSION_DATE/${reldate}/g" < ${f}.head > ${f}.html

# XXX This should be generalized
mkother() {

 desc="- $1"
 s="mod_auth_dacs.html"
 progname=`basename ${s} .html`

 echo "<h2 class='index_title'>"
 $xecho_n '<a name="apache"></a>HTTP Server: Apache'
 echo '</h2>'
 echo '<div class="variablelist"><dl>'
 echo '<dt><span class="term">'
 $xecho_n '<a href="'
 $xecho_n "${s}\">"
 $xecho_n '<tt class="literal">'
 $xecho_n "${progname}"
 echo '</tt></a></span></dt><dd><p>'
 echo "${desc}"
 echo '</p></dd>'
 echo '</dl></div>'
}

trim_initial_text() {

  el_name="$1"

  sed -e "s/.*<${el_name} /<${el_name} /"
}

do_xmlfile_els() {

  filename="$1"
  tmpout="$2"

  while read el
  do
    t=`echo "$el" | sed -e 's/>.*/>/'`
    id=`echo "$t" | sed -e 's/.* id="\(.*\)" .*/\\1/'`
    xrefl=`echo "$t" | sed -e 's/.* xreflabel="\(.*\)">.*/\\1/'`
    b=`basename "${filename}" .xml`
    echo "${b}.html#$id" "$xrefl" >> $tmpout
  done
}
  
do_htmlfile_els() {

  filename="$1"
  tmpout="$2"

  while read el
  do
    t=`echo "$el" | sed -e 's/>.*/>/'`
    id=`echo "$t" | sed -e 's/.* id="\(.*\)" .*/\\1/'`
    xrefl=`echo "$t" | sed -e 's/.* title="\(.*\)">.*/\\1/'`
    echo "${filename}#$id" "$xrefl" >> $tmpout
  done
}
  
# Scan Docbook XML files (the second and remaining arguments) for a given
# element (the first argument), which must include certain attributes
# in a fairly rigid format for ease of processing:
#   <el id="..." xreflabel="...">
# For each file with one or more matching elements, the program outputs
# HTML links:
#   <a href="FILE.html#ID>XREFLABEL</a>
# The idea is that these links will be incorporated into an index table.
do_note_el() {

  which_el= 
  while test -n "$1"
  do
    if test -z "$which_el"
    then
      which_el="<$1 id=\"$2.*>"
    else
      which_el="${which_el}|<$1 id=\"$2.*>"
    fi
    shift 2
  done
  # echo $which_el > /dev/stderr
  tmpout="/tmp/mknotes$$.${2}"
  cp /dev/null $tmpout

  for el_f in *.xml
  do
    # Don't use grep since grep -E isn't universally understood
    # BJB 13-Aug-2010
    sed -e "/${which_el}/p" -e d < "$el_f" | trim_initial_text "${1}" | do_xmlfile_els "$el_f" "$tmpout"
  done

  # This special case is the only HTML source file
  # Required format is: <h3 id="..." xreflabel="...">
  grep "<h3 id=\"$2.*>" < mod_auth_dacs.html \
		| do_htmlfile_els mod_auth_dacs.html "$tmpout"

  # Two column kludge
  echo '<p>'
  echo "<table class=\"note_index\">"

  el="left"
  export el
  row=0

  if $xtest "${use_gnu_sort}" = 1
  then
    sortcmd="${xsort} --key=2"
  else
    sortcmd="${xsort} +1"
  fi

  $sortcmd < "$tmpout" | ./tabtr -c 2 | while true
  do
    read target xrefl
    if $xtest $? -ne 0
    then
      if $xtest $el = "right"
      then
        echo "<td>&nbsp;</td></tr>"
      fi
      break
    fi

    if $xtest $el = "left"
    then
      row=`expr $row + 1`
      if $xtest `expr $row % 2` -eq 1
      then
        echo '<tr class="note_row_odd">'
      else
        echo '<tr class="note_row_even">'
      fi

      echo "<td><a href=\"$target\">$xrefl</a></td>"
      el="right"
    else
      echo "<td><a href=\"$target\">$xrefl</a></td></tr>"
      el="left"
    fi
  done

  echo "</table>"
  echo '</p>'

  rm $tmpout
}

mknotes() {

  echo '<h2 id="annotations" class="index_title">Annotations</h2>'
  echo '<ul>'
  echo '<li class="toc-ent"><a href="#security">Security Notes</a></li>'
  echo '<li class="toc-ent"><a href="#important">Important Notes</a></li>'
  echo '<li class="toc-ent"><a href="#notes">Other Notes</a></li>'
  echo '<li class="toc-ent"><a href="#tips">Tips</a></li>'
  echo '</ul>'

  echo '<h3 class="index_subtitle">'
  echo '<a name="security"></a>Security Notes</h3>'
  do_note_el important security

  echo '<h3 class="index_subtitle">'
  echo '<a name="important"></a>Important Notes</h3>'
  do_note_el important important

  echo '<h3 class="index_subtitle">'
  echo '<a name="notes"></a>Other Notes</h3>'
  do_note_el note note listitem note

  echo '<h3 class="index_subtitle">'
  echo '<a name="tips"></a>Tips</h3>'
  do_note_el tip tip
}

mkvars() {

  echo '<h2 class="index_title">'
  $xecho_n '<a name="variables"></a>Variables'
  echo '</h2>'

  which_el1="<varname id=\"var_.*>"
  which_el2="<envar id=\"var_.*>"
  tmpout="/tmp/mkvars.$$"
  cp /dev/null $tmpout

  for el_f in *.xml
  do
    grep "${which_el1}" < "$el_f" | trim_initial_text "varname" | do_xmlfile_els "$el_f" "$tmpout"
    grep "${which_el2}" < "$el_f" | trim_initial_text "envar" | do_xmlfile_els "$el_f" "$tmpout"
  done

  # Three column kludge
  echo '<p>'
  echo "<table class=\"var_index\">"

  el="1"
  export el
  row=0

  if $xtest "${use_gnu_sort}" = 1
  then
    sortcmd="${xsort} --key=2"
  else
    sortcmd="${xsort} +1"
  fi

  $sortcmd < "$tmpout" | ./tabtr -c 3 | while true
  do
    read target xrefl
    if $xtest $? -ne 0
    then
      if $xtest $el = "2"
      then
        echo "<td>&nbsp;</td><td>&nbsp;</td></tr>"
      elif $xtest $el = "3"
      then
        echo "<td>&nbsp;</td></tr>"
      fi
      break
    fi

    if $xtest $el = "1"
    then
      row=`expr $row + 1`
      if $xtest `expr $row % 2` -eq 1
      then
        echo '<tr class="var_row_odd">'
      else
        echo '<tr class="var_row_even">'
      fi
    fi

    echo "<td><a href=\"$target\">$xrefl</a></td>"
    el=`expr $el + 1`
    if $xtest $el = "4"
    then
      el=1
      echo "</tr>"
    fi
  done

  echo "</table>"
  echo '</p>'

  rm $tmpout
}

mkdirectives() {

  echo '<h2 class="index_title">'
  $xecho_n '<a name="directives"></a>Configuration Directives'
  echo '</h2>'

  tmpout="/tmp/mkdirectives.$$"
  cp /dev/null $tmpout

  for el_f in *.xml
  do
    dl=`grep '<xref linkend=' "${el_f}" | sed -e '/endterm/d' -e 's/.*linkend="//' -e 's/".*//'`
    b=`basename "${el_f}" .xml`
    for d in ${dl}
    do
      text=`echo $d | sed 's/^r_//'`
      echo "${b}.html ${d} ${text}" >> $tmpout
    done
  done

  # Three column kludge
  echo '<p>'
  echo "<table class=\"var_index\">"

  el="1"
  export el
  row=0

  if $xtest "${use_gnu_sort}" = 1
  then
    sortcmd="${xsort} --key=3"
  else
    sortcmd="${xsort} +2"
  fi

  ${sortcmd} < "$tmpout" | ./tabtr -c 3 | while true
  do
    read fname dname tname
    if $xtest $? -ne 0
    then
      if $xtest $el = "2"
      then
        echo "<td>&nbsp;</td><td>&nbsp;</td></tr>"
      elif $xtest $el = "3"
      then
        echo "<td>&nbsp;</td></tr>"
      fi
      break
    fi

    if $xtest $el = "1"
    then
      row=`expr $row + 1`
      if $xtest `expr $row % 2` -eq 1
      then
        echo '<tr class="var_row_odd">'
      else
        echo '<tr class="var_row_even">'
      fi
    fi

    echo "<td><a href=\"$fname#$dname\">$tname</a></td>"
    el=`expr $el + 1`
    if $xtest $el = "4"
    then
      el=1
      echo "</tr>"
    fi
  done

  echo "</table>"
  echo '</p>'

  rm $tmpout
}

mkdtd() {

 dir="$1"
 url_path_prefix="$2"

 echo '<h2 class="index_title">'
 $xecho_n '<a name="dtd-xsd"></a>DTDs'
 echo '</h2>'

 echo '<p class="comment">These XML DTD skeletons are used only to help'
 echo 'document information used by DACS.</p>'

 comma=""
 echo '<p class="filename_index">'
 for dtd in ${dir}/*.dtd
 do
   d=`basename ${dtd} ${dir}`
   $xecho_n "${comma}<a href=\"${url_path_prefix}/$d\">$d</a>"
   comma=", "
 done
 echo '</p>'
}

mksect() {

 sect="$1"
 title="$2"

 echo "<h2 class='index_title'>"
 $xecho_n "<a name=\"sect$1\"></a>Section "
 echo "${sect}: <small>${title}</small>"
 echo '</h2>'

# echo '<div class="variablelist"><dl>'
echo '<div><dl class="dlxxx">'

 for i in *.${sect}
 do
   if $xtest "$i" = "*.${sect}"; then
     continue
   fi

   s="${i}.html"

# Get and strip the man page section: e.g., foo(1) becomes foo
   title=`${getNAME} -t ${i}`
   manname=`echo "${title}" | cut -f 1 -d " "`
   progname=`expr "${manname}" : "\(.*\)(.*"`
   desc=`echo "${title}" | sed -e 's/.* - /- /'`

# Hyphens in the title have been escaped; unescape them
# Also, embedded font changes need to be removed
# Some implementations of sed (or perhaps /bin/sh?) handle the form feed
# character differently in this context
# Mark up "DACS", crudely, in the usual way
   desc=`echo "${desc}" | sed -e 's/\\\\-/-/g' -e 's/\\\\fB//g' -e 's/\\\\fR//g' -e 's/.//g' | sed -e 'sZDACSZ<strong class="command">DACS</strong>Zg'`

#   echo '<dt><span class="term">'
   echo '<dt class="dtxxx"><span>'
   $xecho_n '<a href="'
   $xecho_n "${s}\">"
   $xecho_n '<tt class="literal">'
   $xecho_n "${progname}"
#   echo '</tt></a></span></dt><dd><p>'
   echo '</tt></a></span></dt><dd class="ddxxx"><p>'
   echo "${desc}"

   # Insert manual page section index
   $mkrefindex "$s"

   echo '</p></dd>'

 done
 echo '</dl></div>'
}

mksect 1 "Tools and Utilities" >> ${f}.html
mksect 3 "Functions and Libraries" >> ${f}.html
mksect 5 "Formats and Conventions" >> ${f}.html
mksect 7 "Miscellaneous" >> ${f}.html
mksect 8 "Web Services and CGI" >> ${f}.html
mkother "Apache/DACS authentication and authorization module" >> ${f}.html
mknotes >> ${f}.html
mkvars >> ${f}.html
mkdirectives >> ${f}.html

mkdtd "../dtd-xsd" "/dtd-xsd" >> ${f}.html

now=`date`
echo '</div>' >> "${f}.html"
echo '<div id="credit-tag">' >> "${f}.html"
echo '<hr width="66%"><center>' >> "${f}.html"
echo "This documentation was created on ${now} using " >> "${f}.html"
echo '<a href="http://docbook.sourceforge.net/">DocBook</a> and' >> "${f}.html"
echo '<a href="http://xmlsoft.org/XSLT/">libxslt</a>.' >> "${f}.html"
echo '</center></div>' >> "${f}.html"

echo "<!-- Generated by: $tag -->" >> "${f}.html"

sed -e "s/DACS_VERSION_RELEASE/${release}/g" \
	-e "s/DACS_VERSION_DATE/${reldate}/g" \
	-e "s/DACS_VERSION_REVID/${relrevid}/g" \
	< "${f}.tail" >> "${f}.html"

# cat "${f}.tail" >> "${f}.html"

exit 0
