#!/bin/sh
#
# Copyright (C) 2001-2013 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-deliver.sh
#
# Looks for local mail in the E-MailRelay spool directory and delivers is using
# 'procmail'.
#
# usage: emailrelay-deliver.sh [<spool-dir>]
#
# This illustrates how delivery to local "postmaster" mailboxes could be done,
# although it is not likely to be a useful feature for a typical mail relay 
# setup.
#


store="/var/spool/emailrelay"
postmaster="root"
procmail="procmail"

# parse the command line
#
if test $# -ge 1
then
	store="${1}"
fi

# check the spool directory is valid
#
if test \! -d "${store}"
then
	echo `basename $0`: invalid spool directory >&2
	exit 1
fi

# for each e-mail to a local recipient...
#
for file in "${store}"/emailrelay.*.envelope.local ""
do
	if test -f "${file}"
	then
		content="`echo \"${file}\" | sed 's/envelope/content/'`"

		deliver_to="`fgrep X-MailRelay-To-Local ${file} | sed 's/X-MailRelay-To-Local: //' | tr -d '\015' | sed \"s/postmaster/${postmaster}/g\"`"
		if test "${deliver_to}" = "" 
		then 
			deliver_to="${postmaster}"
		fi

		# deliver using procmail
		#
		if test -f "${content}"
		then
			echo `basename $0`: delivering `basename "${content}"` to ${deliver_to}
			"${procmail}" -d "${deliver_to}" < "${content}"
			rc=$?
			if test "${rc}" -eq 0
			then
				rm -f "${file}" 2>/dev/null
			fi
		fi
	fi
done

