#!/bin/bash
# A small script to let htdig completely reindex the database


if ! lockfile-create /var/run/htdig.cron; then
    # Another htdig indexing cronjob is already running
    exit 0
fi

lockfile-touch /var/run/htdig.cron &
# Save the PID of the lockfile-touch process
BADGER="$!"

RUNSCRIPT="/usr/bin/rundig"
FILEREF="/etc/default/htdig"
REFRESH="/var/lib/htdig/full_refresh_needed"
LIBDIR="/var/lib/htdig"

if [ -x $RUNSCRIPT ]; then
	if [ -r $FILEREF ]; then
		RUN=$(awk '/^run_htnotify/ {print $3}' $FILEREF)
		if [ "$RUN" = "true" ]; then
			if [ -d $LIBDIR ]; then
				if [ ! -f $REFRESH ]; then
					touch $REFRESH
				fi
			fi
		fi
	fi
fi

kill "${BADGER}"
lockfile-remove /var/run/htdig.cron
