#!/bin/bash
#############################################################################
# filename:	killnode  (Choke or Dead!)
#
# description:	This script will shutdown the node. If command line contains
#               the -r (reboot), then the node will reboot instead of die.
#               If -r or -s (shutdown) NOT on command line, script exits
# history:
# 20090302   vk8sj  Initial
# 20090716   vk8sj  added code to rip 'decode' line from log file to hide
#                   DTMF command from prying eyes for those that make log
#                   excerts available on web pages. (like me!)
# 20090922   vk8sj  Check for valid pin code to allow actions.
#
#############################################################################
#
#############################################################################
############################# Functions #####################################
#############################################################################
#
### Logging ..
#
# This function logs Date etc and running script name, then message..
#
function writelog () {
  MESSAGE="`date '+%b %d %Y %T'` ${0##*/}: "$@
  if [ -n "$LOGFILE" ]; then
    echo $MESSAGE >> $LOGFILE
  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
   $BIN/play $AUDIO/custom/sound-confirm.wav     # contains 'beep beep' or whatever
   sleep .5
   my_unkey
}

### Send Error
# Key PTT, wait and then play the 'error' sound file
# If 'notx' on command line, error plays on local monitor
# speaker only, no ptt issued thus no tx!
#
function senderror () {
  if [ "$1" != "notx" ]; then
    my_key
    sleep .5
    $BIN/play $AUDIO/custom/sound-error.wav  # whatever you want for errors
    $BIN/play $AUDIO/error.wav               # add std error message
    sleep .5
    my_unkey
   else
    $BIN/play $AUDIO/custom/sound-error.wav
    $BIN/play $AUDIO/error.wav
  fi
}

### 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
}

#
### End Functions
#
#########################################################################
#########################################################################
#####                    Main engine start!                         #####
#########################################################################
#########################################################################
#
#  Check for activated pin code
#
if [ -f $LOCAL/pin_rx ] ; then         # check for pin flag
   rm -f $LOCAL/pin_rx                 # remove pin flag - used up..
  else
   senderror                           # let user know no pin
   writelog "Error - Inactive pin code"
   exit 1                              # crash out... cya
fi
#################################
#  Process command line options
#
ACTION=""
case $1 in
-r|-R)  ACTION=reboot ;;
-s|-S)  ACTION=poweroff ;;
*)      echo "Incorrect command line arguments.."
        echo " Quitting without actions"
        senderror
        writelog "Error - Incorrect command line arguments.."
        exit 0 ;;
esac
#################################
# Send command confirmation beeps
#
if [ -f $LOCAL/active ] ; then
   $SCRIPT/end
   sleep 8
fi
sendtone
writelog $ACTION
my_key
sleep 1
play $AUDIO/custom/sound-shutdown.wav
sleep 1
my_unkey
$SCRIPT/disable
sudo $ACTION
exit 0

