#!/sbin/runscript
# Copyright 1999-2003 Gentoo Technologies, Inc.
# Distributed under the terms of the GNU General Public License, v2 or later
# $Header: /var/cvsroot/gentoo-x86/app-text/dictd/files/1.8.0/dictd,v 1.2 2003/02/14 22:39:25 vapier Exp $

# NB: Config is in /etc/conf.d/dictd


prepconfig() {
	if [ ! -e ${DICTDCONF} ]
	then
		eerror "dictd not started.  Config file not found."
		return 1
	fi
	# if no dictionaries, skip startup.

	# The new way of doing this is to scan /usr/lib/dict and tweek the conf
	einfo "Scanning for dictionaries..."
	if [ ! -d "${DLIBDIR}" ]; then
		eerror "${DLIBDIR} doesn't exist, no dictionaries found."
		return 1
	fi
	pushd ${DLIBDIR} >/dev/null
	INDEXFILES=`ls *.index`
	if [ -z "$INDEXFILES" ]; then
		eerror "No dictionaries installed."
		return 1
	fi

	cat $DICTDCONF | sed -e '/^#LASTLINE/,$d' > $TMPCONF
	echo "#LASTLINE" >> $TMPCONF

	CNT=0
	for i in $INDEXFILES
	do
		DNAME=`echo $i | awk -F . '{print $1;}'`
		#two possible names for a matching dictionary, check which is there.
		if [ -f ${DNAME}.dict.dz ]; then
			DICT=${DNAME}.dict.dz
		elif [ -f ${DNAME}.dict ];then
			DICT=${DNAME}.dict
		else
			einfo "Index $i has no matching dictionaray..."
		fi

		#ok, go an index, and a dixtionary, append.
		echo "database $DNAME { data \"${DLIBDIR}/${DICT}\"" >> $TMPCONF
		echo "         index \"${DLIBDIR}/$i\" }" >> $TMPCONF

		CNT=`expr $CNT + 1`
	done
	popd >/dev/null
	mv ${TMPCONF} ${DICTDCONF}
	einfo "Done, $CNT dictionary indexes found."
}

depend() {
	need localmount
}

start() {
	ebegin "Starting dictd"
	prepconfig || return 1
	start-stop-daemon --start --quiet --exec /usr/sbin/dictd -- $EARGS
	eend $?
}

stop() {
	ebegin "Stopping dictd"
	start-stop-daemon --stop --quiet --exec /usr/sbin/dictd
	eend $?
}
