#!/bin/bash
#
# This script is run from custom_decode, and will remove the last
# entry from the log, sleep for two minutes, then remove the pin
# enable flag.  This ensures a command activated by pin code is only
# available for 2 mins after receiving the activate command.
#
# After the 2 min timeout, if the pin flag still exists, remove it.
# ** Your pin-activated script should remove the pin flag itself **
# As the flag is in RAM-Disk, PC reset will clear it!!
#
# Unlog (?) added to remove the pin number from the system log file.
# If this is not done, someone hearing the pin activate tone can
# recall the last loglines and discover the pin code.
#
# V1.0 02/02/09 - VK8SJ - Original version
# v1.1 01/01/10 - VK8SJ - added unlog command
# v2.0 12/04/21 - VK2YLD - Mod for Debian
#
#
#
######################################################################
#
# 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
#############################################################################
#
function writelog () {
  MESSAGE="`date '+%b %d %Y %T'` ${0##*/}: "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  fi
}
#
function my_key () {
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/coscheck
      $BIN/key
}

######################################################################
sleep 120                             # Pin timeout delay (Secs)
if [ -f $LOCAL/pin_rx ] ; then        # If pin flag still there..
   rm -f $LOCAL/pin_rx                # bury it!
   writelog "PIN number timeout."
   my_key
   sleep .25
   play $AUDIO/custom/sound-deadpin.wav
   sleep .25
   unkey
fi
exit 0

