#!/bin/bash
##################################################################
##################################################################
#   Custom_Decode for IRLP Node 6732 - VK2YLD
#
# This is the custom decode file.
# Make sure all valid codes finish with "exit 1".
#
#
##################################################################
##### Startup checks...                                      #####
##################################################################
#
# if an interactive function alive, intercept response

if [ -f $LOCAL/interactive ] ; then
   echo $1 > $LOCAL/rxcmd
   exit 1
fi

# DTMF Decoding re-enable
if [ "$1" = "xxxxx" ] ; then $CUSTOM/dtmf_enable ; exit 1 ; fi

# exit here if DTMF Decoding is disabled
if [ -f $LOCAL/nodtmf ] ; then exit 1 ; fi

##################################################################
########## Secret codes..  not repeated back to air ##############
##### ** Pin protected - MUST use activate pin code first ** #####
##################################################################
#
case $1 in
xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           touch $LOCAL/pin_rx               # Function activate pin code
           key
           usleep 2500
           play $AUDIO/custom/sound-livepin.wav
           usleep 2500
           unkey
           $CUSTOM/kill_pin &                # waits 2 mins, then removes pin
           exit 1 ;;

xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           $CUSTOM/dtmf_disable              # Kill DTMF control (stops '73s)
           rm $LOCAL/pin_rx > /dev/null 2>&1 # Pin used.. remove pin flag
           exit 1 ;;

xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           if [ -f $LOCAL/active ] ; then exit 1; fi  # no kill if active!
           $SCRIPT/disable                   # Disable node
           if [ -f /home/EchoIRLP/scripts/echo_disable ]
             then /home/EchoIRLP/scripts/echo_disable ;fi
           rm $LOCAL/pin_rx > /dev/null 2>&1 # Pin used.. remove pin flag
           exit 1 ;;

xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           $SCRIPT/enable                    # Enable node
           if [ -f /home/EchoIRLP/scripts/echo_enable ]
             then /home/EchoIRLP/scripts/echo_enable ;fi
           rm $LOCAL/pin_rx > /dev/null 2>&1 # Pin used.. remove pin flag
           exit 1 ;;

xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           $CUSTOM/killnode -r               # Re-boot node now
           exit 1 ;;

xxxxx)     $CUSTOM/unlog                     # Remove the decode log entry
           $CUSTOM/killnode -s               # Shutdown and Power-off now
           exit 1 ;;

esac


# Repeat DTMF back to caller to verify correct code reception
$CUSTOM/dtmf_ack $1

#################################################################
##### Normal Custom Decodes start here...                   #####
#################################################################
#
#  Now, How can I be helping you?

case $1 in
S69)       $CUSTOM/star69                    # Service a call-waiting with *69
           exit 1 ;;

92760)     $CUSTOM/sendping &                # Ping test anyone?
           exit 1 ;;

#75[0-9])   $CUSTOM/bom ${1:2:1}              # Met bureau - needs last digit
#           exit 1 ;;

80[1-8])   $CUSTOM/play_hb ${1:2:1} &
           exit 1 ;;

809|8[1-3][0-9]|84[0-5])  $CUSTOM/play_q5 ${1:1:2} &
           exit 1 ;;

900)       $CUSTOM/stopnews                  # Kill news that is playing
           exit 1 ;;                         # In full duplex mode only, or with 2nd rx

901)       $CUSTOM/playnews wiaacb &
           exit 1 ;;

902)       $CUSTOM/playnews q &
           exit 1 ;;

903)       $CUSTOM/playnews raotc &
           exit 1 ;;

904)       $CUSTOM/playnews arnl &
           exit 1 ;;

905)       $CUSTOM/playnews nzart &
           exit 1 ;;

906)       $CUSTOM/playnews arrlan &
           exit 1 ;;

907)       $CUSTOM/playnews rsgb &
           exit 1 ;;

908)       $CUSTOM/playnews vk5 &
           exit 1 ;;

909)       $CUSTOM/playnews vk6 &
           exit 1 ;;

910)       $CUSTOM/playnews vk7 &
           exit 1 ;;

911)       $CUSTOM/playnews twiar &
           exit 1 ;;

000|9|9[0-5])  $CUSTOM/tx_info $1            # Play node information recordings
           exit 1 ;;

SS)        $CUSTOM/time_cron $1              # Forward DTMF to signal 'not cron call'
           exit 1 ;;

SP)        $CUSTOM/uptime                    # How long node been operational?
           exit 1 ;;

00)        $CUSTOM/check_irlp
           exit 1 ;;

99[1-9])   $CUSTOM/lastline ${1:2:1}         # Speak last x lines system log
           exit 1 ;;

99)        $CUSTOM/lastline 3                # Speak last 3 lines of log
           exit 1 ;;
esac

##################################################################
##### Echolink Prefix detector...                            #####
##### If you change the EchoIRLP prefix, you must also       #####
##### change "num" to the number of digits in the prefix.    #####
##################################################################

eval `echo $1 | awk -v num=1 '{
     print "PRE="substr($1,1,num);
     print "NODE="substr($1,(num)+1,length($1))}'`

#Call echolink conferences using pound (#)node method
if [ "$PRE" = "P" ] ; then "$ECHO_SCRIPT"/echo_call $NODE ; exit 1 ; fi

if [ -x "$ECHO_SCRIPT/echo_end" ] ; then
  if [ "$1" = "73" ] ; then "$ECHO_SCRIPT"/echo_end ; exit 1 ; fi
fi

##################################################################
##### make sure this is the last test done                   #####
##################################################################
#
# TESTS in here only check the first 2 DIGITS of decoded DTMF
# USE WITH CAUTION !!
#
case ${1:0:2} in                            # Test first 2 digits only
1P|2P|3P)  $CUSTOM/playremotedata $1        # Remote node time, status & location
           exit 1 ;;
esac

# Phew!
#
##################################################################
##### If script hasn't branched by this point, decode is     #####
##### not here.. Pass it back to normal decode script!       #####
##################################################################

