#!/bin/bash
########################################################################
# filename:	sendping
#
# description:	This script is called from custom_decode.
#               It will get an ip address from the user, ping test it
#               and send the result to the user with TTS.
#
# arguments:	none - make 'em up as we go along..
#                    interactive system.... I hope!
#
# flags:        $LOCAL/interactive - System alive
#               $LOCAL/rxcmd       - responses to user prompts
#               $LOCAL/result*     - output dump from 'ping'
#
#  Exit codes   0 - Success
#               1 - User response timeout
#               2 - Destination Unreachable
#               3 - User abort
#
# dependants:   scripts: my-functions.sh
#               system:  ping binary in current path
#
# history:
# 20100104  V1.00    vk8sj  Original ...
# 20110512  V1.01    vk8sj  Added expert mode to shorten prompts
#
#
########################################################################
#
source /home/irlp/custom/my-functions.sh   # test user & load environment
#
########################################################################
# set local variables
#
tictoc_stop="120"                          # user response timeout (sec*4)
expert="no"                                # expert mode - minimal prompts
TTSengine="swift"
#
########################################################################
# local function defines
#
function get_command () {
tictoc=1                                   # init clock var for loops
while [ TRUE ] ; do                        # create endless loop
   if [ -f $LOCAL/rxcmd ] ; then           # command rx yet?
      reply=`cat $LOCAL/rxcmd`
      rm -f $LOCAL/rxcmd                   # get rid of evidence!
      break                                # end endless loop (?)
   fi                                      # outa here..
usleep 250000                              # wait 250ms
let tictoc=$tictoc+1                       # bump counter
if [ "$tictoc" = "$tictoc_stop" ] ; then   # times up yet?
   reply="fail"                            # set flag as failure
   break                                   # jailbreak!!
fi
done                                       # continue endless loop
}

function nothing () {
writelog2 "Aborted - No Response"
sendvoice $CUSTOM/texts/ping_noresponse.ssml ssml
cleanup
exit 1
}

function oops () {
writelog2 "Ping binary crashed out. "$ip_addr
senderror
if [ "$expert" == "yes" ] ; then
   sendvoice "We are sorry, your call has failed overseas. Please try again in a few minutes." quote
  else
   sendvoice "Sorry. The command has aborted. Address $ip_addr not reachable." quote
fi
cleanup
exit 2
}

function cleanup () {
rm -f $LOCAL/interactive                   # old stuff..
rm -f $LOCAL/rxcmd                         # user response clear
rm -f $LOCAL/active                        # look idle again
rm -f $LOCAL/result*                       # squish old results
}

############################## Starts Here ##############################
#
if [ ! -f $LOCAL/enable ] || [ -f $LOCAL/active ] ; then
   exit 1                                        # node must be on-line & idle
fi

# clear all flags to start, tray-tables stowed and seats upright
cleanup
#
touch $LOCAL/interactive                         # set alive flag
echo "stn6732" > $LOCAL/active                   # make us look busy..
writelog2 "Activated"
#
#
sendvoice $CUSTOM/texts/ping_welcome.ssml ssml   # welcome message
get_command                                      # Verify user wants test done
if [ "$reply" = "fail" ] ; then nothing ; fi     # no response
echo "Welcome response - "$reply
case $reply in
1)   echo "Normal mode." ;;
P)   echo "Expert mode."
     expert="yes" ;;
*)   Writelog2 "System exit."
     sendvoice $CUSTOM/texts/ping_exit.ssml ssml
     cleanup
     exit 3 ;;
esac
if [ "$expert" == "yes" ] ; then
   sendvoice $CUSTOM/texts/ping_ex_addr.ssml ssml
  else
   sendvoice $CUSTOM/texts/ping_address.ssml ssml
fi
get_command
if [ "$reply" = "fail" ] ; then nothing ; fi     # no response
ip_addr=`echo $reply | sed s/"S"/"."/g`
echo "Address to ping - "$ip_addr
if [ "$expert" == "yes" ] ; then
   sendvoice $CUSTOM/texts/ping_ex_pkts.ssml ssml
  else
   sendvoice $CUSTOM/texts/ping_packets.ssml ssml
fi
get_command
if [ "$reply" = "fail" ] ; then nothing ; fi     # no response
ip_pkts=$reply                                   # check pkts, max 100
if [ "$ip_pkts" -lt "1" ] || [ "$ip_pkts" -gt "100" ] ; then
   sendvoice "Number of packets was invalid.  Defaulting to 10." quote
   sleep 0.5
   echo "Packet count error :"$ip_pkts" :- Defaulting to 10..."
   ip_pkts=10                                    # set default to 10
fi
echo "Packets to be sent - "$ip_pkts
sendvoice "Your call is important to us, Please wait." quote
ping -c $ip_pkts $ip_addr > $LOCAL/result || oops
tail -n4 $LOCAL/result > $LOCAL/result2
head -n+2 $LOCAL/result2 > $LOCAL/result
echo -n "<break strength='x-strong'/>" >> $LOCAL/result   # ssml command
tail -n2 $LOCAL/result2 >> $LOCAL/result
head -n3 $LOCAL/result > $LOCAL/result2
tail -n1 $LOCAL/result > $LOCAL/result4
sed s/"---"/" "/g $LOCAL/result2 > $LOCAL/result3
sed s/"ms"/" milleseconds."/g $LOCAL/result3 > $LOCAL/result2
sed s/"time"/"total test time"/g $LOCAL/result2 > $LOCAL/result
echo -n ".  Average round trip time was " >> $LOCAL/result
rtt=`sed s/"\/"/"_"/g $LOCAL/result4 | awk -F _ '{print($5)}'`
echo $rtt" milleseconds.  I need a drink now.  See yah later." >> $LOCAL/result
cat $LOCAL/result
sendvoice $LOCAL/result ssml
writelog2 "$ip_addr completed."
cleanup
exit 0

