summaryrefslogtreecommitdiff
path: root/mail/dovecot/files/dovecot.in
blob: 36736d1b0082b872bcb604c4c5fc96ba2242d3e4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/sh

# PROVIDE: dovecot
# REQUIRE: %%REQUIRE%%
# BEFORE:  mail
# KEYWORD: shutdown

# dovecot_enable (bool):	Set it to YES to enable dovecot
#				Default: NO
# dovecot_config (str):		Path to dovecot.conf
#				Default: %%ETCDIR%%/dovecot.conf
#				Set it to a space-separated list to start
#				multiple dovecot instances
# dovecot_flags (str):		Extra flags to pass to dovecot
#				Default: empty

# Define dovecot_* variables in one of these files:
#	/etc/rc.conf
#	/etc/rc.conf.local
#	/etc/rc.conf.d/dovecot

. /etc/rc.subr

name=dovecot
rcvar=dovecot_enable

# read configuration and set defaults
load_rc_config ${name}
: ${dovecot_enable:="NO"}
: ${dovecot_config:="%%ETCDIR%%/${name}.conf"}

command="%%PREFIX%%/sbin/${name}"
start_precmd="start_precmd"
stop_postcmd="stop_postcmd"
restart_cmd="restart_cmd"
extra_commands="reload"

start_precmd()
{	# Ensure runtime directory exists with correct permissions
	/usr/bin/install -o root -g wheel -m 0755 -d ${base_dir}
}

stop_postcmd()
{	# Cleanup runtime directory. The dovecot.conf link has to stay.
	/usr/bin/find ${base_dir} ! -type l -delete
}

restart_cmd()
{	# Overriding makes rc.subr run this once for each instance
	run_rc_command stop
	run_rc_command start
}

# To start multiple instances of dovecot set dovecot_config to
# a space separated list of configuration files.
for config in ${dovecot_config}; do
	required_files="${config}"
	command_args="-c ${config}"
	if [ -s ${config} ]; then
               ${command} ${command_args} -a 2>&1 >/dev/null
                if [ $? -ne 0 ]; then
                       echo "==Error=="
                       echo "Configuration file check failed."
                       # Use dovecot to print the config error.
                       ${command} ${command_args} -a
                       exit $?
                fi
		base_dir=$(${command} ${command_args} -a 2>/dev/null | /usr/bin/awk -F '= ' '/^base_dir =/ { print $2 }')
		pidfile="${base_dir}/master.pid"
	else
		echo "==Error=="
		echo "Config file ${config} does not exist. If this is"
		echo "a new installation, please create the config files as outlined in"
		echo "	# pkg info -D dovecot"
	fi
	run_rc_command "$1"
done