#!/bin/sh
#
# Copyright (C) 2001-2018 Graeme Walker <graeme_walker@users.sourceforge.net>
#
# 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.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
# ===
#
# emailrelay-multicast.sh
#
# An example "--filter" script that copies/links each new message into all
# avaliable sub-directories of the main spool directory. The original message
# files are deleted if they were successfully copied/linked into all
# sub-directories.
#
# This can be used for doing SMTP multicast by having an emailrelay forwarding
# process polling each sub-directory. (For POP multicasting use the
# "emailrelay-filter-copy" program with the "pop-by-name" feature so that
# there is no need to copy or link content files.)
#
# Hard links are used for the content files in order to conserve disk space. Log
# entries are written into the base envelope file to help with error recovery.
#
# The remote SMTP client which is submitting the message will be notified of any
# failures in this script via SMTP error responses. Alternatively an "exit 0"
# can be used to silently leave the message in the main spool directory (see
# below).
#

# parse the command-line
#
content="$1"
envelope="`echo \"${content}\" | sed 's/content$/envelope.new/'`"
base_dir="`dirname \"${content}\"`"
if test "$1" = "" -o "${content}" = "${envelope}" -o "${base_dir}" = "."
then
	echo usage: `basename $0` '<content-file>' >&2
	exit 2
fi

# copy/link message files
#
list="`find \"${base_dir}\" -mount -maxdepth 1 -mindepth 1 -type d`"
error_list=""
subdir_exists="0"
for dir in ${list} ""
do
	if test -d "${dir}" -a "${dir}" != ".." -a "${dir}" != "." -a "${dir}" != "${base_dir}"
	then
		subdir_exists="1"
		envelope_tmp="${envelope}.`basename \"${dir}\"`.tmp"
		envelope_dst="${dir}/`basename \"${envelope}\" .new`"
		if ln "${content}" "${dir}" && cp "${envelope}" "${envelope_tmp}" && mv "${envelope_tmp}" "${envelope_dst}"
		then
			echo "X-MailRelay-Multicast: ${dir}>" | tr '>' '\r' >> "${envelope}"
		else
			error_list="${error_list} ${dir}"
			echo "X-MailRelay-Multicast-Error: ${dir}>" | tr '>' '\r' >> "${envelope}"
		fi
	fi
done

# error handling
#
if test "${subdir_exists}" = "0"
then
	# no sub-directories created -- this script has no effect
	exit 0
elif test "${error_list}" = ""
then
	# successfully copied -- delete the original
	rm -f "${content}" "${envelope}"
	exit 100
else
	# something failed -- tell the submitting smtp client
	# replace these two lines with "exit 0" if the client should not know...
	echo "<<`basename $0`: `basename "${content}"`: failed to copy message into${error_list}>>"
	exit 1
fi

