#!/bin/sh
#
### BEGIN INIT INFO
# Provides:          cyrus-imspd
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Internet Message Support Protocol daemon
# Description:       Internet Message Support Protocol (imsp) daemon, providing
#                    central storage for addressbooks and application config.
### END INIT INFO

set -e

DAEMON=/usr/sbin/cyrus-imspd
PATH=/sbin:/bin:/usr/sbin:/usr/bin

# Has the package been 'removed' but not purged?
test -f $DAEMON || exit 0

. /lib/lsb/init-functions

# Source the config file
DEFAULT=/etc/default/cyrus-imspd
[ -f $DEFAULT ] && . $DEFAULT

ARGS="--exec $DAEMON --pidfile=/var/run/cyrus-imspd.pid --make-pidfile"
[ "$IMSP_USER" ] && ARGS="$ARGS --chuid $IMSP_USER"
[ "$IMSP_GROUP" ] && ARGS="$ARGS --group $IMSP_GROUP"

case "$1" in
    start)
	log_daemon_msg "Starting cyrus-imspd"
	start-stop-daemon $ARGS --start --background --oknodo -- $IMSP_ARGS
	log_end_msg 0
	;;

    stop)
	log_daemon_msg "Stopping cyrus-imspd"
	start-stop-daemon $ARGS --stop --oknodo
	rm -f /var/run/cyrus-imspd.pid
	log_end_msg 0
	;;

    restart|force-reload)
	$0 stop
	sleep 1
	$0 start
	;;

    *)
	echo "Usage: $0 {start|stop|restart|force-reload}" >&2
	exit 1
	;;
esac

exit 0
