#!/bin/bash
#####################################################################
# lastline  - Speak out last 'n' lines from the system log.
#   Original script by Steve Jones VK8SJ, Katherine NT Aust.
#
# Usage:  lastline <x>
#       x = number of lines to speak out.
#
# Uses Swift text-to-speech synthesiser to convert modified data
# to audio transmission.
#
# Version   Date     Who      Wot..
#  1.0.0  18/12/09  VK8SJ, Original hack
#  1.0.1  26/01/12  VK8SJ, Update to select TTS engine
#
#
#
#
######################################################################
# Make sure we are user repeater!!!
   if [ "`/usr/bin/whoami`" != "repeater" ] ; then
     echo "This program must be run as user REPEATER!"
     exit 1
   fi
# Make sure we have sourced the environment file
   if [ "$RUN_ENV" != "TRUE" ] ; then
      . /home/irlp/custom/environment
   fi
#
######################################################################
#  Script Variables
#
Outtext=$LOCAL/voice.txt
TTSengine="swift"          # Set as necessary for text-to-speech
#TTSengine="festival"      #  engine to work correctly.
#
#
######################################################################
######################################################################
#
### Send Tone
# Key the PTT, wait for a while (specified amount of time below),
# play the confirmation beep-beep wav file, then unkey the PTT
#
sendtone() {
   $BIN/key
   sleep .5
   $BIN/play $AUDIO/custom/sound-confirm.wav     # contains 'beep beep' or whatever
   sleep .5
   $BIN/unkey
}
#
### Logging ..
#
# This function logs Date etc and running script name, then message..
#
writelog() {
  MESSAGE="`date '+%b %d %Y %T %z'` "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}

#
### Send Voice Response
# Key PTT, wait then speak from requested text
# or SSML file. Set mode with 2nd parameter ($2)
# - defaults to text
#  (use 'quote' if no file involved)
#
sendvoice() {
  $BIN/key
  sleep .5
  case $TTSengine in
     swift)      /usr/local/bin/swift -m text -f $1 ;;
     *)          /usr/bin/festival --tts $1 ;;
  esac
  sleep .25
  $BIN/unkey
}

#  End Local Functions
######################################################################
#

echo "Log File Reader Activated"

# If rubbish passed to us, die
if [ "$#" = "0" ] ; then           # gotta have digits..
   echo "**ERROR** No line count specified."
   writelog "lastline - DTMF information missing or invalid"
   exit 9
fi

if [ ! -f $LOCAL/enable ] || [ -f $LOCAL/active ] ; then
   echo "Node is either active or disabled. Request denied!"
   writelog "lastline - Node Active or Disabled - denied."
   exit 9
fi

echo "Processing requested "$1" lines."
sendtone                           # Ack cmd received

#
######## Let's go.. ###################################
#

#tail -n$1 $LOGFILE > $LOCAL/logbits

echo "Here is the last "$1" entries from the system log." > $Outtext
tail -n$1 $LOGFILE |  awk '{out=""; out=$4;for(i=6;i<=NF;i++){out=out" "$i}; print out}' | sed s/":"/" "/g >> $Outtext

writelog "Log reader script active - "$1" Lines."

echo "," >> $Outtext

#year=`date '+%b %d %Y'`
#sed s/"$year"/","/ $LOCAL/logbits > $LOCAL/logbits2
#sed s/":"/" "/ $LOCAL/logbits2 > $LOCAL/logbits
#sed s/":"/" "/ $LOCAL/logbits > $LOCAL/logbits2
#sed s/" +1000"/","/ $LOCAL/logbits2 >> $Outtext



cat $Outtext
sendvoice $Outtext

rm -f $LOCAL/log*
rm -f $LOCAL/$Outtext

writelog "Log reader script completed."

exit 0

