#!/bin/sh
#
# $NetBSD: sqwebmail.sh,v 1.12 2014/10/21 12:19:11 gdt Exp $
#
# Courier SqWebMail services daemon
#
# PROVIDE: sqwebmail
# REQUIRE: authdaemond
# KEYWORD: shutdown

. /etc/rc.subr

name="sqwebmail"
rcvar=${name}
command="/usr/pkg/sbin/courierlogger"
ctl_command="/usr/pkg/sbin/webmaild.rc"
pidfile="/var/courier/tmp/sqwebmaild.pid"
required_files="/usr/pkg/etc/courier/calendarmode /usr/pkg/etc/courier/sqwebmaild"

start_precmd="${name}_prestart"
start_cmd="${name}_doit start"
stop_cmd="${name}_doit stop"

mkdir_perms()
{
	dir="$1"; user="$2"; group="$3"; mode="$4"
	test -d $dir || /bin/mkdir -p $dir
	/usr/sbin/chown $user $dir
	/usr/bin/chgrp $group $dir
	/bin/chmod $mode $dir
}

sqwebmail_prestart()
{
	# Courier webmail and calendar (pcp) directories
	/bin/mkdir -p /var/courier/sqwebmail
	/bin/mkdir -p /var/courier/sqwebmail/logincache
	mkdir_perms /var/courier/sqwebmail/calendar \
			root mail 0755
	mkdir_perms /var/courier/sqwebmail/calendar/public \
			root mail 0755
	mkdir_perms /var/courier/sqwebmail/calendar/private \
			root mail 0750
	mkdir_perms /var/courier/sqwebmail/calendar/localcache \
			root mail 0750
	mkdir_perms /var/courier/tmp \
			courier mail 0770
}

sqwebmail_doit()
{
	action=$1
	case $action in
	start)
		for f in $required_files; do
			if [ ! -r "$f" ]; then
				echo 1>&2 "$0: WARNING: $f is not readable"
				return 1
			fi
		done
		echo "Starting ${name}."
		${ctl_command} $action
		;;
	stop)
		echo "Stopping ${name}."
		${ctl_command} $action
		;;
	esac
}

load_rc_config $name
run_rc_command "$1"
