#!/bin/sh -
#
# mail_out_using_sendmail:
#
# Effectively a tiny mail-sending client. Needed because on at least one of
# the machines (namely falcon) that can be used as a DoC mailhost
# from time to time, the Unix command "mail" is very non-standard and
# cumbersome to use in a shell script. - And on DoC machines generally,
# it can throw up obscure and sometimes machine-dependent "bugs" / "features"!
# (sendmail, on the other hand, seems fairly standard across machines.)
#
# usage: mail_out_using_sendmail "<'To: '-specifier>" ["<'Subject: '-specifier>"
#                                                      ["<'Cc: '-specifier>"
#                                                       ["<'Bcc: '-specifier>"]
#                                                      ]
#                                                     ]
#
# (Message is read from standard input.)
#
progname=`basename $0`
local_domain="doc.ic.ac.uk"
from_specifier="`whoami`@$local_domain"

if [ $# -eq 1 ]; then
	to_specifier="$1"
	subject_specifier=""
	cc_specifier=""
	bcc_specifier=""
elif [ $# -eq 2 ]; then
	to_specifier="$1"
	subject_specifier="$2"
	cc_specifier=""
	bcc_specifier=""
elif [ $# -eq 3 ]; then
	to_specifier="$1"
	subject_specifier="$2"
	cc_specifier="$3"
	bcc_specifier=""
elif [ $# -eq 4 ]; then
	to_specifier="$1"
	subject_specifier="$2"
	cc_specifier="$3"
	bcc_specifier="$4"
else
	sed -e 's/^		//' << ________here >&2
		usage: $progname "<'To: '-specifier>" ["<'Subject: '-specifier>"
		                                                     ["<'Cc: '-specifier>"
		                                                      ["<'Bcc: '-specifier>"]
		                                                     ]
		                                                    ]
________here

	exit 1
fi


(
	echo "From: $from_specifier"
	echo "To: $to_specifier"
	echo "Subject: $subject_specifier"
	if [ -n "$cc_specifier" ]; then
		echo "Cc: $cc_specifier"
	fi
	if [ -n "$bcc_specifier" ]; then
		echo "Bcc: $bcc_specifier"
	fi
	echo ""
	cat
) | /usr/lib/sendmail -em -t -i -f "$from_specifier"
