#!/bin/sh
# $Header: /boot/home/code/cvsroot/scripts/AdvAnsweringMachine,v 1.2 1998/05/17 02:21:23 jrand Exp $
#
#  AdvAnsweringMachine
#
#  By: Jeremy Rand
#    Copyright 1997
#
# This script answers the phone and attempts to play a voice message.
# Once the message is played, it starts recording a message from the
# caller.  After the recording is complete, it hangs up the phone.
#
# If the caller enters the DTMF tones in the PASSWORD veariable, it
# starts to play back recent messages.  The caller can use DTMF 7 to
# replay a the current message or 9 to continue to the next one.  To
# accomplish this, it uses a few messages stored in the messages/
# directory.  The "firstmessage" message is played before playing back
# any of the messages and should tell the caller that the password was
# accepted.  The "nextone" message is played between each message and
# should prompt the caller to press DTMF 7 or 9.  The "donethem"
# message says that there are no more messages to be played back.  You
# may need to re-record these messages for your modem (they work fine on
# my ZyXEL 2864).
#
# If the caller presses DTMF 3 (you should not use 3's in your password
# the modem will switch to data mode.  If a modem is placing the call,
# they should establish a carrier.  The program will spawn a login
# program for the caller to login to your system
#

PASSWORD=51
ALLMESSAGES=/tmp/NewMessages."$2"

cleanup () {
  ./tellModem -d onhook "$MODEM" stop > /dev/null
  exit
  }

playmess () {

  cat /dev/null >> "$ALLMESSAGES"	# make sure the message file exists
  ./tellModem "$MODEM" stop > /dev/null	# stop the voice modem if it is busy
  ./tellModem "$MODEM" play ./messages/firstmessage > /dev/null
					# play the password accepted message

  while read LINE			# get a message stored in the file
  do
    if [ -r "$LINE" ]			# if the message is still readable
    then
      DTMF=7				# by default, repeat the current message
      while true
      do
        ./tellModem "$MODEM" play "$LINE" > /dev/null
					# play the current message
        ./tellModem "$MODEM" stop > /dev/null
        RESULT=`./tellModem "$MODEM" play ./messages/nextone`
					# play the prompt for the next message
        ./tellModem "$MODEM" stop > /dev/null

	if echo $RESULT | grep DTMF > /dev/null
	then
	  DTMF=`echo $RESULT | awk '{print $NF}'`
					# get the DTMF that was pressed
          if [ "$DTMF" != 7 ]
          then
            break
          fi
	elif echo $RESULT | grep Error > /dev/null
        then
	  cleanup			# hangup if an error occurs
        fi
      done
    fi
  done < "$ALLMESSAGES"

  ./tellModem "$MODEM" play ./messages/donethem > /dev/null
					# tell the caller that was the last one
  cat /dev/null > "$ALLMESSAGES"	# empty out the messages file
  cleanup				# hangup the phone
  }

cd "$1"
MODEM="$2"
MESSAGE="$3"

REMAINING="$PASSWORD"			# the DTMF tone remaining to be pressed

RESULT=`./tellModem -d offhook "$MODEM" play`
					# get result from playing the greeting

while true
do
  if [ $? != 0 ] || echo $RESULT | grep 'Error' > /dev/null
  then
    cleanup				# hangup phone on an Error
  fi
  
  if echo $RESULT | grep 'Event: Fax' > /dev/null
  then
    ./tellModem "$MODEM" fax "$MESSAGE"
    cleanup
  fi

  if echo $RESULT | grep 'Event: Hangup' > /dev/null
  then
    cleanup
  fi

  if echo $RESULT | grep -v DTMF > /dev/null
  then
    break				# record greeting if not a DTMF
  fi

  DTMF=`echo $RESULT | awk '{print $NF}'`
					# get the DTMF pressed

  if [ `echo $REMAINING| head -1c` -eq "$DTMF" ]
  then					# if the DTMF is next one in PASSWORD
    REMAINING=`echo $REMAINING | sed 's/^.//g'`
					# remove the first number from PASSWORD
    if [ -z "$REMAINING" ]		# if PASSWORD is empty, all tones have
    then				# been pressed, play messages
      playmess
    fi
  else
    REMAINING="$PASSWORD"
  fi

  if [ "$DTMF" = 3 ]			# if a DTMF 3, its a data call
  then
    ./tellModem "$MODEM" data login > /dev/null
    cleanup
  fi

  if [ "$DTMF" = 4 ]			# if a DTMF 4, its a fax call
  then
    ./tellModem "$MODEM" fax "$MESSAGE" > /dev/null
    cleanup
  fi

  RESULT=`./tellModem "$MODEM" cont`	# continue otherwise
done

RESULT=`./tellModem -D 6 "$MODEM" record "$MESSAGE"`
if echo $RESULT | grep -v 'Error' > /dev/null
then
  echo "$MESSAGE" >> "$ALLMESSAGES"
fi

cleanup
