#! /bin/sh
# 
# Script to start and stop dictionary server daemon (/usr/sbin/dictd)
# written by Bob Hilliard <hilliard@debian.org> 15 April 1988, last
# modified on 11 January 1999
# based on /etc/init.d/skeleton v1.7  05-May-1997  by miquels@cistron.nl

# Since dictd doesn't remove its pid properly, use -f option to force
# a start even if /var/run/dictd.pid exists.  dictd is erratic about
# writing a pid in /var/run, and when it does do so, it is even more
# erratic about removing it on exit, so the --pidfile option doesn't
# work consistently.  The --name option seems to work _almost_ always,
# but has caused some installation failures and bug reports.  Although
# stopping and re-starting the daemon is not critical, in that it can
# be recovered from, it is a nuisance on re-installation.  Therefore,
# I have used the oknodo option to prevent breaking the installation.
# I have inserted a sleep betwen stop and start in restart and reload,
# which may help the restart function.  As a further gesture to
# paranoia, I have added direct "kills" to the pids and removed
# /var/run/dictd.pid,in case the normal stop didn't take efect.  I
# hope it is now pretty near buller-proof.
# (See note in README.Debian)  

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/sbin/dictd
NAME=dictd
DESC="dictionary server"

test -f $DAEMON || exit 0

set -e

case "$1" in
  start)
    echo -n "Starting $DESC: "
    su nobody -c "/sbin/start-stop-daemon --start --quiet --oknodo  --exec $DAEMON"
    echo "$NAME."
    ;;
  stop)
    echo -n "Stopping $DESC: "
    start-stop-daemon --stop --quiet --oknodo --name dictd --exec $DAEMON
    kill `ps ax|grep "^.\{20\}dictd"` &> /dev/null ||true
    kill `cat /var/run/dictd.pid` &> /dev/null ||true
    rm -f /var/run/dictd.pid &> /dev/null || true
    echo "$NAME."
    ;;
  restart)
    echo -n "Restarting $DESC: "
start-stop-daemon --stop --quiet --oknodo --exec $DAEMON
    kill `ps ax|grep "^.\{20\}dictd"` &> /dev/null|| true
    kill `cat /var/run/dictd.pid` &> /dev/null||true
    sleep 1
# The daemon starts slowly  - maybe it stops slowly as well
    rm -f /var/run/dictd.pid &> /dev/null || true
    su nobody -c "/sbin/start-stop-daemon --start --quiet --oknodo --name dictd --exec $DAEMON"
    echo "$NAME."
    ;;
  reload|force-reload)
    # at present there is no way to make dictd re-read its config files
    echo -n "Reloading $DESC configuration files"
    start-stop-daemon --stop --quiet --oknodo --name dictd --exec $DAEMON
    kill `ps ax|grep "^.\{20\}dictd"` &> /dev/null||true
    kill `cat /var/run/dictd.pid` &> /dev/null||true
    sleep 1
# The daemon starts slowly - maybe it stops slowly as well
    rm -f /var/run/dictd.pid &> /dev/null || true
    su nobody -c "/sbin/start-stop-daemon --start --quiet --oknodo --exec $DAEMON"
    echo "."
    ;;
  *)
    echo "Usage: /etc/init.d/$NAME {start|stop|restart|reload|force-reload}"
    exit 1
    ;;
esac

exit 0
