#!/bin/csh -f
#######################################################################
#
#  voicemail - send zyxel voice as NextMail
#
#  usage: voicemail <voice_file> <recipient(s)>
#
#
#  (C) Olaf Mueller <olaf@orest.escape.de>  30JUL96
#
########################################################################


set MYNAME = $0
set MYNAME = $MYNAME:t


@ argc = $#argv


if ($argc != 2) then
    echo usage: ${MYNAME} \<voice_file\> \<recipient\(s\)\>
    exit 1
endif


# use option -c for converting zyxel voice to NeXT sounds with 8012Hz, 8bit mulaw
# set SNDCONVERT		= /usr/local/bin/zsnd2snd -c
set SNDCONVERT		= /usr/local/bin/zsnd2snd
set SIZEOFVOICE		= /usr/local/lib/AM/sizeofv
set MAILER			= /usr/lib/sendmail

#	we do not want to mess up current directory
set MailDir 		= /tmp/NextMail
set LETDIR 			= $MailDir/container
set INDEX 			= index.rtf
set VOICE			= call.vox
set LETTER 			= $MailDir/body
set MAIL 			= $MailDir/mail

set voicefile		= "$1"
set recipient		= "$2"
set subject			= "AnsweringMachine"



#######################################  test executables  #############################
if (! -x $SNDCONVERT) then
	echo ${MYNAME}\[`date`\]: cannot execute $SNDCONVERT.
	exit 1
endif

if (! -x $SIZEOFVOICE) then
	echo ${MYNAME}\[`date`\]: cannot execute $SIZEOFVOICE.
	exit 1
endif

if (! -x $MAILER) then
	echo ${MYNAME}\[`date`\]: cannot execute $MAILER.
	exit 1
endif


#######################################  test voice file  #############################
if ( ! -f ${voicefile} ) then
	echo ${MYNAME}: The voice file \"$voicefile\" is not a file. Must be a file.
	exit 1
else
	if ( ! -r ${voicefile} ) then
		echo ${MYNAME}: The voice file \"$voicefile\" is not readable. Please change the read-mode.
		exit 1
	endif
endif


#######################################  can create a new $MailDir ?  ############################
if (-d $MailDir) then
	rmdir $MailDir
	if ( $status != 0 ) then
		echo ${MYNAME}: The directory \"$MailDir\" contains some stuff. Please remove it.
		exit 1
	endif
else
	if (-e $MailDir) then
		echo ${MYNAME}: The file \"$MailDir\" exists. Please remove it.
		exit 1
	endif
endif


#######################################  create a new $MailDir  ##################################
mkdir $MailDir
if ( $status != 0 ) then
	echo ${MYNAME}: We could not create the directory \"$MailDir\".
	exit 1
endif


############################  convert subject to UNIX name  ######################################
set AttachName = `echo $subject | sed "s/[^a-zA-Z]/_/g"`
set AttachName = .tar.123.${AttachName}.attach


#######################################  create our letter directory  ############################
mkdir $LETDIR
if ( $status != 0 ) then
	echo ${MYNAME}: We could not create the directory \"$LETDIR\".
	exit 1
endif


#######################################  build the mail  #########################################
$SNDCONVERT $voicefile $LETDIR/${VOICE} > /dev/null


#######################################  copy the letter into $INDEX  ############################
echo "{\rtf0"												>  $LETDIR/$INDEX
echo "Your Call:	"										>> $LETDIR/$INDEX
echo "{{\attachment100 $VOICE"  							>> $LETDIR/$INDEX
echo "}" 													>> $LETDIR/$INDEX
echo " (`$SIZEOFVOICE $voicefile` min)" 					>> $LETDIR/$INDEX
echo "}"													>> $LETDIR/$INDEX


#######################################  convert $LETDIR directory into tar file of same name ####
cd $LETDIR
tar -cf $LETDIR.tar *
if ( $status != 0 ) then
	echo ${MYNAME}: We could not tar the directory \"$LETDIR\".
	exit 1
endif
cd ..
rm -rf $LETDIR


#######################################  compress + uuencode $LETDIR.tar to $LETTER  #############
compress -f $LETDIR.tar
uuencode $LETDIR.tar.Z $AttachName > $LETTER
rm $LETDIR.tar.Z



#######################################  create Header + Body  ###################################
set MSGSIZE = `ls -al $LETTER | awk '{print $4}'`
echo To: $recipient 										>  $MAIL
echo Subject: $subject 										>> $MAIL
echo Received: by NeXT.Mailer \(1.100\) 					>> $MAIL
echo Next-Attachment: $AttachName, $MSGSIZE, 1/1, 8844, 0 	>> $MAIL
echo ""  													>> $MAIL
cat $LETTER 												>> $MAIL
rm $LETTER



#######################################  send the stuff to sendmail  #############################
cat $MAIL | $MAILER $recipient
if ( $status != 0 ) then
	echo ${MYNAME}: We could not send \"$MAIL\". Please investigate
	exit 1
endif
rm $MAIL


#######################################  clean up  ###############################################
rmdir $MailDir
if ( $status != 0 ) then
	echo ${MYNAME}: We could not remove the directory \"$MailDir\". Please investigate
	exit 1
endif
