#! /bin/sh
##
## $Id: vboxmime,v 1.1 1996/07/15 14:37:16 root Exp $
##
## *Sample* script to send a voice message as mail. The script use mmencode
## (from the metamail package) to encode the message with base64.
##
## Make sure that all needed programs are in the search path!
##
## Usage: vboxmime SAMPLENAME CALLERID [MAIL-TO-ADDRESS]

PATH="${PATH}:/usr/local/lib/vbox"

TEMPNAME="/tmp/vboxmime.$$"
BASENAME="`basename $0`"

SAMPLE="${1}"
NUMBER="${2}"
MAILTO="${3}"

if [ "${MAILTO}" = "" ]
then
   if [ "${USER}" != "" ]
   then
      MAILTO="${USER}"
   fi
fi

if [ "${MAILTO}" = "" ]
then
   echo "${BASENAME}: Can't find email address to mail message."
   exit
fi

if (! test -r "${SAMPLE}")
then
   echo "${BASENAME}: File '${SAMPLE}' doesn't exist."
   exit
fi

COMPRESSION=""

rmdgetheader "${SAMPLE}"

case $? in

   2) COMPRESSION="audio/adpcm2";
      ;;
   3) COMPRESSION="audio/adpcm3";
      ;;
   4) COMPRESSION="audio/adpcm4";
      ;;
   5) COMPRESSION="audio/alaw";
      ;;
   6) COMPRESSION="audio/ulaw";
      ;;
esac

if [ "${COMPRESSION}" = "" ]
then
   echo "${BASENAME}: Unknown compression type or error."
   exit
fi

mmencode <"${SAMPLE}" >${TEMPNAME}

if [ "$?" != "0" ] || (! test -s "${TEMPNAME}")
then
   rm -f "${TEMPNAME}"

   echo "${BASENAME}: Can't encode message."
   exit
fi

(
   echo "To: ${MAILTO}"
   echo "Subject: Voice message (${NUMBER})"
   echo "MIME-Version: 1.0"
   echo "Content-type: ${COMPRESSION}"
   echo "Content-Transfer-Encoding: base64"
   echo ""
   cat  "${TEMPNAME}"
   echo ""
) | sendmail -oi -oem -t

if [ "$?" != "0" ]
then
   echo "${BASENAME}: Error sending encoded mail."
fi

rm -f "${TEMPNAME}"
