#! /bin/sh

# Copyright (c) 2003-2011
# Distributed Systems Software.  All rights reserved.
# $Id: mkversion 2530 2011-09-23 22:32:19Z brachman $

# Make HTML to appear at the very start ($headfile) and very end ($tailfile)
# of a man page.  This is run after the corresponding [nt]roff man page
# source has been generated.  Each manual page's XML source file inserts
# the HTML in $headfile and $tailfile verbatim via the "htmlcode" processing
# instruction.  See http://www.sagehill.net/docbookxsl/InsertExtHtml.html
#
# XXX There may well be a better way; IMHO the quality of the DocBook
# facilities for creating man pages is uniformly poor.

headfile="version-head.html"
tailfile="version-tail.html"
textvfile="version.txt"

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

date=`date '+%e-%h-%Y' | sed -e "s/  / /"`
version=`echo DACS_VERSION_RELEASE \
            | $xcpp -P -include ../include/dacs_version.h - \
                | sed -e 's/"//g' | tail -1`

if test "$1x" = "-tx"
then
  echo "<bogusxmltag>DACS $version</bogusxmltag>" > $textvfile
  exit 0
fi

manpage="$1"
refname=`echo "$manpage" | tr [a-z] [A-Z] | sed -e 's/\.\(.\)$/(\1)/'`
refsect=`echo "$manpage" | sed -e 's/.*\.\(.*\)/\1/'`

case "${refsect}x" in
"1x") title="DACS Tools and Utilities"; ;;
"3x") title="DACS Functions and Libraries"; ;;
"5x") title="DACS Formats and Conventions"; ;;
"7x") title="DACS Miscellaneous"; ;;
"8x") title="DACS Web Services and CGI"; ;;
   *) echo "Can't grok refsect \"$refsect\""; exit 1; ;;
esac

xmlpage="$1.xml"
tag=`grep '.Id: .*\$$' $xmlpage | head -1`

# headfile="/dev/stderr"
# tailfile="/dev/stderr"
#
# echo $date
# echo $version
# echo $refname
# exit 0

echo "<bogusxmltag>"                 > $headfile
echo '<script language="javascript" type="text/javascript" src="css/js/fontselector.js"></script>'     >> $headfile
echo '<table width="100%"><tr>'     >> $headfile
echo '<td align="left">'            >> $headfile
echo "<b>$refname</b></td>"         >> $headfile
echo '<td align="center">'          >> $headfile
echo "<b>$title</b></td>"           >> $headfile
echo '<td align="right">'           >> $headfile
echo "<b>$refname</b></td>"         >> $headfile
echo '</tr></table>'                >> $headfile
echo "</bogusxmltag>"               >> $headfile

echo "<bogusxmltag>"                 > $tailfile
echo "<!-- Generated from $tag -->" >> $tailfile
echo '<table width="100%"><tr>'     >> $tailfile
echo '<td align="left">'            >> $tailfile
echo "<b>DACS Version $version</b></td>" >> $tailfile
echo '<td align="center">'          >> $tailfile
echo "<b>$date</b></td>"            >> $tailfile
echo '<td align="right">'           >> $tailfile
echo "<b>$refname</b></td>"         >> $tailfile
echo '</tr></table>'                >> $tailfile

echo '<hr/><p>'                  >> $tailfile
echo '<!-- Begin font size selector -->' >> $tailfile
echo '<table width="100%"><tr><td align="left">' >> $tailfile
echo '<span class="set_font"><a href="index.html" title="Table of Contents">Table of Contents</a></span></td>' >> $tailfile
echo '<td align="center"><span class="logo"><a href="http://www.dss.ca"><img src="/css/images/dss-long-14y.png" title="Distributed Systems Software, Inc."></img></a></span></td>' >> $tailfile

echo '<td width="5%" align="right">' >> $tailfile
echo '<div class="fontsize_label" title="Font size selector">Font:</div>' >> $tailfile
echo '</td>' >> $tailfile
echo '<td width="10%" align="left">' >> $tailfile
echo '<!-- NB: must set both left margin and padding to work in all browsers-->' >> $tailfile
echo '<!-- The onFocus code eliminates annoying post-click decoration -->' >> $tailfile
echo '<ul id="fontsizecontainer" class="size02">' >> $tailfile
echo " <li><a href=\"javascript:setFont('0');\" onFocus=\"if(this.blur)this.blur()\" title=\"Smallest text size [0]\"><span>Z</span></a></li>" >> $tailfile
echo " <li><a href=\"javascript:setFont('1');\" onFocus=\"if(this.blur)this.blur()\" title=\"Medium text size [1]\"><span>Z</span></a></li>" >> $tailfile
echo " <li><a href=\"JavaScript:setFont('2');\" onFocus=\"if(this.blur)this.blur()\" title=\"Large text size [2]\"><span>Z</span></a></li>" >> $tailfile
echo " <li><a href=\"JavaScript:setFont('3');\" onFocus=\"if(this.blur)this.blur()\" title=\"Largest text size [3]\"><span>Z</span></a></li>" >> $tailfile
echo '</ul>' >> $tailfile
echo '</td>' >> $tailfile
echo '<td width="3%" align="center">' >> $tailfile
echo "<span class=\"set_font\"><a href=\"javascript:setFont('-');\" onFocus=\"if(this.blur)this.blur()\" title=\"Decrease current font size\">&#x02212;&#x02212;</a></span>" >> $tailfile
echo '</td>' >> $tailfile
echo '<td width="3%" align="center">' >> $tailfile
echo "<span class=\"set_font\"><a href=\"javascript:setFontConfig();\" onFocus=\"if(this.blur)this.blur()\" title=\"Remember current font size\">Set</a></span>" >> $tailfile
echo '</td>' >> $tailfile
echo '<td width="3%" align="center">' >> $tailfile
echo "<span class=\"set_font\"><a href=\"javascript:setFont('+');\" onFocus=\"if(this.blur)this.blur()\" title=\"Increase current font size\">++</a></span>" >> $tailfile
echo '</td></tr></table>' >> $tailfile
echo '<!-- End font size selector -->' >> $tailfile

# Set the initial size shown in the image
echo '<script language="javascript" type="text/javascript">' >> $tailfile
echo "doFontConfig();</script>"  >> $tailfile

echo "</p><small><p><b>$tag</b></p></small>"   >> $tailfile
echo "</bogusxmltag>"               >> $tailfile

exit 0
