#!/bin/sh
#	BSDI	audiosend,v 1.2 1995/12/10 23:55:52 sanders Exp
#
# Copyright (c) 1991 Bell Communications Research, Inc. (Bellcore)
# 
# Permission to use, copy, modify, and distribute this material 
# for any purpose and without fee is hereby granted, provided 
# that the above copyright notice and this permission notice 
# appear in all copies, and that the name of Bellcore not be 
# used in advertising or publicity pertaining to this 
# material without the specific, prior written permission 
# of an authorized representative of Bellcore.  BELLCORE 
# MAKES NO REPRESENTATIONS ABOUT THE ACCURACY OR SUITABILITY 
# OF THIS MATERIAL FOR ANY PURPOSE.  IT IS PROVIDED "AS IS", 
# WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
#

unset IFS

# Defaults
RECORD_AUDIO=${RECORD_AUDIO:-vrec}
PLAY_AUDIO=${PLAY_AUDIO:-vplay}
EDIT_AUDIO=${EDIT_AUDIO:-mxv}
METAMAIL_TMPDIR=${METAMAIL_TMPDIR:-${TMPDIR:-/tmp}}
SENDMAIL=/usr/sbin/sendmail

if [ $# -ge 1 ]; then
    to="$1"; shift
else
    echo -n "To: "
    read to
fi

echo -n "Cc: "
read cc

echo -n "Subject: "
read subject

fname=${METAMAIL_TMPDIR}/audio-draft.$$
fnameraw=${METAMAIL_TMPDIR}/audio-raw.$$

rm -f $fnameraw $fname
echo "To: $to" > $fname
echo "Subject: $subject" >> $fname
echo "Cc: $cc" >> $fname
echo "MIME-Version: 1.0" >> $fname
echo "Content-Type: audio/basic" >> $fname
echo "Content-Transfer-Encoding: base64" >> $fname
echo  "" >> $fname

rm -f $fnameraw
if audiocompose audio/basic $fnameraw; then
    mimencode -b < $fnameraw >> $fname
    rm -f $fnameraw
    echo "" >> $fname

    echo -n "Sending mail, please wait...  "
    if ${SENDMAIL} $to $cc < $fname; then
	echo "Done."
	rm $fname
	exit
    else
	echo Mail delivery failed, draft saved in $fname
    fi
else
    # if we have something recorded then save the draft
    if [ -s $fnameraw ]; then
	mimencode -b < $fnameraw >> $fname
	rm -f $fnameraw
	echo "" >> $fname
	echo "Draft saved in $fname"
    else
	rm -f $fnameraw $fname
    fi
    exit 1
fi
