#!/bin/sh

##--------------------------------------------------------------
##
## msmtpq-ng-queue-mta:	A wrapper script for msmtpq-ng-queue for
##			use as sendmail/postfix compatible mailq
##			postqueue and postsuper commands
##
##  Copyright (C) 2016-2019 Daniel Dickinson
##
##  This program is free software: you can redistribute it and/or modify
##  it under the terms of the GNU General Public License as published by
##  the Free Software Foundation, either version 3 of the License, or, at
##  your option, any later version.
##
##--------------------------------------------------------------

## mailq
## is only a wrapper
## it invokes the main msmtpq-ng-queue script
##
## all configuration and documentation is in the msmtpq-ng-queue script

## note that for 'special circumstances'
##   (e.g. embedded systems, etc.)
## where the msmtpq-ng-queue script itself is not on the path
## set the environment variable MSMTPQ_NG_QUEUE to the full path
## to msmtpq-ng-queue in /etc/msmtpq-ng-mta.rc
## e.g.
## MSMTPQ_NG_QUEUE=/path/to/msmtpq-ng-queue

MSMTPQ_WRAPPER="$0"

if [ -z "$MSMTP_MTA_OVERRIDE_CONF" ]; then
	MSMTP_OVERRIDE_CONF=/etc/msmtpq-ng-mta.rc
else
	MSMTP_OVERRIDE_CONF=$MSMTP_MTA_OVERRIDE_CONF
fi
export MSMTP_OVERRIDE_CONF

if [ "$MSMTP_MTA_SKIP_CONF" != "true" ]; then
	# shellcheck source=./tmp/msmtpq-ng.rc
	[ -r /etc/msmtpq-ng-mta.rc ] && . /etc/msmtpq-ng-mta.rc
	# shellcheck source=./tmp/msmtpq-ng.rc
	[ -r ~/.msmtpq-ng-mta.rc ] && . ~/.msmtpq-ng-mta.rc
fi

[ -z "$LOG" ] && LOG=syslog
[ -z "$Q" ] && Q=/var/spool/msmtp/"$(id -un)"
[ -z "$MSMTP_LOCK_DIR" ] && MSMTP_LOCK_DIR=/var/lock/msmtp/"$(id -un)"
[ -z "$MSMTP_LOG_UMASK" ] && MSMTP_LOG_UMASK=007
[ -z "$MSMTP_UMASK" ] && MSMTP_UMASK=077
[ -z "$MSMTP_QUEUE_QUIET" ] && MSMTP_QUEUE_QUIET=true
[ -z "$MSMTP_IGNORE_NO_RECIPIENTS" ] && MSMTP_IGNORE_NO_RECIPIENTS=true
[ -z "$MSMTPQ_NG_QUEUE" ] && MSMTPQ_NG_QUEUE=msmtpq-ng-queue
[ -z "$MSMTPQ_NG" ] && MSMTPQ_NG=msmtpq-ng

export LOG Q MSMTP_LOG_UMASK MSMTP_LOCK_DIR MSMTP_UMASK MSMTP_QUEUE_QUIET MSMTP_IGNORE_NO_RECIPIENTS MSMTPQ_NG MSMTP_CONF MSMTPQ_NG_QUEUE EMAIL_CONN_TEST EMAIL_CONN_TEST_PING EMAIL_CONN_TEST_IP EMAIL_CONN_TEST_SITE MSMTP_HOLD_SMTP_MAIL MSMTP_HOLD_CLI_MAIL MSMTPQ_WRAPPER

if [ "$1" = "$MSMTPQ_WRAPPER" ]; then
	shift
fi

[ -x "$MSMTPQ_NG_QUEUE" ] && exec "$MSMTPQ_NG_QUEUE" "$@"
[ -n "$(command -v "$MSMTPQ_NG_QUEUE")" ] && exec "$MSMTPQ_NG_QUEUE" "$@"

echo "mailq: unable to find msmtpq-ng-queue" >&2
# From sysexits.h: EX_OSFILE
exit 72
