#!/bin/sh
#
# $NetBSD: authdaemond.sh,v 1.5 2008/02/18 20:37:22 jlam Exp $
#
# Courier user authentication daemon
#
# PROVIDE: authdaemond
# REQUIRE: LOGIN

. /etc/rc.subr

name="authdaemond"
rcvar=${name}
command="/usr/pkg/sbin/courierlogger"
ctl_command="/usr/pkg/sbin/authdaemond"
pidfile="/var/authdaemon/pid"
required_files="/usr/pkg/etc/authlib/authdaemonrc"

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
}

authdaemond_prestart()
{
	mkdir_perms /var/authdaemon \
		courier mail 0750
}

authdaemond_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"
				if [ -z $rc_force ]; then
					return 1
				fi
			fi
		done
		echo "Starting ${name}."
		;;
	stop)
		echo "Stopping ${name}."
		;;
	esac

	${ctl_command} ${action}
}

load_rc_config $name
run_rc_command "$1"
