#!/sbin/runscript
# Copyright 1999-2012 Gentoo Foundation
# Distributed under the terms of the GNU General Public License, v2 or later
# $Header: /var/cvsroot/gentoo-x86/net-mail/dovecot/files/dovecot.init-r4,v 1.1 2012/12/01 10:40:49 eras Exp $

extra_started_commands="reload"

depend() {
	need localmount
	before postfix
	after bootmisc ldap mysql ntp-client ntpd postgresql saslauthd slapd 
	use logger net
}

checkconfig() {
	DOVECOT_INSTANCE=${SVCNAME##*.}
	if [ -n "${DOVECOT_INSTANCE}" -a "${SVCNAME}" != "dovecot" ]; then
		DOVECOT_CONF=/etc/dovecot/dovecot.${DOVECOT_INSTANCE}.conf
	else
		DOVECOT_CONF=/etc/dovecot/dovecot.conf
	fi
	if [ ! -e ${DOVECOT_CONF} ]; then
		eerror "You will need an ${DOVECOT_CONF} first"
		return 1
	fi
	if [ -x /usr/sbin/dovecot ]; then
		DOVECOT_BASEDIR=$(/usr/sbin/dovecot -c ${DOVECOT_CONF} -a | grep '^base_dir = ' | sed 's/^base_dir = //')
	else
		eerror "dovecot not executable"
		return 1
	fi
	DOVECOT_BASEDIR=${DOVECOT_BASEDIR:-/var/run/dovecot}
	DOVECOT_PIDFILE=${DOVECOT_BASEDIR}/master.pid
}

start() {
	checkconfig || return 1
	ebegin "Starting ${SVCNAME}"
	start-stop-daemon --start --exec /usr/sbin/dovecot \
		--pidfile "${DOVECOT_PIDFILE}" -- -c "${DOVECOT_CONF}"
	eend $?
}

stop() {
	checkconfig || return 1
	ebegin "Stopping ${SVCNAME}"
	start-stop-daemon --stop --exec /usr/sbin/dovecot \
		--pidfile "${DOVECOT_PIDFILE}"
	eend $?
}

reload() {
	checkconfig || return 1
	ebegin "Reloading ${SVCNAME} configs and restarting auth/login processes"
	start-stop-daemon --signal HUP --exec /usr/sbin/dovecot \
		--pidfile "${DOVECOT_PIDFILE}"
	eend $?
}
