#!/bin/bash
######################################################################## 
# filename:	confirm
#
# description:	This script play the command confirmation beep-beep.
#
# history:
# 20010109	kc6hur	created
# 20020117	kc6hur	made sendtone a subroutine
# 20080720     vk8zsj    modified for Centos layout + tweaks
# 20210417     vk2yld    Make it work on Debian-10
#
#
#
######################################################################## 
#
# 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

#################### Functions ######################################## 
### Key...
# Key the PTT. If the PTT manager program is running then use it;
# otherwise, just mash it
#
function my_key {
   if ps -C ptt -o pid= >&/dev/null
   then
      echo "+" > $LOCAL/ptt_fifo
   else
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/key
   fi
}

### Unkey
# Unkey the PTT. If the PTT manager program is running, then use it;
# otherwise, just bury it
#
function my_unkey {
   if ps -C ptt -o pid= >&/dev/null
   then
      echo "-" > $LOCAL/ptt_fifo
   else
      $BIN/unkey
   fi
}

#
### 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
#
function sendtone {
   my_key
   sleep .5
   play $AUDIO/custom/sound-confirm.wav     # contains 'beep beep' or whatever
   sleep .5
   my_unkey
}
#
#############################################################################
#
# Send command confirmation beeps
#
if [ -f $LOCAL/active ] ; then
   killall ispeaker >&/dev/null
   killall sfswrapper >&/dev/null
   sendtone
   $SCRIPT/sfswrapper
else
   sendtone
fi
#
exit 0
