#!/bin/sh

# PROVIDE: qmailsend
# REQUIRE: network
#
# The wrapper around qmail's qmail-send, qmail-lspawn, qmail-rspawn and
# qmail-clean chain.
#
# the qmailsend_delivery variable controls where mails should be delivered to:
#  maildir - qmail-local to ~/Maildir/ (this is the default)
#  mailbox - qmail-local to ~/Mailbox
#  proc    - procmail to /var/spool/mail/$USER
#  V7      - /bin/mail V7 interface to /var/spool/mail/$USER
#  SVR4    - /bin/mail SVR4 interface to /var/spool/mail/$USER
#  BSD44   - /usr/libexec/mail.local to /var/spool/mail/$USER
#
# Setting qmailsend_dotforward enables support for sendmail style
# .forward files
#

. /etc/rc.subr

name=qmailsend
rcvar=qmailsend_enable

load_rc_config $name

: ${qmailsend_delivery="maildir"}
: ${qmailsend_dotforward="NO"}

start_cmd="${name}_start"
command="%%PREFIX%%/bin/qmail-start"
procname=qmail-send

extra_commands="flush"
flush_cmd="qmailsend_flush"

pidfile="/var/run/${name}.pid"

qmailsend_start() {
  case ${qmailsend_delivery} in
    maildir) command_args=./Maildir/;;
    mailbox) command_args=./Mailbox;;
    proc)    command_args='|preline procmail';;
    V7)      command_args='|preline -f /bin/mail -f "${SENDER:-MAILER-DAEMON}" -d "$USER"';;
    SVR4)    command_args='|preline -f /bin/mail -r "${SENDER:-MAILER-DAEMON}" -d "$USER"';;
    BSD44)   command_args='|preline -f /usr/libexec/mail.local -r "${SENDER:-MAILER-DAEMON}" -d "$USER"';;
    *)       err 1 "Error: Unknown qmailsend delivery method: ${qmailsend_delivery}";;
  esac
  if checkyesno qmailsend_dotforward; then
    command_args='|dot-forward .forward
'"${command_args}"
  fi

  exec env - PATH="%%PREFIX%%/bin:%%LOCALBASE%%/bin:$PATH" ${command} "${command_args}" splogger qmail&
  /bin/pgrep -P $$ > ${pidfile}
}

qmailsend_flush() {
  /bin/pkill -ALRM ${rc_pid}
}

run_rc_command "$1"

