#!/bin/sh
# /etc/init.d/dmimapd
# This file is part of Decimail; see http://decimail.org
# (C) 2004-2006 Philip Endecott
 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
# based on /etc/init.d/skeleton

# This should be put in /etc/init.d/dmimapd
# It is then symlinked to from /etc/rc.d/
# To make these symlinks, use update-rc.d:
#   update-rc.d dmimapd defaults N
# where N determines the order in which things are started,
# small numbers first.
# 30 seems like a good number - after postgres and most other
# things.
# Note that this will not do anything if any symlinks are already
# present, i.e. it will not override locally-defined things.

source /etc/decimail.conf

PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
DAEMON=/usr/local/sbin/dmimapd
DAEMON_OPTS="-d $DECIMAIL_DB_CONNECTION"
NAME=dmimapd
DESC="Decimail IMAP Daemon"

# Complain if daemon is not present
if test \! -x $DAEMON
then
  echo "$DAEMON not found / not executable" 1>&2
  exit 1
fi

# Read config file if it is present.
#[ -r /etc/default/$NAME ] && . /etc/default/$NAME

set -e

ulimit -c unlimited

case "$1" in
  start)
	echo -n "Starting $DESC: $NAME"
	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		--oknodo --exec $DAEMON -- $DAEMON_OPTS
	echo "."
	;;
  stop)
	echo -n "Stopping $DESC: $NAME"
	start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		--oknodo
	echo "."
	;;
  #reload)
	#
	#	If the daemon can reload its config files on the fly
	#	for example by sending it SIGHUP, do it here.
	#	(dmimapd currently can't)
	#
	#	If the daemon responds to changes in its config file
	#	directly anyway, make this a do-nothing entry.
	#
	# echo -n "Reloading $DESC configuration..."
	# start-stop-daemon --stop --signal 1 --quiet --pidfile \
	#	/var/run/$NAME.pid --exec $DAEMON
	# echo "done."
  #;;
  restart|force-reload)
	#
	#	If the "reload" option is implemented, move the "force-reload"
	#	option to the "reload" entry above. If not, "force-reload" is
	#	just the same as "restart".
	#
	echo -n "Restarting $DESC: $NAME"
	start-stop-daemon --stop --quiet --pidfile /var/run/$NAME.pid \
		--oknodo
	sleep 1
	start-stop-daemon --start --quiet --pidfile /var/run/$NAME.pid \
		--oknodo --exec $DAEMON -- $DAEMON_OPTS
	echo "."
	;;
  *)
	N=/etc/init.d/$NAME
	# echo "Usage: $N {start|stop|restart|reload|force-reload}" >&2
	echo "Usage: $N {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0