#!/bin/sh

# vg_call can optionally replace the standard vgetty answering
# mechanism. This is off by default, define R_CALL_PROGRAM
# in voclib.h if you want to use this. Be warned, however,
# this isn't as well tested or robust as the C code.
# Features like random message selection and logging don't work either.
#
# Make sure that the messages M_GREET and M_GOODBYE (defined
# below) exist.
#
# $1 is the log file
# vg_call will return with an exit code of 0 after a voice call
# and 1 if vgetty should attempt a data/fax connection.

VOICE_DIR=/var/spool/voice
ZPLAY=/usr/bin/zplay

MSG_DIR=$VOICE_DIR/messages

M_GREET=$MSG_DIR/standard
M_GOODBYE=$MSG_DIR/goodbye

PATH=$VOICE_DIR:/bin:/usr/bin:/usr/local/bin
OPT=-St
LOGFILE=/tmp/log_mg.`basename $1`
TMP=/tmp/vg_call.$$

log () {
	echo >>$LOGFILE
	echo -n `date "+%m/%d %H:%M:%S vg "`"$*" >>$LOGFILE
}

NUM=1
RECORDING=$VOICE_DIR/incoming/voc-Z$NUM$$
while [ -f $RECORDING ]
do
	NUM=`expr $NUM + 1`
	RECORDING=$VOICE_DIR/incoming/voc-Z$NUM$$
done

### play greeting message

log "playing $M_GREET"
ret=`$ZPLAY $OPT -T 0 -D $M_GREET`
log "got $ret"

case $ret in
[E])	log "error!"
	exit 0
	;;
[#V])	log "hangup request"
	exit 0
	;;
[db])	log "nobody there"
	exit 0
	;;
[c])	log "got fax calling tone"
	exit 1
	;;
[0-9])	log "dtmf digit"
	exec vg_dtmf $ret
	;;
esac

### beep

log "beep"
$ZPLAY $OPT -b '[933,0,12]'

### record a message or dtmf digits

log "recording $RECORDING"
$ZPLAY $OPT -CD -r $RECORDING >$TMP
read ret dtmf <$TMP
rm $TMP

log "got $ret $dtmf"

case $ret in
[sc])	log "fax/data call"
	exit 1
	;;
esac

vg_message $RECORDING

if [ ! -z "$dtmf" ]
then
	log "got dtmf command"
	exec vg_dtmf $dtmf
fi

$ZPLAY $OPT $M_GOODBYE

exit 0
