#!/bin/sh
#RCUPDATE:3 4:75:This line is required for script management

. /etc/rc.d/config/functions

SERVICE=dictd
opts="start stop"


DICTDCONF=/etc/dict/dictd.conf
TMPCONF=/etc/dict/dictd.conf.$$
prepconfig() {
	if [ ! -e ${DICTDCONF} ]
	then
		eerror "dictd not started.  Config file not found."
		exit 1
	fi
	# if no dictionaries, skip startup.

# The new way of doing this is to scan /usr/lib/dict and tweek the conf
	DLIBDIR=/usr/lib/dict 

	einfo "Scanning for dictionaries..."
	if [ ! -d "${DLIBDIR}" ]; then
		eerror "${DLIBDIR} doesn't exist, no dictionaries found."
		exit 1
	fi
	pushd ${DLIBDIR} >/dev/null
	INDEXFILES=`ls *.index`
	if [ -z "$INDEXFILES" ]; then
		eerror "No dictionaries installed."
		exit 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."
}

start() {
    ebegin "Starting supervised ${SERVICE}"
    prepconfig
	ln -sf ../services/${SERVICE} ${SVCDIR}/control/${SERVICE}
    eend $?
}

stop() {
    ebegin "Stopping supervised ${SERVICE}"
   	if [ -e ${SVCDIR}/control/${SERVICE} ]
	then
		/usr/bin/svc -dx ${SVCDIR}/control/${SERVICE}
		rm ${SVCDIR}/control/${SERVICE}
	fi
	eend $?
}

doservice ${@}

#vim:sw=4:ts=4:ai
