#!/bin/sh
#
# mkcatindex.sh - Make an HTML page listing all categories in a relation
#
#	syntax: mkcatindex QDDBDIR RELATION KEYFIELD
#
#		The pages are generated in the current directory.
#		footer.printf and header.printf files are also assumed.
#


if [ "$WWWPROJECT" = "" ]; then
  WWWPROJECT=WWWutil
fi

# define where your WWW utils pagemaker scripts live
WWWUTIL="/var/lib/WWWutil"
PAGEMAKE=$WWWUTIL/pagemake

# Where are the WWWutil databases?
WWWDBS="/var/lib/WWWutil/qdDBs"

# the actual DB dir of your WWWutil database:
WWWDB=$WWWDBS/WWWutil

# where your qddb executables (query) live:
QBIN="/usr/local/qddb/bin"

# and where awk is
AWK="/usr/bin/awk"

#
# thats it, the rest is boilerplate
#

QDDBDIR=$1
RELATION=$2
KEYFIELD=$3
targetfile=categories.spider
tmpfile=/tmp/mkcatindex.$$

# header from database is a printf format string for printing two strings:
#printf "`$QBIN/query $WWWDB -sep ' ' \
#	Name "$WWWPROJECT" -print Header`" "$RELATION" \
#		"$RELATION" >> $tmpfile
printf "`cat header.printf`" "$RELATION" "$RELATION" \
	> $tmpfile

echo "<UL>" >>$tmpfile
$PAGEMAKE/categories $QDDBDIR $RELATION $KEYFIELD \
	| $AWK -f $PAGEMAKE/mkcatindex.awk >>$tmpfile
echo "</UL>" >>$tmpfile

#$QBIN/query $WWWDB -sep ' ' Name "$WWWPROJECT" -print Footer >>$tmpfile
cat footer.printf >> $tmpfile


cp $tmpfile $targetfile
rm -f $tmpfile

