:
# 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.

# Conversion from C shell to Bourne shell by Z-Code Software Corp.
# Conversion Copyright (c) 1992 Z-Code Software Corp.
# 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 Z-Code Software not
# be used in advertising or publicity pertaining to this
# material without the specific, prior written permission
# of an authorized representative of Z-Code.  Z-CODE SOFTWARE
# 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.

# This is to make metamail/showaudio do playback on the speaker, not the phone.
AUDIOSPEAKERFORCE=1 ; export AUDIOSPEAKERFORCE

if test -d /usr/sony
then
	dev=/dev/sb0
else
	dev=/dev/audio
fi

if test -f /usr/lib/sendmail
then
    MAILCOMMAND=/usr/lib/sendmail
else
    MAILCOMMAND=/bin/mail
fi

echo-n "To: "
read to
echo-n "Subject: "
read subject
echo-n "CC: "
read cc

record=1

while true
do
	if test $record -eq 1
	then
		echo-n "Press RETURN when you are ready to start recording: "
		read foo

		fname=/tmp/audio-out.$$
		fnameraw=/tmp/audio-raw.$$

		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

		trap "kill -9 $! > /dev/null 2>&1" 1 2 15

		if test -z "RECORD_AUDIO"
		then
			(/bin/cat < $dev > $audiofile) &
		else
			($RECORD_AUDIO > $audiofile) &
		fi
		echo-n "press RETURN when you are done recording: "
		read foo
		echo One moment please...
		/bin/sleep 1
		echo-n Killing recording job...
		/bin/kill -9 $! > /dev/null 2>&1
		mmencode -b < $fnameraw >> $fname
		/bin/rm $fnameraw
		echo "" >> $fname
	fi
	record=0

	echo ""
	echo "What do you want to do?"
	echo ""
	echo "1 -- Send mail"
	echo "2 -- Listen to recorded message"
	echo "3 -- Replace with a new recording"
	echo "4 -- Quit"
	read which
	case $which in
		1) echo-n "Sending mail, please wait...  "
		  $MAILCOMMAND $to $cc < $fname

		  if test $? -eq 0
		  then
			echo Done.
			/bin/rm -f $fname
			exit 0
		  else
			echo Mail delivery failed, draft is in $fname
		  fi ;;
		2) metamail -d $fname ;;
		3) record=1 ;;
		4) exit 0 ;;
	esac
done
