#!/bin/sh

# rundig_full_reindex
# Runs htdig to build index data from scratch
# requires command line parameter: the htdig conf file w/ full path

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

# Setup temp dir location for work files
TMPDIR=$DBDIR
export TMPDIR

usage ()
{
	echo "Usage: use Mailman's cron/nightly_htdig_just_update to call this script"
	echo "Example: /dh/mailman/INSTANCE/cron/nightly_htdig_just_update -v LIST_NAME"
	exit
}

# Get conf file and dir location
# For Reference:
#   /dh/mailman/fritz/archives/private/zaralug-slgt.org/htdig/zaralug-slgt.org.conf
if [ "$#" != "1" ] ; then
	echo "List conf file not passed in!"
	usage
fi
conffile="$1"
if [ ! -f $conffile ] ; then
	echo "List conf file specified does not exist as a file!"
	usage
fi

mm="`dirname $conffile|xargs dirname|xargs dirname|xargs dirname|xargs dirname|xargs basename`"
list="`basename $conffile|sed s/.conf//`"
workdir="/dh/mailman/$mm/archives/htdig-db/$list"

# Setup log file
logdate=`date +%Y%m%d`
logdir="/dh/mailman/$mm/logs"
logfile="$logdir/htdig_monthly_reindex.$logdate.log"

# remove existing work files so index is rebuilt from scratch
rm $workdir/db.docdb.work
rm $workdir/db.docs.index.work
rm $workdir/db.excerpts.work
rm $workdir/db.words.db.work
rm $workdir/db.words.db.work_weakcmpr

# index html archives via http
echo $(date +"%D %T") $HOSTNAME "mailman" $mm $list "htdig indexing started" >> $logfile
# c: conf file, v: verbose, a: use work files, s: statistics at end
$BINDIR/htdig -a -s -c $conffile >> $logfile
echo $(date +"%D %T") $HOSTNAME "mailman" $mm $list "htdig indexing completed" >> $logfile

# create document index and word database from htdig's results
echo $(date +"%D %T") $HOSTNAME "mailman" $mm $list "htmerge started" >> $logfile
$BINDIR/htmerge -a -s -c $conffile >> $logfile
echo $(date +"%D %T") $HOSTNAME "mailman" $mm $list "htmerge complete" >> $logfile

# Make work files active by copying them on top of the active/old files.
# htdig&htmerge update the .work files only and do not touch the live files.
cp -u $workdir/db.docdb.work $workdir/db.docdb
cp -u $workdir/db.docs.index.work $workdir/db.docs.index
cp -u $workdir/db.excerpts.work $workdir/db.excerpts
cp -u $workdir/db.words.db.work $workdir/db.words.db
cp -u $workdir/db.words.db.work_weakcmpr $workdir/db.words.db_weakcmpr

# Remove logs over 6 months old
/usr/bin/find $logdir -name 'htdig_monthly_reindex.*' -type f -mtime +180 -delete ;
