#!/bin/sh
#
# /usr/local/sbin/localrundig
# Copyright 2001-2002 Jonas Smedegaard <dr@jones.dk>
#
# $Id: localrundig,v 1.4 2002-03-07 16:22:51 jonas Exp $
#
# Create a search database for ht://Dig.
# Based on upstream sample script (but completely rewritten by now)
#

DBDIR=/var/lib/htdig
COMMONDIR=/etc/htdig
BINDIR=/usr/bin

# To help debug problems, try running rundig -vvv
# This will add lots of debugging output to all of the programs
stats= opts= alt= init=
for arg
do
    case "$arg" in
    -a)  alt="$arg" ;;
    -s)  stats="$arg" ;;
    -i)  init="$arg" ;;
    *)   opts="$opts $arg" ;;	# e.g. -v or -c config
    esac
done

config=`echo "$opts"|sed 's/^.*-c[[:space:]]\+\([[:alnum:]/._-]\+\).*$/\1/'`
test -n "$config" || config=/etc/htdig/htdig.conf
locale=`grep '^locale:' $config | sed -e 's/.*:[[:space:]]*\([[:alnum:]_-]\+\).*/\1/'`
test -n "$locale" || locale=en_US
search_object=`grep '^search_object:' $config | sed -e 's/.*:[[:space:]]*\([[:alnum:]_-]\+\).*/\1/'`

LOCALDBDIR=/var/local/lib/htdig/db/$search_object
LOCALLANGSTATICDIR=/usr/local/lib/htdig/lang/$locale
LOCALLANGDIR=/var/local/lib/htdig/lang/$locale

test -d $LOCALDBDIR || mkdir $LOCALDBDIR

#
# Set the TMPDIR variable if you want htmerge to put files in a location
# other than the default.  This is important if you do not have enough
# disk space for the big sort that htmerge runs.  Also, be aware that
# on some systems, /tmp is a memory mapped filesystem that takes away
# from virtual memory.
#
TMPDIR=${TMPDIR:-$LOCALDBDIR}
export TMPDIR

$BINDIR/htdig $init $opts $stats $alt
LC_COLLATE=C $BINDIR/htmerge $opts $stats $alt
case "$alt" in
-a)
  ( cd $LOCALDBDIR && test -f db.docdb.work &&
    for f in *.work
    do
        mv -f $f `basename $f .work`
    done ) ;;
esac
# If you're low on disk space and you don't mind completely reindexing
# every time you run this script, you can always
# rm $DBDIR/db.wordlist
# OR
# If you'd rather run update digs all the time with the minimal databases
# Keep only the following files (and don't call htdig with -i):
# db.docdb, db.docdb.work, db.docs.index, db.wordlist.work, db.words.db
$BINDIR/htnotify $opts

#
# Create the endings and synonym databases if they don't exist
# or if they're older than the files they're generated from.
# These databases are semi-static, so even if pages change,
# these databases will not need to be rebuilt.

case "$locale" in
en_*)
    $BINDIR/htfuzzy $opts metaphone ;;
*)
    $BINDIR/htfuzzy $opts soundex ;;
esac

if [ "`ls -t $LOCALLANGSTATICDIR/dict.0 $LOCALLANGDIR/word2root.db 2>/dev/null | sed 1q`" = \
	"$LOCALLANGSTATICDIR/dict.0" ]
then
    $BINDIR/htfuzzy $opts endings
fi

if [ "`ls -t $LOCALLANGSTATICDIR/synonyms $LOCALLANGDIR/synonyms.db 2>/dev/null | sed 1q`" = \
	"$LOCALLANGSTATICDIR/synonyms" ]
then
    $BINDIR/htfuzzy $opts synonyms
fi