#!@BASH@

#
# multidig 1.1
#
# Copyright (c) 1998-1999 The ht://Dig Group
# Distributed under the terms of the GNU General Public License (GPL)
#   version 2 or later.
# for the ht://Dig search system http://www.htdig.org/
# and the multidig script system http://www.htdig.org/contrib/scripts/
#
# Part of the "multidig script system"
# a system of shell scripts and some modified conf files
# that makes dealing with multiple databases easier for ht://Dig
#
# Syntax:
# multidig [-v]
#
# Performs all the digging, merging and so on needed
#  for indexing and updating multiple db
# Merges multiple databases into ``collected'' db
#

# This is useful for debugging info
if [ "$1" = "-v" ]; then
    verbose=-v
fi

# You may need to set the following:
MULTIDIG_CONF=@CONFIG_DIR@/multidig.conf
source $MULTIDIG_CONF

# Start indexing.
rm $REPORT
for db in `cat $DB_LIST`; do
    echo Digging $db at: `date`
    # What's the conf file for this database?
    CONF=$CONFIG_DIR/$db.conf
    if [ "$1" = "-v" ]; then
	echo "  Indexing $db at: `date`"
    fi
    $BINDIR/htdig -a $verbose -s -c $CONF >>$REPORT
    if [ "$1" = "-v" ]; then
	echo "  Merging $db at: `date`"
    fi
    $BINDIR/htmerge -a $verbose -s -c $CONF >>$REPORT

    if [ "$1" = "-v" ]; then
	echo "  Moving files $db at: `date`"
    fi
    # If you don't have the space for backups, this step can be omitted
    if [ $BACKUPS = "true" ]; then
	cp $DB_BASE/$db/db.docdb      $DB_BASE/$db/db.docdb.bak
	cp $DB_BASE/$db/db.docs.index $DB_BASE/$db/db.docs.index.bak
	# cp $DB_BASE/$db/db.wordlist $DB_BASE/$db/db.wordlist.bak
	cp $DB_BASE/$db/db.words.db   $DB_BASE/$db/db.words.db.bak
    fi

    # Copy the db.docdb file, the .work file is needed for update digs
    cp $DB_BASE/$db/db.docdb.work         $DB_BASE/$db/db.docdb
    # We don't do anything with the db.wordlist file because the
    # .work file is needed for update digs and the non-work file isn't needed
    # cp $DB_BASE/$db/db.wordlist.work    $DB_BASE/$db/db.wordlist
    # These .work files are never used, so let's just keep the active copy
    mv $DB_BASE/$db/db.docs.index.work    $DB_BASE/$db/db.docs.index
    mv $DB_BASE/$db/db.words.db.work      $DB_BASE/$db/db.words.db

    # Make them world readable!
    chmod 644 $DB_BASE/$db/db.docdb     
    chmod 644 $DB_BASE/$db/db.docdb.work
    chmod 644 $DB_BASE/$db/db.docs.index
    # chmod 644 $DB_BASE/$db/db.wordlist 
    chmod 644 $DB_BASE/$db/db.words.db
    if [ "$1" = "-v" ]; then
	echo "  Done with $db at: `date`"
    fi
done
# Now generate the collections by merging their component databases
# We do this in our gen-collect script, so we won't do that here.
$BINDIR/gen-collect $1

if [ "$1" = "-v" ]; then
	echo
	fgrep "htdig:" $REPORT
	echo
	fgrep "htmerge:" $REPORT
	echo
	echo Total lines in $REPORT: `wc -l $REPORT`
fi

# You probably don't need to do this since the script will remove it next
# time it's run. But you can do it anyway
# rm $REPORT
