#! /bin/sh -
# Adapted from Rnmail, by lwall@sdcrdcf
#
# syntax: notesmail -h headerfile
# (headerfile includes the old article, if any)
#

export PATH || (echo "OOPS, this isn't sh.  Desperation time.  I will feed myself to sh."; sh $0; kill $$)

# System dependencies

mailer="/bin/rmail"
sendmail="/usr/lib/sendmail"
# if you change this to something that does signatures, take out signature code

# what pager you use--if you have kernal paging use cat
pager="/usr/bin/more"
# default editor
defeditor="/usr/bin/vi"

test=test
sed=__SED__
echo=__ECHO__
cat=__CAT__
grep=__GREP__
rm=__RM__
# set $nflag to "-n" if your echo has that flag
#   (BSD has it, SysV and HP-UX don't)
nflag=

if $test -x /usr/lib/sendmail ; then	# for BSD use "-f" instead of "-x"
	mailer=$sendmail
fi
tmpart=/tmp/rnmail$$
dotdir=${DOTDIR-${HOME-$LOGDIR}}

headerfile=""
case $# in
0) ;;
*)  case $1 in
    -h)
	headerfile="$2"
	case $# in
	3) oldart=$3 ;;
	esac
	;;
    esac
    ;;
esac

case $headerfile in
'')
    case $# in
    0)
	to=h
	while $test "$to" = h ; do
	    $echo ""
	    $echo $nflag "To: "
	    read to
	    case $to in
	    h|\?)
		to=h
		$cat <<'EOH'

Type the net address of those people that you wish the message sent to.
Note that you will be asked later for additional addresses of people to
whom the message is not addressed but you wish to get copies.

Separate multiple addresses with spaces.

EOH
		;;
	    esac
	done
	;;
    *)
	to="$*"
	;;
    esac
    to=`$echo "$to" | $sed 's/  */ /g'`

    title=h
    while $test "$title" = h ; do
	$echo ""
	$echo $nflag "Title/Subject: "
	read title
	case $title in
	h|\?)
	    title=h
	    $cat <<'EOH'

Type the title for your message.  
EOH
	    ;;
	esac
    done

# now build a file with a header for them to edit
    
    $cat > $tmpart <<EOHeader
To: $to
Subject: $title
Cc:
Bcc:

EOHeader

    ;;
*)
    $cat < $headerfile  > $tmpart
    ;;
esac

state=edit

$echo "Entering editor..."

while true ; do
    case $state in
    edit)
	rescue="sleep 1; $cat $tmpart >>${HOME-$LOGDIR}/dead.letter ; $echo saved in ${HOME-$LOGDIR}/dead.letter ; $rm -f $tmpart; exit"
	trap "$rescue" 1
	trap "" 2
	${VISUAL-${EDITOR-$defeditor}} $tmpart $oldart
	trap "$rescue" 2
	state=ask
	;;
	
    ask)
	$echo ""
	$echo "Send, abort, edit, or list? "
	read ans
	
	case $ans in
	a*)
	    state=rescue
	    ;;
	e*)
	    state=edit
	    ;;
	l*)
	    $pager $tmpart
	    state=ask
	    ;;
	s*)
	    state=send
	    ;;
	h*|\?)
	    $cat <<'EOH'

Type s to send the message, a to abort and append the message to dead.letter,
e to edit the message again, or l to list the message.
EOH
	esac
	;;
    
    send)
	if $test -f $dotdir/.signature; then
	    echo $nflag "Append .signature file? [y] "
	    read ans
	    case $ans in
	    ''|y*) cat $dotdir/.signature >> $tmpart
	    esac
	fi
	case $mailer in
	*sendmail)
	    $mailer -t <$tmpart
	    ;;
# but recmail does not know about Bcc, alas
	*recmail)
	    $mailer <$tmpart
	    ;;
	*)
	    set `$sed <$tmpart -n -e '/^To:/{' -e 's/To: *//p' -e q -e '}'`
	    set "$@" `$sed <$tmpart -n -e '/^Cc:/{' -e 's/Cc: *//p' -e q -e '}'`
	    set "$@" `$sed <$tmpart -n -e '/^Bcc:/{' -e 's/Bcc: *//p' -e q -e '}'`
	    $grep -v "^Bcc:"  <$tmpart | $mailer "$@"
	    ;;
	esac
	case $? in
	0)
	    state=cleanup
	    ;;
	*)
	    state=rescue
	    ;;
	esac
	;;
    rescue)
	$cat $tmpart >> ${HOME-$LOGDIR}/dead.letter
	$echo "Message saved to ${HOME-$LOGDIR}/dead.letter"
	state=cleanup
	;;
    cleanup)
	$rm -f $tmpart
	exit
	;;
    esac
done
