#!/bin/sh
#
# $NetBSD: courierd.sh,v 1.5 2008/02/19 18:45:24 jlam Exp $
#
# Courier core processes 
#
# PROVIDE: courierd
# REQUIRE: authdaemond courierfilter 
# KEYWORD: shutdown

. /etc/rc.subr

name="courierd"
rcvar=${name}
command="/usr/pkg/sbin/${name}"
ctl_command="/usr/pkg/sbin/courier"

restart_cmd="${name}_doit restart"
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
}

courierd_prestart()
{
	# Courier mail submission directories
	mkdir_perms /var/courier/msgq \
			courier mail 0750
	mkdir_perms /var/courier/msgs \
			courier mail 0750
	mkdir_perms /var/courier/tmp \
			courier mail 0770
	mkdir_perms /var/courier/track \
			courier mail 0755

	# Courier webadmin directories
	mkdir_perms /var/courier/webadmin \
			courier mail 0700
	mkdir_perms /var/courier/webadmin/added \
			courier mail 0700
	mkdir_perms /var/courier/webadmin/removed \
			courier mail 0700

	# Courier delivery configuration directories
	mkdir_perms /usr/pkg/etc/courier/aliasdir \
			courier mail 0755
	mkdir_perms /usr/pkg/etc/courier/aliases \
			courier mail 0750
	mkdir_perms /usr/pkg/etc/courier/smtpaccess \
			courier mail 0755
}

courierd_doit()
{
	action=$1

	case $action in
	restart)
		echo "Restarting ${name}."
		;;
	start)
		test -f /usr/pkg/etc/courier/aliases.dat ||
			/usr/pkg/sbin/makealiases
		echo "Starting ${name}."
		;;
	stop)
		echo "Stopping ${name}."
		;;
	esac

	${ctl_command} $action
}

load_rc_config $name
run_rc_command "$1"
