#!/bin/sh
#
# dictd:	Starts the Dictionary Daemon
#
# chkconfig:	- 90 10
# description:	This is a daemon for the Dictionary Server Protocol (DICT), \
#               a TCP transaction based query/response protocol that allows \
#               a client to access dictionary definitions from a set of \
#		natural language dictionary databases.
# processname:	dictd
# config:	/etc/dictd.conf 
# config:       /etc/dictd/*

DAEMON_FILE=dictd
DAEMON_NAME="Dictionary Daemon"  
DAEMON_CONF=/etc/dictd.conf

generate_dictdconf() {
    umask 022
    if ls /etc/dictd/*.dictconf >/dev/null 2>&1; then 
	echo "# DO NOT EDIT! This file is autogenerated by $0." >$DAEMON_CONF
	echo "# To configure dictd edit /etc/dictd/* files and restart daemon"\
          >>$DAEMON_CONF
	cat /etc/dictd/dictd-main.conf /etc/dictd/*.dictconf >>$DAEMON_CONF
	return 0
    fi 

    echo "$0: no dictionaries found"
    return 1	
}

# Source function library.
. /etc/init.d/functions

# Get network config
. /etc/sysconfig/network

# Check that networking is up.
if [ ${NETWORKING} = "no" ] ; then
	echo "Networking is down. ${DAEMON_FILE} can't be run."
	exit 1
fi

# Get sysconfig
[ -f /etc/sysconfig/${DAEMON_FILE} ] && . /etc/sysconfig/${DAEMON_FILE}

[ -x /usr/sbin/${DAEMON_FILE} ] || exit 0

RETVAL=0
# See how we were called.
start() {
	if [ ! -f /var/lock/subsys/${DAEMON_FILE} ]; then
		if generate_dictdconf; then
		    echo -n "Starting ${DAEMON_NAME}: "
		    daemon ${DAEMON_FILE} ${DICTD_OPTS}
		    pidofproc ${DAEMON_FILE} >/dev/null 2>&1
		    RETVAL=$?
		    echo
		    [ $RETVAL -eq 0 ] && touch /var/lock/subsys/${DAEMON_FILE}
		    return ${RETVAL}
		else
		    return 1
		fi
	else
		echo "${DAEMON_NAME} service is already running."
		return 1
	fi
}
stop(){
	if [ -f /var/lock/subsys/${DAEMON_FILE} ]; then
		echo -n "Stopping ${DAEMON_NAME} service"
		killproc /usr/sbin/${DAEMON_FILE}
		RETVAL=$?
		echo
		rm -f /var/lock/subsys/${DAEMON_FILE} >/dev/null 2>&1
		return ${RETVAL}
	else
		echo "${DAEMON_NAME} service is not running."
		return 1
	fi
}
reload(){
	if generate_dictdconf; then
		echo -n "Reloading ${DAEMON_FILE}: "
		killproc ${DAEMON_FILE} -HUP
		RETVAL=$?
		echo
		return ${RETVAL}
	else
		return 1
	fi
}

case "$1" in
  start)
	start
	;;
  stop)
	stop
	;;
  status)
        status ${DAEMON_FILE}
        ;;
  restart)
        stop
        start
        ;;
  reload)
	reload
	;;
  *)
	msg_usage "$0 {start|stop|status|restart|reload}"
	exit 1
	;;
esac

exit $RETVAL
