#!/bin/sh
#
# $NetBSD: courierpop.sh,v 1.16 2008/02/19 18:45:14 jlam Exp $
#
# Courier POP3 services daemon
#
# PROVIDE: courierpop
# REQUIRE: authdaemond
# KEYWORD: shutdown

. /etc/rc.subr

name="courierpop"
rcvar=${name}
command="/usr/pkg/sbin/couriertcpd"
ctl_command="/usr/pkg/sbin/pop3d"
pidfile="/var/courier/tmp/pop3d.pid"
required_files="/usr/pkg/etc/courier/pop3d /usr/pkg/etc/courier/pop3d-ssl"

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
}

courierpop_prestart()
{
	mkdir_perms /var/courier/tmp \
		courier mail 0770
}

courierpop_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

		. /usr/pkg/etc/courier/pop3d

		case x$POP3DSTART in
		x[yY]*)
			echo "Starting ${name}."
			${ctl_command} $action
                ;;
		esac
		;;
	stop)
		echo "Stopping ${name}."
		${ctl_command} $action
		;;
	esac
}

load_rc_config $name
run_rc_command "$1"
