#!/bin/sh
# 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.

# Brought into line with metamail 2.7 beta release Csh version
#    Dave Shield	February 1994

if test ! -z "${METAMAIL_TMPDIR:-}"
then
        METAMAIL_TMPDIR=/tmp
fi

# 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/sbin/sendmail
then
    MAILCOMMAND=/usr/sbin/sendmail
else
    MAILCOMMAND=/bin/mail
fi

if test ! -z "${1:-}"
    to=$1
else
    echo -n "To: "
    read to
fi
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=${METAMAIL_TMPDIR}/audio-out.$$
		fnameraw=${METAMAIL_TMPDIR}/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 > $fnameraw) &
		else
			($RECORD_AUDIO > $fnameraw) &
		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
		mimencode -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
