#!/bin/sh
echo DEFANGED.1
exit
#!/bin/sh
echo DEFANGED.612
exit
#!/bin/sh
echo DEFANGED.2
exit
#!/usr/bin/vm-hylafax_egetty shell

####################
BEEP_FREQ=2400
BEEP_TIME=0
WAIT_TIME_BFR=0
MESSAGE_FILE_RU="/var/spool/voice/messages/fax-ru.rmd"
MESSAGE_FILE_EN="/var/spool/voice/messages/fax-en.rmd"
WAIT_TIME_AFT=5
###TRACE="Y"
TRACE="N"
####################


# Define the function to receive an answer from the voice library
function receive
{
     read -r INPUT <&$VOICE_INPUT;
     echo "$INPUT";
}

# Define the function to send a command to the voice library
function send
{
     echo $1 >&$VOICE_OUTPUT;
     kill -PIPE $VOICE_PID
}

# Define the function to output trace messages
function trace
{
     if [ "$TRACE" == "Y" ]; then
          echo "`date` $0: $1" >&2
     fi
}

# Define the function to send a stop command to the voice library
function stop
{
     trace "Let's stop it all"
     send "STOP"						# <---
     ANSWER=`receive`						# --->
     if [ "$ANSWER" != "READY" ]; then
          trace "can't stop this, anyway OK"
          #EXIT_CODE=4
     fi
     trace "Stopped"
}


trace "Let's see if the voice library is talking to us"
ANSWER=`receive`						# <---
if [ "$ANSWER" != "HELLO SHELL" ]; then
     trace "voice library not answering"
     exit 4
fi

trace "Let's answer the message"
send "HELLO VOICE PROGRAM"					# --->
ANSWER=`receive`						# <---
if [ "$ANSWER" != "READY" ]; then
     trace "initialization failed"
     exit 4
fi

trace "Enabling autostop"
send "AUTOSTOP ON"						# --->
ANSWER=`receive`						# <---
if [ "$ANSWER" != "READY" ]; then
     trace "can't enable autostop, anyway OK"
     #exit 4
fi

trace "Enabling events"
send "ENABLE EVENTS"						# --->
ANSWER=`receive`						# <---
if [ "$ANSWER" != "READY" ]; then
     trace "can't enable events"
     exit 4
fi

trace "Settin' device to dialup line (pickin' up the handset)"
send "DEVICE DIALUP_LINE"					# --->


EXIT_CODE=-1
WAIT_TIME=$WAIT_TIME_BFR
MESSAGE_FILE=$MESSAGE_FILE_RU

while [ "$EXIT_CODE" == "-1" ]; do

     trace "Reading answer/event"
     ANSWER=`receive`						# <---
     trace "answer \"$ANSWER\" recieved"

     case "$ANSWER" in

          READY)
               
               if [ "$(( $BEEP_FREQ > 0 ))" == "1" ] && [ "$(( $BEEP_TIME > 0 ))" == "1" ] ; then

                    trace "Start beepin' at $BEEP_FREQ Hz for $BEEP_TIME msec"
                    send "BEEP $BEEP_FREQ $BEEP_TIME"		# --->
                    ANSWER=`receive`				# <---
                    if [ "$ANSWER" != "BEEPING" ]; then
                         trace "can't start beepin', anyway OK"
                         #EXIT_CODE=4
                    else
                         trace "Beepin' started"
                    fi
                    BEEP_TIME=0

               elif [ "$(( $WAIT_TIME > 0 ))" == "1" ]; then

                    trace "Start waiting for $WAIT_TIME sec"
                    send "WAIT $WAIT_TIME"			# --->
                    ANSWER=`receive`				# <---
                    if [ "$ANSWER" != "WAITING" ]; then
                         trace "can't start waiting, anyway OK"
                         #EXIT_CODE=4
                    else
                         trace "Waiting started"
                    fi
                    WAIT_TIME=0

               elif [ "${#MESSAGE_FILE}" != "0" ] && [ -f $MESSAGE_FILE ]; then

                    trace "Start playing $MESSAGE_FILE file"
                    send "PLAY $MESSAGE_FILE"			# --->
                    ANSWER=`receive`				# <---
                    if [ "$ANSWER" != "PLAYING" ]; then
                         trace "can't start playing, anyway OK"
                         #EXIT_CODE=4
                    else
                         trace "Playing started"
                    fi

                    if [ "$MESSAGE_FILE" == "$MESSAGE_FILE_RU" ]; then
                         MESSAGE_FILE=$MESSAGE_FILE_EN
                    else
                         MESSAGE_FILE=
                         WAIT_TIME=$WAIT_TIME_AFT
                    fi

               else

                    trace "Returning to answer anything"
                    EXIT_CODE=0
                    #trace "Returning to answer modem"
                    #EXIT_CODE=1
                    #trace "Returning to recieve a fax"
                    #EXIT_CODE=2
                    #trace "Returning to do hangup"
                    #EXIT_CODE=5

               fi ;;

          FAX_CALLING_TONE)

               stop
               trace "Returning to recieve a fax"
               EXIT_CODE=2 ;;

          DATA_CALLING_TONE|DATA_OR_FAX_DETECTED)

               stop
               trace "Returning to answer anything"
               EXIT_CODE=0 ;;
               #trace "Returning to answer modem"
               #EXIT_CODE=1 ;;

          RECEIVED_DTMF)

               DTMF_DIGIT=`receive`
               trace "DTMF digit '$DTMF_DIGIT' recieved"
               if [ "$DTMF_DIGIT" == "*" ]; then
                    stop
                    trace "Returning to answer anything"
                    EXIT_CODE=0
                    #trace "Returning to answer modem"
                    #EXIT_CODE=1
               fi ;;

          BUSY_TONE|DIAL_TONE|DEVICE_NOT_AVAILABLE)

               stop
               trace "Returning to do hangup"
               EXIT_CODE=5 ;;

          *)
               ;;

     esac
     
done

trace "Let's say goodbye"
send "GOODBYE"							# --->
ANSWER=`receive`						# <---
if [ "$ANSWER" != "GOODBYE SHELL" ]; then
     trace "could not say goodbye to the voice library, anyway OK"
     #EXIT_CODE=4
else
     trace "Goodbye said"
fi

exit $EXIT_CODE

