#!/bin/sh -
PATH=$PATH:/usr/local/sbin:/sbin:/usr/sbin:/usr/lib; export PATH
umask 077

sent="$HOME/l/mail/sent.mbox"
sendmail="${SENDMAIL-sendmail}"

# sentmail - archiving sendmail front-end that sets the envelope to the From
# Steve Kinzler, steve@kinzler.com, Sep 09/Aug 11
# https://kinzler.com/me/home.html#unix

tmp=/tmp/sentmail$$

case "`lockfile-create 2>&1`" in
*sage*)	trap 'rm -f $tmp; lockfile-remove "$sent"' 0 1 2 13 15
	lockfile-create "$sent";;
*)	trap 'rm -f $tmp "$sent.lock"'	     0 1 2 13 15
	lockfile "$sent.lock";;
esac

cat > $tmp || exit

from=`sed -n 's/^From:[ 	]*//p; /^$/q' $tmp |
      sed 's/[ 	]*$//; s/.*<\([^>]*\)>.*/\1/; s/(.*)//; s/[ 	].*//'`
case "$from" in
?*)	From="$from"; set x -f "$from" ${1+"$@"}; shift;;
*)	From="${USER-${LOGNAME-me}}@${HOST-localhost}";;
esac

cat < $tmp | (echo "From $From `date`" >> "$sent"
	      tee -a "$sent"
	      echo '' >> "$sent")		| "$sendmail" ${1+"$@"}
