#!/bin/csh -fb
# (The "-fb" might need to be changed to "-f" on some systems)
#
# 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.
#

if (! $?METAMAIL_TMPDIR) then
    set METAMAIL_TMPDIR=/tmp
endif

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

if (-d /usr/sony) then
    set dev=/dev/sb0
else
    set dev=/dev/audio
endif

if ( $#argv == 1 ) then
    set to = $1
else
    echo -n "To: "
    set to = $<
endif
echo -n "Subject: "
set subject = $<
echo -n "CC: "
set cc = $<

record:
echo -n "Press RETURN when you are ready to start recording: "
set foo = $<

set fname = ${METAMAIL_TMPDIR}/audio-out.$$
set 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
# THIS IS UNBELIEVABLY GRUBBY
onintr cleanup
if (! $?RECORD_AUDIO) then
    (/bin/cat < $dev > $fnameraw) &
else
    ($RECORD_AUDIO > $fnameraw) &
endif
jobs -l > ${METAMAIL_TMPDIR}/AUDCAT.$$
set foo=`/bin/cat ${METAMAIL_TMPDIR}/AUDCAT.$$`
/bin/rm ${METAMAIL_TMPDIR}/AUDCAT.$$
set PID=$foo[2]
echo -n "press RETURN when you are done recording: "
set foo=$<
echo One moment please...
/bin/sleep 1
echo -n Killing recording job...
/bin/kill -9 $PID >& /dev/null
mimencode -b < $fnameraw >> $fname
/bin/rm $fnameraw
echo "" >> $fname

whatnext:
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"
set which = $<
    switch ("$which")
        case 1:
          echo -n "Sending mail, please wait...  "
          /usr/sbin/sendmail $to  $cc < $fname
          if (! $status) then
            echo "Done."
            rm $fname
            exit
          else
            echo Mail delivery failed, draft mail is in $fname
          endif
          breaksw
        case 2:
          metamail -d $fname
          breaksw
        case 3:
          goto record
        case 4:
          exit
    endsw
goto whatnext
exit 0
cleanup:
kill -9 $PID >& /dev/null
