#!/bin/bash

# packetrig.sh
# dranch@trinityos.com
#
#  This comprehensive script supports setting up both physical TNCs and soundcard
#  based TNCs.  It supports setting up unique digipeater paths, etc.  This script
#  is part of the hampacketizing-centos documentation suite.  Please see that 
#  document for all the details, licensing, etc.
#  http://www.trinityos.com/HAM/index-ham.html

VERSION="2.52"

# This script controls many things for the Linux HamShack computer. This script
# contains the following sections:
#
# --
# Changes and Errata
# Requirements for this script
# Binary Paths and variables
# Functions
# Main program
# --

#-------------------------------------------------------------------------
# Changes and Errata --- Older errata is at the BOTTOM of this file
#-------------------------------------------------------------------------
# 2.52 - 11/17/20  - completed swap from tmd710_tncsetup to $TNCSETUP
#
# 2.51 - 11/11/20 - re-enabled the timestamp in the STARTMONITOR function
#
# 2.50 - 04/08/20 - Extended DEBUG level 2 statements and added more helpful
#                   non-debug statements
#
# 2.49 - 06/23/19 - Fixed misalignments for direwolf running on HF
#                 - Aligned all the CHECK-ERR requests to use the "hard" parameter
#                   and added lots of missing checks through the code
#                 - added the DISPLAYPARMS output function
#                 - Other minor cleanups
#
# 2.48 - 05/17/19 - Added crc_force=1 (no SMACK) when loading the mkiss module
#
# 2.47 - 03/24/19 - Minor formatting changes when the serial port is foumd in use
#
#
# Many more errata lines can be found at the end of this file


#-------------------------------------------------------------------------
# Requirements for this script:
#-------------------------------------------------------------------------
#
#  This script controls many things for the Linux HamShack computer.  It is
#  specifically used for everything AX.25 but it also restores mixer sound-levels
#  for HF modes, etc. too!
#
#  All you you really have to change to get things working are the various 
#  variables at the top of this script for your specific callsign, computer 
#  serial ports, UNPROTO paths, etc.  After that's done, you shouldn't have to 
#  do anything else (famous last words eh?)!
#
#
#  Here is a quick list of supported features and thus requirements for this 
#  script to completely function:
#
#  KISS - allow multiple programs to connect to the TNC
#     - AX.25 and KISS enabled kernel per
#       http://www.trinityos.com/HAM/CentosDigitalModes/hampacketizing-centos5.html#1.ax25kernel
#
#  AX25-tools - Support various ax25 setup
#       http://www.trinityos.com/HAM/CentosDigitalModes/hampacketizing-centos5.html#2.ax25tools
#
#  Direwolf - software-based TNC (recommended)
#     - Installing the soundmodem software per
#       http://www.trinityos.com/HAM/CentosDigitalModes/hampacketizing-centos.html#6b.compile-direwolf
#
#  Soundmodem - software-based TNC (obsolete)
#     - Installing the soundmodem software per
#       http://www.trinityos.com/HAM/CentosDigitalModes/hampacketizing-centos.html#6c.compile-soundmodem
#
#  tmd710_tncsetup - Kenwood D710 KISS initialization program
#     - http://www.trinityos.com/HAM/CentosDigitalModes/usr/src/misc/D710/tmd710_tncsetup.c
#
#  alsactl - soundcard volume settings
#     - part of most Linux distributions
#
#  Xastir - APRS software for linux
#     - AX25-tools (above)
#
#  JNOS - comprehensive packet BBS software
#     - http://www.trinityos.com/HAM/CentosDigitalModes/hampacketizing-centos5.html#15.jnos
#
#  ntpdate - Internet based time
#     - needed for time critical applications like WSJT's JT65
#
#  (optional) - APRX
#     - Used to inject APRS-IS object data and digipeat UI frames
#
#-------------------------------------------------------------------------

#where you want all logging to go
LOG="/var/log/packet.log"

#Debugging : 
#
#  value of 0 disables debugging
#  value of 1 to enable common debugging
#  value of 2 is used for debugging specific sections
DEBUG=2



#-------------------------------------------------------------------------
# Binary paths - change these only if your distribution places your programs
#                somewhere else or renames them (I'm looking at you Ubuntu..)
#-------------------------------------------------------------------------
LSMOD="/sbin/lsmod"
MODPROBE="/sbin/modprobe"
IFCONFIG="/sbin/ifconfig"
ROUTE="/sbin/route"
ALSACTL="/sbin/alsactl"
ALSALVLS="/etc/asound.state"
KISSATTACH="/usr/sbin/kissattach"
MKISS="/usr/sbin/mkiss"
KISSPARMS="/usr/sbin/kissparms"
AXPARMS="/usr/sbin/axparms"
AX25D="/usr/sbin/ax25d"
NETROMD="/usr/sbin/netromd"
NRATTACH="/usr/sbin/nrattach"
LISTEN="/usr/bin/listen"
ACPICTL="/etc/rc.d/init.d/acpid"
KILLALL="/usr/bin/killall"
TNCSETUP="/usr/local/sbin/tmd710_tncsetup"
APRX="/usr/sbin/aprx"
LDSPED="/usr/sbin/ldsped"
DIREWOLF="/usr/bin/direwolf"
#(optional) Script to control a US Interface Navigator
USISCRIPT="/usr/local/sbin/usinterface-commands.sh"

#-------------------------------------------------------------------------
# Application paths - change these only if your distribution places your programs
#                somewhere else or renames them 
#-------------------------------------------------------------------------
JNOSPATH="/usr/local/jnos"


#-------------------------------------------------------------------------
# Variables
#-------------------------------------------------------------------------
#My Xwindows logged in user-id :: required for disabling power management
#                                 under KDE.. dunno why
XUSERNAME="dranch"

#MyCallsign
MYBEACONCALL="KI6ZHD"

#Officially assigned AMPR.ORG assigned TCP/IP and netmask
AMPRIP="44.4.10.39"
AMPRNW="44.4.10.0"
AMPRNM="255.255.255.128"


#VHF packetrig serial port AX25 - this should almost ALWAYS be ax0
VHFD710AXINT=ax0

#VHF packetrig serial port AX25 name
#  This is IMPORTANT and I recommend you name it something that reflects your setup
#  It CANNOT be named something like ax0, sm0, etc.  It needs to be unique
VHFD710AXPORT=d710

#VHF packetrig serial port for Netrom
VHFD710NRINT=nr0

#VHF packetrig serial port for netrom interfaces
VHFD710NRPORT=netrom

#VHF packetrig serial port Unix device - this is using Udev aliases to make it easy to read
VHFD710PORT="/dev/USI_SER"

#VHF packetrig serial port COMM speed
#  Speed needs to be set in /etc/ax25/axports too
VHFD710BAUD="9600"

#TNC RF BAUD - initializing with an X for now
VHFD710RFBAUD="1200"

#--------

#VHF Direwolf port AX25 name - usually is dw0 but must not conflict
#
#  For example with multiple radios:
#      dw0 for    HF
#      dw1 for    2m: 144.910
#

VHF857AXINT=ax0

#HF packetrig serial port AX25 name
HFDWMAXPORT=ft950
VHFDWMAXPORT=dw0
#--------

#VHF Soundmodem packetrig serial port AX25 name - usually is sm0 but must not conflict
#
#  For example with multiple radios:
#      sm0 for    2m: 144.910
#      sm1 for 1.25m: 223.668
#      sm2 for  70cm: 441.500
#
#HFSMDMAXPORT=sm1
#VHFSMDMAXPORT=sm1

#--------
# Changing sound card levels, etc

#(optional) Which ALSA interface number should the built-in soundcard - for soundmodem
BUILTINANUM=0

#(optional) Which ALSA interface number should the US Navigator be (was 2) - used for HF modes
USNAVALSANUM=1


#-------


# BEACONS
#
#   The configuration of the BEACON PATH also sets the various KISS Advanced
#   tuning, if NETROM will START, etc.
#
#When on the 145.050 frequency, lets use the local NODE system

#was 070618
ALT1BEACONPATH="KLPRC3 KBERR KRDG KCORN KVOLC KBETH TAH0E"
#Deal with BERRY being down
#ALT1BEACONPATH="KLPRC3 KBETH KVOLC KHILL KBETH TAH0E"
#was 01/05/14
#ALT1BEACONPATH="WOODY KBERR KRDG KCORN KVOLC KBETH TAH0E"
# was 07/09/13 ALT1BEACONPATH="KLPRC KMOC KTUO KBERR KRDG KCORN TAH0E"
#  06/15/13 was ALT1BEACONPATH="WOODY KBERR KTUO KMOC KBETH KLIVE KPAC"
#  was ALT1BEACONPATH="WOODY KLIVE KBERR KBETH KTUO KMOC"
#  was ALT1BEACONPATH="WOODY KPAC KLIVE KBERR KBETH KTUO KMOC"
#
#This is used to broadcast across the 144.910 band
ALT2BEACONPATH="LPRC2"

#This is used to localize any broadcast band - SCC packet network - No digipeating beacons
ALT3BEACONPATH=""

#This is used to localize any HF broadcast band - No digipeating beacons
ALT4BEACONPATH=""

#This is used to localize any HF broadcast band - No digipeating beacons
ALT5BEACONPATH=""

#How often to beacon in minutes - the more you beacon, the more you clog the
# channel.  I wouldn't recommend anything less than say every 45 min
#  was 90 - 03/07/13
BEACONTIME=120

#What to beacon
#BEACONSTRING="KI6ZHD/b KI6ZHD-1/pbbs - Santa Clara, CA" 
#BEACONSTRING="KI6ZHD/k KI6ZHD-1/p SCLARA/n 44.4.10.40/ip : 73 from Santa Clara "
BEACONSTRING="KI6ZHD/k KI6ZHD-1/b SCLARA/n 44.4.10.39/ip : Linpac in Santa Clara"

#This is used to broadcast across the band from a highlevel digipeater
#  Leave this as the default EMPTY unless you know what you're doing
#
#  Set to NULL to send broadcasts to local simplex only - no digipeating
BASEBEACONPATH=""

#-------

#How often to send Netrom updatesn in minutes - the more you send routes, the 
# more you clog the channel but changing conditions can effect valid routes.  
# Default is 60
NETROMADVTIME=120

#-------------------------------------------------------------------------
# Advanced AX.25 framing and delay settings
#-------------------------------------------------------------------------
#  Note:  This is all hard core settings and you need to be very aware of
#         what these settings do if you want to change them.  You've been
#         warned!
#
# http://tldp.org/HOWTO/AX25-HOWTO/x235.html#AEN258
#
#  Linux  | TAPR TNC | Description
# --------+----------+-----------------------------------------------------
#   T1    |  FRACK   | How long to wait before retransmitting an 
#         |          | unacknowledged frame
#         |          | - lnx default(msec): 10000 (10s)
#         |          |
#   T2    | RESPTIME | The minimum amount of time to wait for another frame 
#         |          | to be received before transmitting an acknowledgement.
#         |          | - lnx default(msec): 3000 (3s)
#         |          |
#   T3    |  CHECK   | The period of time we wait between sending a check 
#         |          | that the link is still active.
#         |          | - lnx default(msec): 300000 (300s)
#         |          |
#   N2    |  RETRY   | How many times to retransmit a frame before assuming 
# maximum |          | the connection has failed. 
#  retry  |          | - lnx default: 10
#  count  |          |
#         |          |
#  Idle   |  <none>  | The period of time a connection can be idle before we 
#         |          | close it down.
#         |          |
#  Window | MAXFRAME | The maximum number of unacknowledged transmitted frames
#         |          | - lnx default: 2
# --------+----------+-----------------------------------------------------
#
# --
#  The following settings conform to the Santa Clara County VHF Packet settings
#     http://www.scc-ares-races.org/packet/SCC_Std_TNC_Params_v100505.pdf
#  -----------------------------------------------------------------------------
#
#     # NOTE:  many of these items are NON-optimal - they are for a lowest 
#              common denominator for that specific system
#
#          TAPR / Linux   : Santa Clara settings
#       ------------------:------------------------
#   -   slottime time     : 100 (ms)
#   -   persist  time     :  63 (generic weighting)
#   -   resptime / T2     : 500 (ms)
#   -   Packet Lenth      : 128 (bytes)
#   -   Maxframe / Window :   2 (frames)
#   -   FRACK / T1        :   6 (seconds)
#   -   Retry / N2        :  10 (retries)
#   -   Check / T3        : 300 (seconds)
#   -   TX delay          : 300 (ms)
#   x   Sendpac           : $0D (hex)     -- Seem to be TNC specific only parameters
#   x   Pactime           :   1 (100ms)   -- Seem to be TNC specific only parameters
#
#
#
#  Recommended HF settings:
#  --------------------------
#   Will be implimented later
#  --------------------------
#    - In axports, maximum packet length 60 and window 1
#    - In /proc/sys/net/ax25/<port>/
#           t1_timeout to 5000 (5 seconds) - FRACK
#    -      maximum_retry_count to 15 (RETRY)
#    - In /etc/ax25/soundmodem.conf set ppersist to 63 and 
#        slottime to 30, and evidently txtail is useful too (for HF only)
#    - http://www.vectorbd.com/bfd/packet/hfpacket.txt
#                      MAXFRAME 1 to 2 
#                      PACLEN 32, 64 or 80
#                      TXDELAY 40 or 50
#                      FRACK 6, 7 or 8
#
# Values set in the /etc/ax25/axports file
#   - callsign
#   - PacLen (MTU)
#   - maxframe (window
#
# Other AX.25 settings found in "cat /proc/sys/net/ax25/ax0/" 
#    but parameters like TXDELAY, SLOTTIME, PERSIST, are ONLY set by the kissparms program 
#    which evidently writes that data to the KISS TNC yet the kissparms program does NOT 
#    allow the ability to SHOW any set results
#
#      Research: maybe reading this is possible with some work:
#       http://search.cpan.org/~rbdavison/Device-TNC-0.03/lib/Device/TNC/KISS.pm
#
#
#   WARNING:
#   --------
#   kissparms manpage says it uses 10s of MSECs
#   AX.25 howto says it uses MSECs
#   --> Could depend on the kernel itself due legacy reasons
#       http://osdir.com/ml/hams/2005-10/msg00080.html
#
#Set some advanced kiss parameters
#---------------------------------
# These values assume we should be using miliseconds - not confirmed
#  Linux kernel defaults to the following from the source file: include/net/ax25.h
#
# #define AX25_DEF_IPDEFMODE      0                       /* Datagram */
# #define AX25_DEF_AXDEFMODE      0                       /* Normal */
# #define AX25_DEF_BACKOFF        1                       /* Linear backoff */
# #define AX25_DEF_CONMODE        2                       /* Connected mode allowed */
# #define AX25_DEF_WINDOW         2                       /* Window=2 */
# #define AX25_DEF_EWINDOW        32                      /* Module-128 Window=32 */
# #define AX25_DEF_T1             10000                   /* T1=10s */
# #define AX25_DEF_T2             3000                    /* T2=3s  */
# #define AX25_DEF_T3             300000                  /* T3=300s */
# #define AX25_DEF_N2             10                      /* N2=10 */
# #define AX25_DEF_IDLE           0                       /* Idle=None */
# #define AX25_DEF_PACLEN         256                     /* Paclen=256 */
# #define AX25_DEF_PROTOCOL       AX25_PROTO_STD_SIMPLEX  /* Standard AX.25 */
# #define AX25_DEF_DS_TIMEOUT     180000                  /* DAMA timeout 3 minutes */

#Notes on some of these values:
#
#  AX25_DEF_AXDEFMODE - This is for setting the max AX.25 sequence number range
#        0 - default - max AX.25 sequence value of 7 (modulo-8) 
#                         - initial connection uses the SABM code
#
#        1 -         - max AX.25 sequence value of 127 (modulo-128)
#                         - initial connection uses the SABME code
#
#    Note: If you try to connect to a remote machine that doesn't support the SABME code,
#          it will usually just disconnect you.  Linux will then re-attempt the connection 
#          with SABM - see: http://www.ir3ip.net/iw3fqg/doc/ax25ext.htm
#
#           KPC3+ v9.1 - does NOT support SABME


# -------------------------------------------------------------
# ALT1BEACONPATH Settings -- only will be used depending if this path is used
# -------------------------------------------------------------
#    NOTE: it's unclear which time units are being used here
#
#  ax25-tools-0.0.10rc2/kiss/kissparams.c code divides all provided values by 10
#    so this seems to be correct
#
#  Half duplex, TXDELAY of 200ms, SLOTTIME of 100ms, PERSIST of 63 (out of 256), TXtail of 20ms
#was
#AX25VHF1KISSPARMS="-f n -t 15 -s 10 -r 63"  ;; times are in ms and man page says they 
#                                               are divided by 10 but they ARENT for VE7FET
#                                               code - must fall on 10ms boundaries
AX25VHF1KISSPARMS="-f n -t 200 -s 100 -r 63 -l 20"

#  The following values are for an optimial packet network
#  -------------------------------------------------------
#
# plz note: the PACLEN value in the /etc/ax25/axports doesn't seem to alter the 
#           /proc values though OUTBOUND calls do seem to honor the axports MTU but
#           so lets set them directly to be consistent
#
#Array: Setting Max-PACLEN, T1(FRACK), T2 (RESPTIME), T3(CHECK), N2(RETRY), STD-Window
#06/05/16 was
#AX25VHFADV1KISSPARMS=(255 6000 500 300000 10 4)
AX25VHFADV1KISSPARMS=(255 6000 500 300000 10 4)


# -------------------------------------------------------------
# Settings that will be used depending if no-beacon-digi is set
# -------------------------------------------------------------
#
#  Half duplex, TXDELAY of 300ms, SLOTTIME of 100ms, PERSIST of 63 (out of 256), TXtail of 20ms
#was AX25VHF2KISSPARMS="-f n -t 30 -s 10 -r 63" ;; time is in ms and man page says they 
#                                                  are divided by 10 but they ARENT for VE7FET
#                                                  code - must fall on 10ms boundaries
AX25VHF2KISSPARMS="-f n -t 300 -s 100 -r 63 -l 20"
#
#
# - These values are for the Santa Clara County Packet backbone system (ALT3 beacon)
#
# plz note: the PACLEN value in the /etc/ax25/axports doesn't seem to alter the 
#           /proc values though OUTBOUND calls do seem to honor the axports MTU but
#           so lets set them directly to be consistent
#
#    * with a value of 255 in axports but 128 in /proc and if you try to create
#      a long frame, the ax.25 call program will abort it's connection
#
#Array: Setting Max-PACLEN, T1(FRACK), T2 (RESPTIME), T3(CHECK), N2(RETRY), STD-Window
AX25VHFADV2KISSPARMS=(128 6000 500 300000 10 2)


# -------------------------------------------------------------
# ALT5BEACONPATH Settings -- only will be used depending if this path is used
# -------------------------------------------------------------
#    NOTE: it's unclear which time units are being used here
#
#  ax25-tools-0.0.10rc2/kiss/kissparams.c code divides all provided values by 10
#    so this seems to be correct
#
#  Half duplex, TXDELAY of 800ms, SLOTTIME of 100ms, PERSIST of 63 (out of 256), TXtail of 20ms
#was
#AX25VHF1KISSPARMS="-f n -t 15 -s 10 -r 63"  ;; times are in ms and man page says they 
#                                               are divided by 10 but they ARENT for VE7FET
#                                               code - must fall on 10ms boundaries
AX25VHF3KISSPARMS="-f n -t 800 -s 100 -r 63 -l 20"

#  The following values are for an optimial packet network
#  -------------------------------------------------------
#
# plz note: the PACLEN value in the /etc/ax25/axports doesn't seem to alter the 
#           /proc values though OUTBOUND calls do seem to honor the axports MTU but
#           so lets set them directly to be consistent
#
#Array: Setting Max-PACLEN, T1(FRACK), T2 (RESPTIME), T3(CHECK), N2(RETRY), STD-Window
#06/05/16 was
#AX25VHFADV1KISSPARMS=(255 6000 500 300000 10 4)
AX25VHFADV3KISSPARMS=(255 6000 500 300000 10 4)


# -------------------------------------------------------------
# ALT4BEACONPATH Settings -- HF SOUNDMODEM - only will be used depending if 
#                            this beacon path is used
# -------------------------------------------------------------
#
#  ax25-tools-0.0.10rc2/kiss/kissparams.c code divides all provided values by 10
#    so this seems to be correct
#
#  Half duplex, TXDELAY of 150ms, SLOTTIME of 100ms, PERSIST of 63 (out of 256), TXtail of 20ms
#was
AX25HF1KISSPARMS="-f n -t 400 -s 100 -r 63 -l 20" #times are in ms and man page says they 
#                                                 are divided by 10 but they aren't for VE7FET
#                                                 code - must fall on 10ms boundaries

#  The following values are for an optimial HF packet network
#  -------------------------------------------------------
#
# note#1: the PACLEN value in the /etc/ax25/axports doesn't seem to alter the 
#         /proc values though OUTBOUND calls do seem to honor the axports MTU but
#         so lets set them directly to be consistent
#
# note#2: The minimum packet length to be able to configure an IP address on am
#         ax25 interface is 68bytes
#         https://en.wikipedia.org/wiki/Maximum_transmission_unit#cite_note-5
#
#Array: Setting Max-PACLEN, T1(FRACK), T2 (RESPTIME), T3(CHECK), N2(RETRY), STD-Window
AX25HFADV1KISSPARMS=(64 6000 500 300000 10 1)


#==================================================================================
#==================================================================================


#==================================================================================
#----------------------------------------------------------------------------------
# NO OTHER CHANGES should be made below this line unless you know what you're doing
#----------------------------------------------------------------------------------
#==================================================================================

#Where we keep state of what rig is being used
KISSPKTRIG="/var/lock/packetrig-state.flag"
PWRSTATE="/var/lock/power-management.state"

#------------------------------
#Some high level temp variables
#------------------------------

#Variable to control which port we're going to use - initializing with value X for now
CURRAXINT="X"



#------------------------------------------------------------------------
# Various Sub-Functions
#------------------------------------------------------------------------

function USAGE {
  echo -e "\npacketrig.sh - $VERSION"
  echo -e "dranch@trinityos.com\n"
  echo -e "USAGE:   packetrig.sh [mode or command] [beacon path]"
  echo -e " "
  echo -e "     Mode                 Description"
  echo -e "     ------------------ : ---------------------------------------------------------"
  echo -e "     vhf-d710-packet    - 1200 BAUD VHF packet on D710 with mheard, beacon, etc."
  echo -e "     vhf-d710-96packet  - 9600 BAUD UHF packet on D710 with mheard, beacon, etc."
  echo -e "     vhf-d710-xastir    - 1200 BAUD VHF packet on D710 for Xastir APRS -no beacon"
  echo -e "     --"
  echo -e "     vhf-smdm-packet    - VHF packet mode on soundmodem with mheard, beacon, etc."
  echo -e "     vhf-smdm-aprs      - VHF packet mode on soundmodem with APRS - no beacons"
  echo -e "     --"
  echo -e "     --"
  echo -e "     dw-857-psk-packet  - FT857 VHF packet mode using direwolf with mheard, etc."
  echo -e "     dw-hf-packet       - FT950 HF packet mode using direwolf with mheard, beacon, etc."
  echo -e "     smdm-hf-packet     - HF packet mode using soundmodem with mheard, beacon, etc."
  echo -e "     --"
  echo -e "     jnos-d710          - 1200BAUD VHF packet on D710 with netkiss for AMPR, JNOS"
  echo -e "     jnos-smdm          - 1200baud VHF packet on soundmodem with netkiss for AMPR, JNOS"
  echo -e "     "
  echo -e "     "
  echo -e "     Beacon path - used in conjunction with a chosen mode above:"
  echo -e "     --"
  echo -e "     alt1beacon         - KISS profile for 145.050 - NorCal digi, netrom, tcp/ip"
  echo -e "     alt2beacon         - KISS profile for 144.910 - Norcal LRPC2 digi path for beacons"
  echo -e "     alt3beacon         - KISS profile for 144.990 - S.Clara County - no digi path for beacons"
  echo -e "     alt4beacon         - KISS profile for HF      - 300baud packet : no digi path for beacons"
  echo -e "     alt5beacon         - KISS profile for VHF     - 2400baud PSK packet : no digi path for beacons"
  echo -e "     no-beacon-digi     - KISS profile for HF **   - Norcal Generic no digi path for beacons"
  echo -e "     "
  echo -e "     Commands:"
  echo -e "     --"
  echo -e "     1beacon <digip>    - TX 1 beacon - will digipeat via custom digi if given"
  echo -e "     vhf-d710-rekiss    - re-enable KISS mode on D710 if it was power cycled"
  echo -e "     show-kiss-parms    - show configured KISS parameters"
  echo -e "     --"
  echo -e "     d710-tnc-off       - shuts down all packet modes and turns OFF the tnc"
  echo -e "     stop               - shuts down all packet modes"
  echo -e " "
  echo -e "Examples:"
  echo -e "     : Start up VHF packet on the D710 with WOODY digipeated beacons\n"
  echo -e "       packetrig.sh vhf-d710-packet alt1beacon\n"
}

function CHECK-ERR () {
  #Makes the STDOUT output more clear if there is an error - if invoked with
  # "hard" and there is an error, stop the program
  if [ $? -ne 0 ]; then
     echo -n "<-- ERR"
     ERRORED=1
    else
     ERRORED=0
  fi

  if [ "$1" = "hard" ] && [ $ERRORED -eq 1 ]; then
     echo "Hard error.  Exiting"
     exit 1
  fi
}

function TNC-OFF () {
   #options:
   #  force-d710 - force the rig being manupulated to be the D710
   #
   GETRIGSTATE $1
   if [ "$PDEV" = "d710" ]; then
      echo -ne "\n[D710 TNC off] "
      echo -ne "\n[D710 TNC off] " >> $LOG 2>&1
      #Turn off the TNC
      sleep 1
      $TNCSETUP -B 0 -S $VHFD710PORT -i 1 -h >> $LOG 2>&1
      CHECK-ERR hard
     else
      echo -ne "[sndmdm down] "
   fi
}

#Power Management - To enable/disable the machine from suspending
function PM-CONTROL () {
   if [ "$1" = "disable-suspend" ]; then

      ##NOTE: This /etc/pm/config.d/config approach doesn't seem to work in Centos6, 
      ##      leaving it in place for now
      #if [ -f /etc/pm/config.d/config ]; then
      #   rm -f /etc/pm/config.d/config 
      #   #This might be fairly RHEL/Centos specific
      #   if [ $? -ne 0 ]; then
      #      echo -e "\nERROR: Problem with the /etc/pm/config.d path.  Wrong distro?"
      #      exit 1
      #   fi
      #fi

      #For KDE4, use Dbus to signal Powerdevil to change the configured power profile
      #  NOTE: You MUST create a profile called "Performance-nosleep" that has 
      #        "When system is idle for more than 90min: DO NOTHING"
      #
      # This command will only work for the user who is logged into KDE4
      #  Should also create a pop-up on the desktop

      sudo -u $XUSERNAME qdbus org.kde.powerdevil /modules/powerdevil setProfile Performance-nosleep

      if [ $? -eq 1 ]; then
         echo -e "--"
         echo -e "WARNING: Any errors such as:"
         echo -e "\n       Could not connect to D-Bus server: org.freedesktop.DBus.Error.Spawn.ExecFailed: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed."
         echo -e "\nreflects that this command is NOT being run from a terminal "
         echo -e "that has X forwarding enabled for the KDE popup.  This should "
         echo -e "be benign and power management should change regardless"
         echo -e "--"
        else
         echo "pm-suspend-disabled" > $PWRSTATE
      fi
          
      #DISABLED: kludgy way at disabling suspend - works
      #if [ -f /usr/sbin/pm-suspend ]; then 
      #   mv /usr/sbin/pm-suspend /usr/sbin/pm-suspend.disabled 
      #   mv /usr/sbin/pm-suspend-hybrid /usr/sbin/pm-suspend-hybrid.disabled 
      #  else
      #   echo -e "\nWARNING: /usr/sbin/pm-suspend should be present but isn't"
      #   echo -e "\nWARNING: /usr/sbin/pm-suspend should be present but isn't" >> $LOG 2>&1
      #fi

      #NOTE: This /etc/pm/config.d/config approach doesn't seem to work in Centos6, 
      #      leaving it in place for now
      #echo -e '#Created via the trinityos.com packetrig.sh script' > /etc/pm/config.d/config
      #echo -e 'HIBERNATE_RESUME_POST_VIDEO="no"' >> /etc/pm/config.d/config
      #echo -e 'SUSPEND_MODULES=""' >> /etc/pm/config.d/config
      #echo -e '\n#Leave hibernate on just in case the power goes out and the battery gets depleted' >> /etc/pm/config.d/config
      #echo -e 'DISABLE_HIBERNATE="no"' >> /etc/pm/config.d/config
      #echo -e 'DISABLE_SUSPEND="yes"' >> /etc/pm/config.d/config
      #$ACPICTL reload 2>&1 >> $LOG

      echo -ne "[Power control: Suspend Disabled] "
      echo -ne "[Power control: Suspend Disabled] " >> $LOG 2>&1
      echo -e " "
      echo -e " " >> $LOG 2>&1
   fi

   if [ "$1" = "enable-suspend" ]; then

      echo -e "\n\nEnter control-c within 5 seconds to disable returning the computer to a suspendable power savings state"
      sleep 5

      # This command will only work for the user who is logged into KDE4
      #  Should also create a pop-up on the desktop
      sudo -u $XUSERNAME qdbus org.kde.powerdevil /modules/powerdevil setProfile Performance

      if [ $? -eq 1 ]; then
         echo -e "--"
         echo -e "WARNING: Any errors such as:"
         echo -e "\n       Could not connect to D-Bus server: org.freedesktop.DBus.Error.Spawn.ExecFailed: /bin/dbus-launch terminated abnormally with the following error: Autolaunch error: X11 initialization failed."
         echo -e "\nreflects that this command is NOT being run from a terminal "
         echo -e "that has X forwarding enabled for the KDE popup.  This should "
         echo -e "be benign and power management should change regardless"
         echo -e "--"
        else
         echo "pm-suspend-enabled" > $PWRSTATE
      fi

      #DISABLED: kludgy way at disabling suspend - works
      #if [ -f /usr/sbin/pm-suspend.disabled ]; then 
      #   mv /usr/sbin/pm-suspend.disabled /usr/sbin/pm-suspend
      #   mv /usr/sbin/pm-suspend-hybrid.disabled /usr/sbin/pm-suspend-hybrid
      #  else
      #   echo -e "\nWARNING: /usr/sbin/pm-suspend.disabled should be present but isn't"
      #   echo -e "\nWARNING: /usr/sbin/pm-suspend.disabled should be present but isn't" >> $LOG 2>&1
      #fi
      
      #NOTE: This /etc/pm/config.d/config approach doesn't seem to work in Centos6, 
      #      leaving it in place for now
      #rm -f /etc/pm/config.d/config

      echo -ne "[Power control: Suspend Enabled] "
      echo -ne "[Power control: Suspend Enabled] " >> $LOG 2>&1
      $ACPICTL reload 2>&1 >> $LOG
   fi
}

function STOPPROCESSES {
   echo -e "\n---------------------------------------------" >> $LOG 2>&1
   echo "Stopping: `date`" >> $LOG 2>&1
   echo -e "---------------------------------------------" >> $LOG 2>&1

   echo -e "\nShutting down any existing processes: "
   echo -ne "\nShutting down any existing processes. " >> $LOG 2>&1
   echo -en "[jnos] "
   echo -en "[jnos] " >> $LOG 2>&1
   $KILLALL jnos >> $LOG 2>&1

   # It's ultra important to tear down any existing AX.25 sessions before removing 
   # the kernel modules or I've seen modern Linux 3.7.x kernels panic!  This script
   # is currently only killing JNOS, linpac, call
   #
   #   See the clear-ax25-netrom-connection.sh script for specific ways to 
   #   clear ax.25 and/or netrom connections and a description of the
   #   issue

   echo -en "[linpac] "
   echo -en "[linpac] " >> $LOG 2>&1
   $KILLALL linpac >> $LOG 2>&1

   echo -en "[call] "
   echo -en "[call] " >> $LOG 2>&1
   $KILLALL call >> $LOG 2>&1

   echo -ne "[beacon] "
   echo -ne "[beacon] " >> $LOG 2>&1 
   $KILLALL beacon >> $LOG 2>&1 

   echo -ne "[mheard] "
   echo -ne "[mheard] " >> $LOG 2>&1 
   $KILLALL mheardd >> $LOG 2>&1 

   #ax-apps 0.0.6 was axlisten, but now as of 0.0.10, it's listen
   echo -ne "[listen] "
   echo -ne "[listen] " >> $LOG 2>&1 
   $KILLALL listen >> $LOG 2>&1 

   echo -en "[ldsped] "
   echo -en "[ldsped] " >> $LOG 2>&1
   $KILLALL ldsped >> $LOG 2>&1

   echo -en "[aprx] "
   echo -en "[aprx] " >> $LOG 2>&1
   $KILLALL aprx >> $LOG 2>&1

   echo -en "[netromd] "
   echo -en "[netromd] " >> $LOG 2>&1
   $KILLALL netromd >> $LOG 2>&1

   echo -en "[ax25d] "
   echo -en "[ax25d] " >> $LOG 2>&1
   $KILLALL ax25d >> $LOG 2>&1

   echo -e "[ax25ipd] "
   echo -e "[ax25ipd] " >> $LOG 2>&1 
   $KILLALL ax25ipd  >> $LOG 2>&1 

   echo -en "[net2kiss] "
   echo -en "[net2kiss] " >> $LOG 2>&1
   $KILLALL net2kiss >> $LOG 2>&1

   echo -en "[kissnetd] "
   echo -en "[kissnetd] " >> $LOG 2>&1 
   $KILLALL kissnetd  >> $LOG 2>&1 

   echo -ne "[KISS de-attach] "
   echo -ne "[KISS de-attach] " >> $LOG 2>&1
   sleep 1
   $KILLALL kissattach >> $LOG 2>&1

   #Second check
   if [ -n "`$IFCONFIG | grep ax0 2>&1`" ]; then
      echo -ne "[KISS de-attach try2] "
      echo -ne "[KISS de-attach try2] " >> $LOG 2>&1
      $KILLALL kissattach >> $LOG 2>&1 
   fi

   echo -ne "[MKISS de-attach] "
   echo -ne "[MKISS de-attach] " >> $LOG 2>&1
   sleep 1
   $KILLALL mkiss >> $LOG 2>&1

   echo -ne "[soundmodem] "
   echo -ne "[soundmodem] " >> $LOG 2>&1 
   $KILLALL soundmodem >> $LOG 2>&1 

   echo -ne "[direwolf] "
   echo -ne "[direwolf] " >> $LOG 2>&1 
   $KILLALL direwolf >> $LOG 2>&1 

   echo -ne "[kernel modules] "
   echo -ne "[kernel modules] " >> $LOG 2>&1
   echo -ne "\nunloading kernel modules" >> $LOG 2>&1
   sleep 1
   $MODPROBE -q -r netrom >> $LOG 2>&1
   $MODPROBE -q -r mkiss >> $LOG 2>&1
   sleep 1
   $MODPROBE -q -r ax25 >> $LOG 2>&1
   $MODPROBE -q -r crc16 >> $LOG 2>&1 

   #DISABLED - not really worth starting up the TNC to only shut it back down
   #
   #reset the TNC to 1200 just in case we push the TNC on the D710
   #  If you don't do this and the TNC was in 9600 before, it will restart
   #  in 9600
   #echo -ne "[D710 TNC to 1200] "
   #echo -ne "[D710 TNC to 1200] " >> $LOG 2>&1
   #/usr/local/sbin/tmd710_tncsetup -B 0 -S $VHFD710PORT -b 1200 -h >> $LOG 2>&1

   #Do we have any running state to be aware of
   #
   if [ -f $KISSPKTRIG ]; then
      #Ok, get which rig we're using
      echo -ne "[TNC off] "
      echo -ne "[TNC off] " >> $LOG 2>&1
      TNC-OFF
     else
      echo -ne "[TNC skip] "
      echo -ne "[TNC skip] " >> $LOG 2>&1
   fi

   #Allow the machine to suspend if idle long enough
   PM-CONTROL enable-suspend

   #Send a CRLF
   echo " "

   echo -e "\nclearing packetrig state flag file" >> $LOG
   rm -f $KISSPKTRIG

   if [ -n "`$IFCONFIG | grep ax0 2>&1`" ]; then
      echo -e "\nWARNING: ax interfaces still exist in ifconfig after SECOND killall kissattach attempt\n"
   fi
}


function INITIALIZEDATE {
   echo -e "\n---------------------------------------------" >> $LOG 2>&1
   echo "Starting: `date`" >> $LOG 2>&1
   echo -e "---------------------------------------------" >> $LOG 2>&1
   echo -e "\nStarting up the packet system:  See $LOG for full logs: "
   echo -e "\nStarting up the packet system:  See $LOG for full logs: " >> $LOG 2>&1 

   #Dont allow the machine to suspend if idle long enough
   PM-CONTROL disable-suspend

   echo -en "[ntp] "
   echo -en "[ntp] " >> $LOG 2>&1 
   /usr/sbin/ntpdate -b north-america.pool.ntp.org >> $LOG 2>&1 
}

function LOADKISSMODULES {

   if [ ! -L $VHFD710PORT ]; then
      echo -e "\nFATAL: serial port $VHFD710PORT not found\n"
      echo -e "Abort.\n\n"
      exit 1
   fi

   echo -en "[tty perms] "
   echo -en "[tty perms] " >> $LOG 2>&1 
   chmod 666 $VHFD710PORT >> $LOG 2>&1 
   #chmod 666 /dev/ttyUSB0 >> $LOG 2>&1 

   if [ -n "`ps aux | grep $VHFD710PORT | grep -v grep`" ]; then
     echo -e "\nERROR: VHFD710PORT $VHFD710PORT is in use:\n `ps aux | grep $VHFD710PORT | grep -v grep`\n"
     echo -e "\nERROR: VHFD710PORT $VHFD710PORT is in use:\n `ps aux | grep $VHFD710PORT | grep -v grep`\n" >> $LOG 2>&1
     exit 1
   fi
   echo -ne "[kernel modules] "
   echo -ne "[kernel modules] " >> $LOG 2>&1
   #Attempt to set the crcmode to NONE :: this can also be done with kissparm's -c option
   echo -e "\nLoading mkiss kernel module with CRC-mode: none" >> $LOG 2>&1
   $MODPROBE -q mkiss crc_force=1 >> $LOG 2>&1 
   #$MODPROBE -q mkiss >> $LOG 2>&1 
   if [ -z "`$LSMOD | grep mkiss`" ]; then
      echo "\nmkiss kernel module not found. aborting.\n"
      echo "\nmkiss kernel module not found. aborting.\n" >> $LOG 2>&1 
      exit 1
   fi
}

function INITSNDCARD () {
   echo -ne "[audio] "
   if [ $DEBUG -eq 1 ]; then
      echo -e "DEBUG: INITSNDCARD options: $@"
      echo -e "DEBUG: CMDLINE: $CMDLINE"
   fi

   if [ -n "`echo $@ | grep vhf`" ]; then
     #This soundcard line will be specific to this Dell laptop I'm using
     if [ "`cat /proc/asound/cards | grep "ICH4 - Intel 82801DB-ICH4" | awk -F [ '{print $1}'`" == "0" ]; then
        echo -e "\n\nERROR: Built-in souncard enumeration.  Sound should be device: $BUILTINANUM\n"
        echo -e "\n\nERROR: Built-in souncard enumeration.  Sound should be device: $BUILTINANUM\n" >> $LOG 2>&1
        cat /proc/asound/cards
        echo -e "\nAborting\n"
        echo -e "\nAborting\n" >> $LOG 2>&1
        exit 1
     fi
   fi

   if [ -n "`echo $@ | grep hf`" ]; then
     if [ "`cat /proc/asound/cards | grep "USB-Audio - USB Audio CODEC" | awk -F [ '{print $1}'`" == "1" ]; then
        #This is using the US Interface Navigator USB sound card
        echo -e "\n\nERROR: US Navigator souncard enumeration.  Sound should be device: $USNAVALSANUM\n"
        echo -e "\n\nERROR: US Navigator souncard enumeration.  Sound should be device: $USNAVALSANUM\n" >> $LOG 2>&1
        cat /proc/asound/cards
        echo -e "\nAborting\n"
        echo -e "\nAborting\n" >> $LOG 2>&1
        exit 1
     fi
   fi

   echo -e "\nLoading alsactl levels" >> $LOG 2>&1 
   if [ ! -f /etc/asound.state ]; then 
      echo -e "\nNo Alsa sound settings found.  You need to set them up and"
      echo -e "then use the 'alsactl store' command - even with Pulse/PortAudio setups"
     else
      $ALSACTL -f $ALSALVLS restore >> $LOG 2>&1
      echo -en "[USI lvl]"
      $USISCRIPT write-cfg >> $LOG 2>&1
   fi
}

function INITSOUNDMODEM {
   echo -ne "[soundmodem init] "
   echo -ne "[soundmodem init] " >> $LOG 2>&1
   if [ -n "`ps aux | grep -i pavucontrol | grep -v grep`" ]; then
      echo -e "\nERROR:  the Pulse Audio pavucontrol control cannot be running while"
      echo -e   "        soundmodemconfig tries to operate as soundmodem is NOT PulseAudio"
      echo -e   "        friendly"
      exit 1
   fi
   echo -e "\nStarting soundmodemconfig, check your levels" >> $LOG 2>&1 
   /usr/bin/soundmodemconfig >> $LOG 2>&1 

   echo -en "\n\nWere the levels ok?  [Y to continue : N to recheck levels]: "
   read ANSWER
   if [ "$ANSWER" = N ] || [ "$ANSWER" = n ]; then
      echo -e "\nRerun the start-packet.sh script\n\n"
      INITSOUNDMODEM
   fi
}

function STARTSOUNDMODEM {
   #Soundmodem sets all of it's own KISS paramters
   #
   #   Soundmodem has NOT been tuned for Santa Clara County backbone
   #
   echo -en "\nStarting PACKET mode: "
   echo -en "\nStarting PACKET mode: " >> $LOG 2>&1 
   echo -ne "[soundmodem] "
   echo -ne "[soundmodem] " >> $LOG 2>&1
   #bring up soundmodem (as root)
   /usr/sbin/soundmodem -v5 >> $LOG 2>&1 & 
   
   #Variable to control which port we're going to use
   CURRAXINT=$HFSMDMAXPORT
   if [ $DEBUG -ge 2 ]; then
      echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
   fi
}

function STARTDIREWOLF {
   #Soundmodem sets all of it's own KISS paramters
   #
   #   Soundmodem has NOT been tuned for Santa Clara County backbone
   #
   echo -en "\nStarting PACKET mode: "
   echo -en "\nStarting PACKET mode: " >> $LOG 2>&1 
   echo -ne "[direwolf] "
   echo -ne "[direwolf] " >> $LOG 2>&1

   #echo -e "\nDEBUG:This routine is broken as it needs to be told to use VHFDWMAXPORT or HFDWMAXPORT"
   #echo -e "DEBUG: var1 is: $1"
   #Variable to control which port we're going to use
   if [ "$1" == "hf" ]; then
      echo -e "\nSetting CURRAXINT to $HFDWMAXPORT"
      if [ $DEBUG -ge 2 ]; then
         echo -e "DEBUG: CURRAXINT currently is: $CURRAXINT"
         echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
      fi
      CURRAXINT=$HFDWMAXPORT
      if [ $DEBUG -ge 2 ]; then
         echo -e "DEBUG: CURRAXINT is now: $CURRAXINT"
      fi
     else
      echo -e "\nFallthrough: Setting CURRAXINT to $VHFDWMAXPORT"
      if [ $DEBUG -ge 2 ]; then
         echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
         echo -e "DEBUG: CURRAXINT currently is: $CURRAXINT"
      fi
      CURRAXINT=$VHFDWMAXPORT
      if [ $DEBUG -ge 2 ]; then
         echo -e "DEBUG: CURRAXINT is now: $CURRAXINT"
      fi
   fi
   echo -e "\n\nDEBUG: CURRAXINT is: $CURRAXINT"

   #bring up direwolf (as root) - direwolf.conf file is changed to match in the main function
   # -t 0 : disables text colors
   # -c   : config file
   # -p   : KISS mode
   # -q d : supress APRS symbol decodes
   echo -e "DEBUG: direwolf config is (match the file sizes):\n`ls -la /etc/ax25 | grep direwolf`\n"
   $DIREWOLF -t 0 -c /etc/ax25/direwolf.conf -p -q d >> $LOG 2>&1 &
   CHECK-ERR hard
   sleep 1
   echo "Installing one KISS connection on PTY port /tmp/kisstnc"
   echo "Installing one KISS connection on PTY port /tmp/kisstnc" >> /var/log/packet.log
   #direwolf prefers the use of MKISS for some reason
   $MKISS -s 19200 -x 1 /tmp/kisstnc > /tmp/unix98
   CHECK-ERR hard
   #This creates a PTS interface like "/dev/pts/3"
   export PTS0=`more /tmp/unix98 | grep -w /dev | cut -b -11`
   echo "PTS0 device: $PTS0"
   echo "PTS0 device: $PTS0" >> $LOG
   if [ "$CURRAXINT" == "ft950" ]; then
      echo "Do not have FT950 in CW mode when running this script or rig will key up 100% of the time"
      echo "Do not have FT950 in CW mode when running this script or rig will key up 100% of the time" >> /var/log/packet.log
   fi
   $KISSATTACH $PTS0 $CURRAXINT >> $LOG
   CHECK-ERR hard
}

function INITD710TNC {
   #Set the AX interface name
   #  Need to switch over to this new variable name in the future but it's not
   #  needed by this function, its only really needed by KISSTUNE
   #
   if [ $DEBUG -ge 2 ]; then
      echo -e "\nDEBUG: CURRAXINT is currently: $CURRAXINT"
   fi
   export CURRAXINT="$VHFD710PORT"
   if [ $DEBUG -ge 2 ]; then
      echo -e "\nDEBUG: CURRAXINT is now: $CURRAXINT"
      echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
   fi
   if [ $DEBUG -ge 1 ]; then
      echo -e "\nDEBUG: Setting CURRAXINT to $VHFD710PORT"
      echo -e "VHFD710PORT is: $VHFD710PORT"
   fi
      if [ $DEBUG -ge 2 ]; then
         echo -e "\nDEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
      fi

   if [ $VHFD710RFBAUD -eq 9600 ]; then
      echo -e "[D710 9600] "
      echo -e "[D710 9600] " >> $LOG 2>&1
      #tmd710_tncsetup uses a fixed serialport speed of 9600
      $TNCSETUP -B 0 -S $VHFD710PORT -b 9600 -h >> $LOG 2>&1
     else
      echo -e "[D710 1200] "
      echo -e "[D710 1200] " >> $LOG 2>&1
      #tmd710_tncsetup uses a fixed serialport speed of 9600
      $TNCSETUP -B 0 -S $VHFD710PORT -b 1200 -h >> $LOG 2>&1
   fi
   sleep 1
   #The following comments reflect an older more simplified way of enabling KISS modes
   # but I found the above Linux binary mode to be both more reliable and more powerful
   #
   #(stty -F $VHFD710PORT ispeed $VHFD710BAUD ospeed $VHFD710BAUD -ignpar cs8 -cstopb -echo; echo -e "HBAUD 9600\r"; ) <$VHFD710PORT >$VHFD710PORT
   #(stty -F $VHFD710PORT ispeed $VHFD710BAUD ospeed $VHFD710BAUD -ignpar cs8 -cstopb -echo; echo -e "HBAUD 1200\r"; ) <$VHFD710PORT >$VHFD710PORT
   #(stty -F $VHFD710PORT ispeed $VHFD710BAUD ospeed $VHFD710BAUD -ignpar cs8 -cstopb -echo; echo -e "KISS ON\r"; ) <$VHFD710PORT >$VHFD710PORT
   #(stty -F $VHFD710PORT ispeed $VHFD710BAUD ospeed $VHFD710BAUD -ignpar cs8 -cstopb -echo; echo -e "RESTART\r"; ) <$VHFD710PORT >$VHFD710PORT
   #sleep 1
   echo -ne "[D710 KISS attach] "
   echo -ne "[D710 KISS attach] " >> $LOG 2>&1
   echo -e "\nkissattach-ing $VHFD710PORT : $PDEV : $AMPRIP\n" >> $LOG 2>&1 
   $KISSATTACH -l $VHFD710PORT $PDEV $AMPRIP >> $LOG 2>&1

   #Kissattach doesn't set the subnet mask so lets do that here
   echo -ne "[AMPR IP] "
   echo -ne "[AMPR IP] " >> $LOG 2>&1
   echo -e "$IFCONFIG $VHFD710AXINT $AMPRIP netmask $AMPRNM" >> $LOG 2>&1
   $IFCONFIG $VHFD710AXINT $AMPRIP netmask $AMPRNM
   CHECK-ERR hard

   # Used to reach local subnets via RF only.  If other local stations only offer
   # AMPR access via the Itnernet, you should configure specific HOST (/32) routes
   # in addition to this subnet specific route
   echo -ne "[AMPR GW] "
   echo -ne "[AMPR GW] " >> $LOG 2>&1
   echo -e "$ROUTE add -net 44.0.0.0 netmask 255.0.0.0 $VHFD710AXINT" >> $LOG 2>&1
   $ROUTE add -net $AMPRNW netmask $AMPRNM $VHFD710AXINT
   CHECK-ERR hard
}

function KISSTUNE {

   if [ $DEBUG -ge 2 ]; then
      echo -e "\nDEBUG: in KISSTUNE"
      echo -e "DEBUG: CURRAXINT is $CURRAXINT"
      if [ $DEBUG -ge 2 ]; then
         echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
      fi
      echo -e "\nDEBUG: KISS parameter tuning $CURRAXINT for profile $KISSTUNEPROFILE"
      echo -e "\nDEBUG: KISS parameter tuning $CURRAXINT for profile $KISSTUNEPROFILE" >> $LOG 2>&1 
   fi

   case $KISSTUNEPROFILE in
      #Profile #1
      1) if [ $DEBUG -eq 2 ]; then
            echo -e "\nDEBUG: KISSTUNE CASE1"
            echo -e "\nDEBUG: KISS advanced parameter tuning $CURRAXINT\n" >> $LOG 2>&1 
            echo -ne "DEBUG: [KISSTUNEa] "
            echo -ne "DEBUG: [KISSTUNEa] " >> $LOG 2>&1 
         fi

         $KISSPARMS -p $VHFD710AXPORT $AX25VHF1KISSPARMS >> $LOG 2>&1

         if [ ${#AX25VHFADV1KISSPARMS[@]} -eq 6 ]; then
            echo ${AX25VHFADV1KISSPARMS[0]} > /proc/sys/net/ax25/$VHFD710AXINT/maximum_packet_length
            echo ${AX25VHFADV1KISSPARMS[1]} > /proc/sys/net/ax25/$VHFD710AXINT/t1_timeout
            echo ${AX25VHFADV1KISSPARMS[2]} > /proc/sys/net/ax25/$VHFD710AXINT/t2_timeout
            echo ${AX25VHFADV1KISSPARMS[3]} > /proc/sys/net/ax25/$VHFD710AXINT/t3_timeout
            echo ${AX25VHFADV1KISSPARMS[4]} > /proc/sys/net/ax25/$VHFD710AXINT/maximum_retry_count
            echo ${AX25VHFADV1KISSPARMS[5]} > /proc/sys/net/ax25/$VHFD710AXINT/standard_window_size
            if [ $DEBUG -eq 2 ]; then
               echo -ne "DEBUG: [KISSTUNEb] "
               echo -ne "DEBUG: [KISSTUNEb] " >> $LOG 2>&1 
            fi
           else
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV1KISSPARMS array \n"
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV1KISSPARMS array \n" >> $LOG 2>&1
            echo -e "ARRAY: ${AX25VHFADV1KISSPARMS[@]}"
            echo -e "`set 2>&1 | grep KISS`"
            echo -e "ARRAY: ${AX25VHFADV1KISSPARMS[@]}" >> $LOG 2>&1
         fi
         ;;

      #Profile #2
      2) if [ $DEBUG -eq 2 ]; then
            echo -e "\nDEBUG: KISSTUNE CASE2"
            echo -e "\nDEBUG: KISS advanced parameter tuning $CURRAXINT\n" >> $LOG 2>&1 
            echo -ne "DEBUG: [KISSTUNEa] "
            echo -ne "DEBUG: [KISSTUNEa] " >> $LOG 2>&1 
            #echo -e "`set 2>&1 | grep KISS`"
         fi

         $KISSPARMS -p $VHFD710AXPORT $AX25VHF2KISSPARMS >> $LOG 2>&1

         if [ ${#AX25VHFADV2KISSPARMS[@]} -eq 6 ]; then
            echo "CONFIRM and CLEAN ME UP"
            echo ${AX25VHFADV2KISSPARMS[0]} > /proc/sys/net/ax25/$VHFD710AXINT/maximum_packet_length
            echo ${AX25VHFADV2KISSPARMS[1]} > /proc/sys/net/ax25/$VHFD710AXINT/t1_timeout
            echo ${AX25VHFADV2KISSPARMS[2]} > /proc/sys/net/ax25/$VHFD710AXINT/t2_timeout
            echo ${AX25VHFADV2KISSPARMS[3]} > /proc/sys/net/ax25/$VHFD710AXINT/t3_timeout
            echo ${AX25VHFADV2KISSPARMS[4]} > /proc/sys/net/ax25/$VHFD710AXINT/maximum_retry_count
            echo ${AX25VHFADV2KISSPARMS[5]} > /proc/sys/net/ax25/$VHFD710AXINT/standard_window_size
            #
            # Need to switch to the proper AGNOSTIC variable
            #echo ${AX25VHFADV2KISSPARMS[0]} > /proc/sys/net/ax25/$CURRAXINT/maximum_packet_length
            #echo ${AX25VHFADV2KISSPARMS[1]} > /proc/sys/net/ax25/$CURRAXINT/t1_timeout
            #echo ${AX25VHFADV2KISSPARMS[2]} > /proc/sys/net/ax25/$CURRAXINT/t2_timeout
            #echo ${AX25VHFADV2KISSPARMS[3]} > /proc/sys/net/ax25/$CURRAXINT/t3_timeout
            #echo ${AX25VHFADV2KISSPARMS[4]} > /proc/sys/net/ax25/$CURRAXINT/maximum_retry_count
            #echo ${AX25VHFADV2KISSPARMS[5]} > /proc/sys/net/ax25/$CURRAXINT/standard_window_size
            if [ $DEBUG -eq 2 ]; then
               echo -ne "[KISSTUNEb] "
               echo -ne "[KISSTUNEb] " >> $LOG 2>&1 
            fi
           else
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV2KISSPARMS array"
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV2KISSPARMS array" >> $LOG 2>&1
            echo -e "ARRAY: ${AX25VHFADV2KISSPARMS[@]}"
            echo -e "ARRAY: ${AX25VHFADV2KISSPARMS[@]}" >> $LOG 2>&1
            #echo -e "`set 2>&1 | grep KISS`"
         fi
         ;;

      #Profile #3 - used by Direwolf HF 
      3) if [ $DEBUG -eq 2 ]; then
            echo -e "\nDEBUG: KISSTUNE CASE3"
            echo -e "\nDEBUG: KISS advanced parameter tuning $CURRAXINT\n" >> $LOG 2>&1 
            echo -ne "DEBUG: [KISSTUNEa] "
            echo -ne "DEBUG: [KISSTUNEa] " >> $LOG 2>&1 
            #echo -e "`set 2>&1 | grep KISS`"
         fi

         $KISSPARMS -p $CURRAXINT $AX25HF1KISSPARMS >> $LOG 2>&1

         if [ ${#AX25VHFADV1KISSPARMS[@]} -eq 6 ]; then
            echo ${AX25HFADV1KISSPARMS[0]} > /proc/sys/net/ax25/$CURRAXINT/maximum_packet_length
            echo ${AX25HFADV1KISSPARMS[1]} > /proc/sys/net/ax25/$CURRAXINT/t1_timeout
            echo ${AX25HFADV1KISSPARMS[2]} > /proc/sys/net/ax25/$CURRAXINT/t2_timeout
            echo ${AX25HFADV1KISSPARMS[3]} > /proc/sys/net/ax25/$CURRAXINT/t3_timeout
            echo ${AX25HFADV1KISSPARMS[4]} > /proc/sys/net/ax25/$CURRAXINT/maximum_retry_count
            echo ${AX25HFADV1KISSPARMS[5]} > /proc/sys/net/ax25/$CURRAXINT/standard_window_size
            if [ $DEBUG -eq 2 ]; then
               echo -ne "[KISSTUNEb] "
               echo -ne "[KISSTUNEb] " >> $LOG 2>&1 
            fi
           else
            echo -e "\nWARNING: More or less than four varaibles in the AX25HFADV1KISSPARMS array"
            echo -e "\nWARNING: More or less than four varaibles in the AX25HFADV1KISSPARMS array" >> $LOG 2>&1
            echo -e "ARRAY: ${AX25HFADV1KISSPARMS[@]}"
            echo -e "ARRAY: ${AX25HFADV1KISSPARMS[@]}" >> $LOG 2>&1
            #echo -e "`set 2>&1 | grep KISS`"
         fi
         ;;

      #Profile #4 - use by Direwolf HF 
      4) if [ $DEBUG -eq 2 ]; then
            echo -e "\nDEBUG: KISSTUNE CASE4"
            echo -e "\nDEBUG: KISS advanced parameter tuning CURRAXINT: $CURRAXINT\n" >> $LOG 2>&1 
            echo -ne "DEBUG: [KISSTUNEa] "
            echo -ne "DEBUG: [KISSTUNEa] " >> $LOG 2>&1 
            #echo -e "`set 2>&1 | grep KISS`"
         fi

         echo -e "\nDEBUG: CURRAXINT is $CURRAXINT"
         echo -e "\nDEBUG: Setting KISSPARMS: $AX25VHF3KISSPARMS"
         echo -e "\nDEBUG: overriding CURRANINT of $CURRAXINT with $HFDWMAXPORT"
         #I don't think this should be here
         #CURRAXINT=$HFDWMAXPORT
         echo -e "DEBUG:  Running: $KISSPARMS -p $CURRAXINT $AX25VHF3KISSPARMS"
         $KISSPARMS -p $CURRAXINT $AX25VHF3KISSPARMS >> $LOG 2>&1
         CHECK-ERR hard

         echo "DEBUG: Array AX25VHFADV3KISSPARMS contains: ${AX25VHFADV3KISSPARMS[@]}"

         if [ ${#AX25VHFADV3KISSPARMS[@]} -eq 6 ]; then
            echo "DEBUG: setting /proc/sys/net/ax25/$CURRAXINT variables"
            echo ${AX25VHFADV3KISSPARMS[0]} > /proc/sys/net/ax25/$CURRAXINT/maximum_packet_length
            echo ${AX25VHFADV3KISSPARMS[1]} > /proc/sys/net/ax25/$CURRAXINT/t1_timeout
            echo ${AX25VHFADV3KISSPARMS[2]} > /proc/sys/net/ax25/$CURRAXINT/t2_timeout
            echo ${AX25VHFADV3KISSPARMS[3]} > /proc/sys/net/ax25/$CURRAXINT/t3_timeout
            echo ${AX25VHFADV3KISSPARMS[4]} > /proc/sys/net/ax25/$CURRAXINT/maximum_retry_count
            echo ${AX25VHFADV3KISSPARMS[5]} > /proc/sys/net/ax25/$CURRAXINT/standard_window_size
            if [ $DEBUG -eq 2 ]; then
               echo -ne "[KISSTUNEb] "
               echo -ne "[KISSTUNEb] " >> $LOG 2>&1 
            fi
           else
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV3KISSPARMS array"
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV3KISSPARMS array" >> $LOG 2>&1
            echo -e "ARRAY: ${AX25VHFADV3KISSPARMS[@]}"
            echo -e "ARRAY: ${AX25VHFADV3KISSPARMS[@]}" >> $LOG 2>&1
            #echo -e "`set 2>&1 | grep KISS`"
         fi
         ;;

      #Profile #5 - use by Direwolf VHF PSK 
      5) if [ $DEBUG -eq 2 ]; then
            echo -e "\nDEBUG: KISSTUNE CASE5"
            echo -e "\nDEBUG: KISS advanced parameter tuning CURRAXINT: $CURRAXINT\n" >> $LOG 2>&1 
            echo -ne "DEBUG: [KISSTUNEa] "
            echo -ne "DEBUG: [KISSTUNEa] " >> $LOG 2>&1 
            #echo -e "`set 2>&1 | grep KISS`"
         fi

         echo -e "\nDEBUG: CURRAXINT is $CURRAXINT"
         echo -e "\nDEBUG: Setting KISSPARMS: $AX25VHF3KISSPARMS"
         echo -e "\nDEBUG: overriding CURRANINT of $CURRAXINT with $VHF857AXINT"
         #I don't think this should be here
         #CURRAXINT=$VHF857AXINT
         echo -e "DEBUG:  Running: $KISSPARMS -p $CURRAXINT $AX25VHF3KISSPARMS"
         $KISSPARMS -p $CURRAXINT $AX25VHF3KISSPARMS >> $LOG 2>&1
         CHECK-ERR hard

         echo "DEBUG: Array AX25VHFADV3KISSPARMS contains: ${AX25VHFADV3KISSPARMS[@]}"

         if [ ${#AX25VHFADV3KISSPARMS[@]} -eq 6 ]; then
            echo "DEBUG: setting /proc/sys/net/ax25/$CURRAXINT variables"
            echo ${AX25VHFADV3KISSPARMS[0]} > /proc/sys/net/ax25/$CURRAXINT/maximum_packet_length
            echo ${AX25VHFADV3KISSPARMS[1]} > /proc/sys/net/ax25/$CURRAXINT/t1_timeout
            echo ${AX25VHFADV3KISSPARMS[2]} > /proc/sys/net/ax25/$CURRAXINT/t2_timeout
            echo ${AX25VHFADV3KISSPARMS[3]} > /proc/sys/net/ax25/$CURRAXINT/t3_timeout
            echo ${AX25VHFADV3KISSPARMS[4]} > /proc/sys/net/ax25/$CURRAXINT/maximum_retry_count
            echo ${AX25VHFADV3KISSPARMS[5]} > /proc/sys/net/ax25/$CURRAXINT/standard_window_size
            if [ $DEBUG -eq 2 ]; then
               echo -ne "[KISSTUNEb] "
               echo -ne "[KISSTUNEb] " >> $LOG 2>&1 
            fi
           else
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV3KISSPARMS array"
            echo -e "\nWARNING: More or less than four varaibles in the AX25VHFADV3KISSPARMS array" >> $LOG 2>&1
            echo -e "ARRAY: ${AX25VHFADV3KISSPARMS[@]}"
            echo -e "ARRAY: ${AX25VHFADV3KISSPARMS[@]}" >> $LOG 2>&1
            #echo -e "`set 2>&1 | grep KISS`"
         fi
         ;;

      #Fallthrough profile
      *) echo -e "\nERROR: unknown KISSTUNEPROFILE: $KISSTUNEPROFILE" >> $LOG 2>&1
         echo -e "\nERROR: unknown KISSTUNEPROFILE: $KISSTUNEPROFILE"
         echo -e "\nERROR: KISSTUNE FALLTHRU DEFAULT" >> $LOG 2>&1
         echo -e "\nERROR: KISSTUNE FALLTHRU DEFAULT"
         ;;
   esac
}

function ADVAX25 {
   echo -e "[NetROM module] "
   echo -e "[NetROM module] " >> $LOG 2>&1
   $MODPROBE -q netrom >> $LOG 2>&1

   # Not sure if we need this nexst one, want to get rid of the 
   # KI6ZHD-14 route
   # --
   # d710: fm KI6ZHD-14 to NODES ctl UI^ pid=CF(NET/ROM) len 28
   # NET/ROM Routing: ZHDROM
   #         KI6ZHD-5  ZHDNOD       KI6ZHD-14    255
   #--
   echo -ne "[Netrom IP] "
   echo -ne "[Netrom  IP] " >> $LOG 2>&1
   #$NRATTACH -i $AMPRIP netnod >> $LOG 2>&1
   $NRATTACH -i $AMPRIP -m 236 $VHFD710NRPORT >> $LOG 2>&1
   CHECK-ERR hard

   $IFCONFIG $VHFD710NRINT $AMPRIP netmask $AMPRNM
   CHECK-ERR hard

   #Associate a local Linux username to a callsign for programs that are
   # run locally and require a callsign identification
   #
   #  Only required by Unode so far
   echo -ne "[axparms] "
   echo -ne "[axparms] " >> $LOG 2>&1
   $AXPARMS -assoc $MYBEACONCALL $XUSERNAME >> $LOG 2>&1
   CHECK-ERR hard
   
   #Check that things are up
   if [ -z "`$IFCONFIG | grep ax0`" ] || [ -z "`$IFCONFIG | grep nr0`" ]; then 
      echo -e "\n *** ax0 or nr0 interfaces missing!***\n"
      $IFCONFIG
      echo -e "\nFatal error.  Exiting!\n"
      exit 1
   fi

   #Start up ax25d which is like inetd for packet
   #-l is for logging, -i sends a netrom update immeadiately and -t 30 sends 
   # updates every 30 min
   echo -ne "[ax25d] "
   echo -ne "[ax25d] " >> $LOG 2>&1
   $AX25D -l >> $LOG 2>&1
   CHECK-ERR hard

   echo -ne "[netromd] "
   echo -ne "[netromd] " >> $LOG 2>&1
   #Start up the netromd system as configured in /etc/ax25/nr*
   #  -i : send routes immeadiately
   #  -l : enable logging
   #  -t : send netrom updates every 60 min
   #  -d : OPTIONAL: enable debugging statements
   $NETROMD -d -i -l -t $NETROMADVTIME >> $LOG 2>&1
   CHECK-ERR hard

   # Support APRX for sending APRS-IS objects and digipeating
   if [ -f $APRX ]; then
       echo -ne "[aprx] "
       echo -ne "[aprx] " >> $LOG 2>&1
       $APRX
       CHECK-ERR hard
      else
       echo -e "\nAPRX not installed" >> $LOG 2>&1
   fi

   if [ -f $LDSPED ]; then
       echo -ne "[ldsped] "
       echo -ne "[ldsped]\n" >> $LOG 2>&1
       #Run LDSPED as a daemon
       $LDSPED >> $LOG 2>&1
       CHECK-ERR hard
      else
       echo -e "\nLDSPED not installed" >> $LOG 2>&1
   fi


   #Manual: restore previously learned routes at via the nodesave command from 
   #        ax25-tools-0.0.10-rc2
   #
   #  su
   #  /usr/sbin/nodesave > /var/ax25/nr-routes-`date +%m%d%y`.sh
   #  chmod 700 /var/ax25/nr-routes-145.050-`date +%m%d%y`.sh
   #
   #  NOTE: This remaining (previous) code is WRONG and it's due to inconsistent syntax
   #        Read the man page very VERY carefully but:
   #
   #          when configuring *nodes* "+" is to add new entries, "-" to remove node entries
   #
   #          when configuring *routes* (which are already created when nodes are added) 
   #              "+" is used to LOCK them, "-" is to unlock them
   #
   #       #rewrite the results to NOT create permanent routes - let them expire
   #       cat /var/ax25/nrroutes-raw.`date +%m%d%y`.sh | sed "s/\+/\-/" > /var/ax25/nr-routes-145.050-`date +%m%d%y`.sh
   #
   #echo -e "\nNow pre-loading previously learned 145.050 Netrom routes as found in /var/ax25/nr-routes-*"
   #`ls -lat /var/ax25/nr-routes* | head -n1 | awk '{print $9}'`
   echo -e "\n\nYou can optionally preload older learned NetROM routes by running this script as root:"
   echo -e "  `ls -t /var/ax25/nr-routes-*.sh | head -n1`"
}


function STARTMONITOR {
  if [ $DEBUG -eq 2 ]; then
     echo -e "\n\nDEBUG: STARTMONITOR: About to fetch GETBEACONPATHPREFS"
     echo -e "DEBUG: function CMD line is: $@"
     echo -e "DEBUG: var1 is $1"
     echo -e "DEBUG: *** No variables used in this function"
  fi
  if [ $DEBUG -eq 1 ]; then
     echo -e "\nSleeping for 3" >> $LOG 2>&1 
  fi

   echo -en "[mheard] "
   echo -en "[mheard] " >> $LOG 2>&1 
   /usr/sbin/mheardd >> $LOG 2>&1 
   CHECK-ERR hard

   #mheard check
   if [ ! -f /var/ax25/mheard/mheard.dat ]; then
      echo -e "\nERROR: /var/ax25/mheard/mheard.dat not found!"
      exit 1
   fi

   if [ $DEBUG -eq 1 ]; then
      echo -e "\nSleeping for 3" >> $LOG 2>&1 
   fi

   #ax-apps 0.0.6 was axlisten, but now as of 0.0.10, it's listen
   echo -en "[listen] "
   echo -en "[listen] " >> $LOG 2>&1
   echo -e "\nNow sending listen in colorized decode mode to /var/log/listen-`date +%m%d%y`\n" >> $LOG 2>&1 
   #8 is 8bit; a is bidir; c is color but doesnt allow scrollback; r is readible; p is port
   $LISTEN -8arp $PDEV >> /var/log/listen-`date +%m%d%y` 2>&1 &
   CHECK-ERR hard
}

function DISPLAYPARMS () {
   if [ $DEBUG -ge 1 ]; then
      echo -e "\nDEBUG: DISPLAYPARMS: viewing parameters for CURRAXINT: $CURRAXINT"
      echo -e "DEBUG:  Comment:  CURRAXINT should be something like ax0, not a /dev interface"
   fi
   echo -e "\nValidate Linux AX.25 kernel parameters:"
   echo -e "*** FIX ME to use abstracted interface varilable - currently HARDCODED to ax0"
   echo -en "   AX.25 packet length: "
   #cat /proc/sys/net/ax25/$CURRAXINT/maximum_packet_length
   cat /proc/sys/net/ax25/ax0/maximum_packet_length
   echo -en "   T1 timeout: "
   #cat /proc/sys/net/ax25/$CURRAXINT/t1_timeout
   cat /proc/sys/net/ax25/ax0/t1_timeout
   echo -en "   T2 timeout: "
   #cat /proc/sys/net/ax25/$CURRAXINT/t2_timeout
   cat /proc/sys/net/ax25/ax0/t2_timeout
   echo -en "   T3 timeout: "
   #cat /proc/sys/net/ax25/$CURRAXINT/t3_timeout
   cat /proc/sys/net/ax25/ax0/t3_timeout
   echo -en "   Max Retry: "
   #cat /proc/sys/net/ax25/$CURRAXINT/maximum_retry_count
   cat /proc/sys/net/ax25/ax0/maximum_retry_count
   echo -en "   Packet Window size: "
   #cat /proc/sys/net/ax25/$CURRAXINT/standard_window_size
   cat /proc/sys/net/ax25/ax0/standard_window_size
}

function GETRIGSTATE () {
  if [ -f $KISSPKTRIG ]; then
     PDEV="`cat $KISSPKTRIG`"
    else

     if [ $DEBUG -eq 1 ]; then
        echo "DEBUG: parm1: $1"
     fi

     if [ "$1" = "force-d710" ]; then
         PDEV="d710"
       else
        echo -e "\nPacketRig state not found.  Aborting.\n\n"
        echo -e "\nPacketRig state not found.  Aborting.\n\n" >> $LOG 2>&1
        exit 1
     fi
  fi
}

function GETBEACONPATHPREFS () {
  # We need specific information about how the channel needs to be setup and
  # we infer this from the BEACON path - that's another way of specifying the
  # packet frequency

  if [ $DEBUG -eq 2 ]; then
     echo -e "\nDEBUG: GETBEACONPATHPREFS: About to fetch GETBEACONPATHPREFS"
     echo "DEBUG: CMD line is: $@"
     echo "DEBUG: var1 is $1"
  fi

  if [ -n "`echo $@ | grep alt1beacon`" ]; then
     #Set KISS parameter options
     export KISSTUNEPROFILE=1
     #Enable Advanced AX25 services for this beacon - assumes 145.050
     export ENABLEADVAX25=1

   elif [ -n "`echo $@ | grep alt2beacon`" ]; then
     #Set KISS parameter options
     export KISSTUNEPROFILE=1
     #Disable Advanced AX25 services for this beacon 
     export ENABLEADVAX25=1

   elif [ -n "`echo $@ | grep alt3beacon`" ]; then
     if [ $DEBUG -eq 2 ]; then
        echo "DEBUG: alt3beacon - setting KISSTUNEPROFILE=2 and ENABLEADVAX25=0"
     fi
     #Set KISS parameter options
     export KISSTUNEPROFILE=2
     #Disable Advanced AX25 services for this beacon 
     export ENABLEADVAX25=1

   # Direwolf HF 
   elif [ -n "`echo $@ | grep alt4beacon`" ]; then
     if [ $DEBUG -eq 2 ]; then
        #9/8/2015 - was profile 3
        echo "DEBUG: alt4beacon - setting KISSTUNEPROFILE=1 and ENABLEADVAX25=0"
     fi
     #Set KISS parameter options
     export KISSTUNEPROFILE=1
     #Disable Advanced AX25 services for this beacon 
     export ENABLEADVAX25=0

   # 857 PSK Direwolf
   elif [ -n "`echo $@ | grep alt5beacon`" ]; then
     if [ $DEBUG -eq 2 ]; then
        echo "DEBUG: alt5beacon - setting KISSTUNEPROFILE=1 and ENABLEADVAX25=0"
     fi
     #Set KISS parameter options
     export KISSTUNEPROFILE=4
     #Disable Advanced AX25 services for this beacon 
     export ENABLEADVAX25=0

   elif [ -n "`echo $@ | grep no-beacon-digi`" ]; then
     #Set KISS parameter options
     export KISSTUNEPROFILE=4
     #Disable Advanced AX25 services for this beacon 
     export ENABLEADVAX25=0

   else
     echo -e "\n\nERROR: No beacon path specified\n"
     USAGE
     exit 1
     #if [ $DEBUG -eq 2 ]; then
     #   echo "DEBUG: fallthru ELSE - setting KISSTUNEPROFILE=1 and ENABLEADVAX25=0"
     #fi
     ##Defaults for an unspecified digi
     ##Set KISS parameter options
     #export KISSTUNEPROFILE=1
     ##Disable Advanced AX25 services for this beacon 
     #export ENABLEADVAX25=0
  fi
}

function ALT1BEACON (){
   #When on the 145.050 frequency, lets use the local NODE system
   GETRIGSTATE

   echo -ne "[digipeated beacon #1] "
   echo -ne "[digipeated beacon #1] " >> $LOG 2>&1 
   /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON $ALT1BEACONPATH" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   CHECK-ERR hard
}

function ALT2BEACON (){
   #This is used to broadcast across the 144.910 band
   GETRIGSTATE
   echo -ne "[digipeated beacon #2] "
   echo -ne "[digipeated beacon #2] " >> $LOG 2>&1 
   /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON $ALT2BEACONPATH" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   CHECK-ERR hard
}

function ALT3BEACON (){
   #This is used to broadcast across the 144.990 band
   GETRIGSTATE
   echo -ne "[digipeated beacon #3] "
   echo -ne "[digipeated beacon #3] " >> $LOG 2>&1 
   /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   CHECK-ERR hard
}

function ALT4BEACON (){
   #This is used to broadcast across the 144.990 band
   GETRIGSTATE
   echo -ne "[beacon #4 disabled] "
   echo -ne "[beacon #4 disabled] " >> $LOG 2>&1 
}

function ALT5BEACON (){
   #This is used to broadcast across the 144.990 band
   GETRIGSTATE
   echo -ne "[non-digipeated beacon #5] "
   echo -ne "[non-digipeated beacon #5] " >> $LOG 2>&1 
   #Overriging beacon time to 15min
   #/usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON" -t 15 $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   CHECK-ERR hard
}

function NODIGIEDBEACON (){
   GETRIGSTATE
   echo -ne "[no digipeated beacons] "
   echo -ne "[no digipeated beacons] " >> $LOG 2>&1 
   /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
   CHECK-ERR hard
}

function STARTBEACON () {
  #Thoug Odd, it's the selection of the beacons that sets most of the various TNC
  # and KISS parameters in this packetrig script.  If you want unique TNC/KISS
  # parameters, you need to create a NEW BEACON profile

  if [ $DEBUG -eq 1 ]; then
     echo -e "DEBUG: STARTBEACON options: $@"
     echo -e "DEBUG: CMDLINE: $CMDLINE"
  fi
  if [ -n "`echo $@ | grep alt1beacon`" ]; then
     ALT1BEACON

     #Configure any KISS optimizations
     KISSTUNE

    elif [ -n "`echo $@ | grep alt2beacon`" ]; then
     ALT2BEACON
     #Configure any KISS optimizations
     KISSTUNE

    elif [ -n "`echo $@ | grep alt3beacon`" ]; then
     ALT3BEACON
     #Configure any KISS optimizations
     KISSTUNE

    #HF Soundmodem
    elif [ -n "`echo $@ | grep alt4beacon`" ]; then
     ALT4BEACON
     #Configure any KISS optimizations
     KISSTUNE

    #VHF Direwolf PSK
    elif [ -n "`echo $@ | grep alt5beacon`" ]; then
     ALT5BEACON
     #Configure any KISS optimizations
     KISSTUNE

    elif [ -n "`echo $@ | grep no-beacon-digi`" ]; then
     NODIGIEDBEACON
     #Configure any KISS optimizations
     KISSTUNE

    else
     echo -ne "[beacon] "
     echo -ne "[beacon] " >> $LOG 2>&1 
     #/usr/sbin/beacon -d BEACON -t 20 $PDEV "HD"
     #/usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON" -t 20 $PDEV "KI6ZHD/b KI6ZHD-1/pbbs - Santa Clara, CA" >> $LOG 2>&1 
     #
     # Note:  Soundmodem artificially holds down any transmits for a period 
     #        time to learn the system.  The delay is about 20 seconds or so
     /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON $BASEBEACONPATH" -t $BEACONTIME $PDEV "$BEACONSTRING" >> $LOG 2>&1 
     CHECK-ERR hard

     #Configure any KISS optimizations
     KISSTUNE

  fi
}


function 1BEACON (){
   GETRIGSTATE
   #Note:  When soundmodem initially starts, it will NOT transmit for a few 
   #       as it needs to learn the noise floor.  You MUST wait before beacons
   #       will start working
   if [ $DEBUG -eq 1 ]; then
      echo -e "DEBUG: CMDLINE        : 1BEACON and $1"
      echo -e "DEBUG: MYBEACONCALL   : $MYBEACONCALL"
      echo -e "DEBUG: PDEV           : $PDEV"
      echo -e "DEBUG: BASEBEACONPATH : $BASEBEACONPATH"
      echo -e "DEBUG: BEACONSTRING   : $BEACONSTRING"
   fi
   if [ -n "$1" ]; then
      echo -e "[1beacon sent via $1] "
      echo -e "[1beacon sent via $1] " >> $LOG 2>&1
      /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON $1" $PDEV "$BEACONSTRING" >> $LOG 2>&1 
      CHECK-ERR hard
     else
      #send beacon via a specific digi PATH
      echo -e "[1beacon sent] "
      echo -e "[1beacon sent] " >> $LOG 2>&1
      /usr/sbin/beacon -c "$MYBEACONCALL" -d "BEACON $BASEBEACONPATH" $PDEV "$BEACONSTRING" >> $LOG 2>&1 
      CHECK-ERR hard
   fi
}




#------------------------------------------------------------------------------
# Main program
#------------------------------------------------------------------------------

if [ -z "$1" ]; then
  USAGE
  exit 1
fi

if [ $UID -ne 0 ]; then
  echo -e "\nERROR: you must be root\n"
  exit 1
fi


#Get the command line options given
CMDLINE="$@"
echo -e "\nWelcome to packetrig.sh - $VERSION\n"

if [ $DEBUG -ge 1 ]; then
   echo -e "\nDEBUG:  Debugging level is set to: $DEBUG"
   echo -e "\nDEBUG: Setting CMDLINE to: $@"
   echo -e "CMDLINE: $CMDLINE"
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-smdm-packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Setting Kenwood D710 for VHF/UHF packet
   PDEV=$VHFSMDMAXPORT
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[smdm vhf conf] "
   echo -en "[smdm vhf conf] " >> $LOG 2>&1
   /bin/cp -f /etc/ax25/soundmodem-vhf.conf /etc/ax25/soundmodem.conf
   CHECK-ERR hard
   /bin/cp -f /etc/ax25/axports-smdm /etc/ax25/axports
   CHECK-ERR hard
   /bin/cp -f /root/LinPac/macro/init.mac-smdm /root/LinPac/macro/init.mac
   CHECK-ERR hard

   #initialize sound card
   INITSNDCARD vhf

   #reset sound card audio levels and start soundmodem
   INITSOUNDMODEM

   #start soundmodem
   STARTSOUNDMODEM

   #start mheard and listen
   STARTMONITOR

   #start beacon
   STARTBEACON

   #Tune the KISS parameters
   KISSTUNE

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   #Closing notes
   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710aa6wk"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-d710-packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Using the Kenwood D710 for VHF/UHF packet
   PDEV=$VHFD710AXPORT

   #Set the TNC RF speed
   if [ -n "`echo $1 | grep 96packet`" ]; then
      VHFD710RFBAUD="9600"
     else
      VHFD710RFBAUD="1200"
   fi

   #Setting STATE for beacon
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   if [ $DEBUG -ge 2 ]; then
      echo -e "\nDEBUG: Executing function: GETBEACONPATHPREFS $CMDLINE"
   fi
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct axports, and other configs
   echo -en "[d710 vhf conf] "
   echo -en "[d710 vhf conf] " >> $LOG 2>&1

   if [ $DEBUG -eq 2 ]; then
      echo -e "\nDEBUG: KISSTUNEPROFILE is $KISSTUNEPROFILE"
   fi

   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 4
      1) /bin/cp -f /etc/ax25/axports-d710-255 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      #2 - Paclength of 128 and a window size of 2
      2) /bin/cp -f /etc/ax25/axports-d710-128 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
         if [ $DEBUG -eq 2 ]; then
            echo -e "DEBUG: copied over axports-d710-128"
         fi
      ;;
      *) echo -e "\nERROR: in loading vhf-d710-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize TNC
   INITD710TNC 

   #start mheard and listen
   STARTMONITOR

   #start beacon
   STARTBEACON $CMDLINE

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-d710-96packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Using the Kenwood D710 for VHF/UHF packet
   PDEV=$VHFD710AXPORT

   #Set the TNC RF speed
   if [ -n "`echo $1 | grep 96packet`" ]; then
      VHFD710RFBAUD="9600"
     else
      VHFD710RFBAUD="1200"
   fi

   #Setting STATE for beacon
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct axports, and other configs
   echo -en "[d710 vhf conf] "
   echo -en "[d710 vhf conf] " >> $LOG 2>&1

   if [ $DEBUG -eq 1 ]; then
      echo -e "DEBUG: Copying over proper axports config"
   fi

   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 4
      #AX25VHFADV1KISSPARMS
      1) /bin/cp -f /etc/ax25/axports-d710-255 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      #2 - Paclength of 128 and a window size of 2
      #AX25VHFADV2KISSPARMS
      2) /bin/cp -f /etc/ax25/axports-d710-128 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading vhf-d710-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize TNC
   INITD710TNC 

   #start mheard and listen
   STARTMONITOR

   #start beacon
   STARTBEACON

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi

#------------------------------------------------------------------------------
#Have to add the dash to make grep not double match
if [ `echo $1 | grep dw-857-psk-packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Setting VHF for packet
   PDEV=$VHFDWMAXPORT
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[dw vhf conf] "
   echo -en "[dw vhf conf] " >> $LOG 2>&1

   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 2
      1)
         echo "KISSTUNEPROFILE $KISSTUNEPROFILE for dw-857-psk-packet not implimented"
      ;;
      2)
         echo "KISSTUNEPROFILE $KISSTUNEPROFILE for dw-857-psk-packet not implimented"
      ;;
      3)
         echo "KISSTUNEPROFILE $KISSTUNEPROFILE for dw-857-psk-packet not implimented"
      ;;
      4)
         echo -e "\nDEBUG: In dw-857-psk-packet section 4\n"
         /bin/cp -f /etc/ax25/axports-dw-vhf /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /etc/ax25/direwolf-857-psk.conf /etc/ax25/direwolf.conf
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-dw /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading dw-hf-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize sound card
   echo -e "\n VHF radio should be set to FM mode on desired frequency"

   #08/27/16 - This actually only looks for the available soundcard (looks for old Dell one.. needs to be updated)
   #  Leave it to be HF and not VHF for now
   INITSNDCARD hf

   #start soundmodem
   STARTDIREWOLF

   #start mheard and listen
   STARTMONITOR

   #start beacon
   STARTBEACON $CMDLINE

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi

#------------------------------------------------------------------------------
#Have to add the dash to make grep not double match
if [ `echo $1 | grep dw-hf-packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Setting HF for packet
   PDEV=$HFDWMAXPORT
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[dw hf conf] "
   echo -en "[dw hf conf] " >> $LOG 2>&1

   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 2
      1)
         /bin/cp -f /etc/ax25/axports-dw-hf /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /etc/ax25/direwolf-hf.conf /etc/ax25/direwolf.conf
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-dw-hf /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading dw-hf-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize sound card
   echo -e "\n HF radio should be set to either 14.1016 USB or 14.105 Mhz LSB Mhz"
   INITSNDCARD hf

   #start direwolf
   STARTDIREWOLF hf

   #start mheard and listen
   STARTMONITOR

   #start beacon
   #STARTBEACON

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
#Have to add the dash to make grep not double match
if [ `echo $1 | grep smdm-hf-packet` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Setting HF for packet
   PDEV=$HFSMDMAXPORT
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[smdm hf conf] "
   echo -en "[smdm hf conf] " >> $LOG 2>&1

   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 2
      3) /bin/cp -f /etc/ax25/soundmodem-hf.conf /etc/ax25/soundmodem.conf
         CHECK-ERR hard
         /bin/cp -f /etc/ax25/axports-smdm /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-smdm /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading smdm-hf-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize sound card
   echo -e "\n HF radio should be set to 14,102.28Mhz USB"
   INITSNDCARD hf

   #reset sound card audio levels and start soundmodem
   INITSOUNDMODEM

   #start soundmodem
   STARTSOUNDMODEM

   #start mheard and listen
   STARTMONITOR

   #start beacon
   STARTBEACON

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-d710-xastir` ]; then

   #Stop any old processes
   STOPPROCESSES

   #Using the Kenwood D710 for VHF/UHF packet
   PDEV=$VHFD710AXPORT

   #Setting STATE for beacon
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[d710 vhf conf] "
   echo -en "[d710 vhf conf] " >> $LOG 2>&1
   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 4
      1) /bin/cp -f /etc/ax25/axports-d710-255 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      #2 - Paclength of 128 and a window size of 2
      2) /bin/cp -f /etc/ax25/axports-d710-128 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading vhf-d710-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize TNC
   INITD710TNC 

   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-sndmdm-aprs` ]; then
   #Stop any old processes
   STOPPROCESSES

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[smdm vhf conf] "
   echo -en "[smdm vhf conf] " >> $LOG 2>&1
   /bin/cp -f /etc/ax25/soundmodem-vhf.conf /etc/ax25/soundmodem.conf
   CHECK-ERR hard
   /bin/cp -f /etc/ax25/axports-smdm /etc/ax25/axports
   CHECK-ERR hard
   /bin/cp -f /root/LinPac/macro/init.mac-smdm /root/LinPac/macro/init.mac
   CHECK-ERR hard

   #initialize sound card
   INITSNDCARD vhf

   #reset sound card audio levels and start soundmodem
   INITSOUNDMODEM

   #start soundmodem
   STARTSOUNDMODEM

   #Enable this if you really want to log APRS traffic
   #start mheard and listen
   #STARTMONITOR

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS


   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"
   echo -e "n.. manually start xastir now"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep jnos-smdm` ]; then
   #Stop any old processes
   STOPPROCESSES

   #Using the Kenwood D710 for VHF/UHF packet
   PDEV=$VHFSMDMAXPORT

   #Setting STATE for beacon
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[vhf smdm conf] "
   echo -en "[vhf smdm conf] " >> $LOG 2>&1
   /bin/cp -f /etc/ax25/soundmodem-vhf.conf /etc/ax25/soundmodem.conf
   CHECK-ERR hard
   /bin/cp -f /etc/ax25/axports-smdm /etc/ax25/axports
   CHECK-ERR hard
   /bin/cp -f /root/LinPac/macro/init.mac-smdm /root/LinPac/macro/init.mac
   CHECK-ERR hard

   #initialize sound card
   INITSNDCARD vhf

   #reset sound card audio levels and start soundmodem
   INITSOUNDMODEM

   #reset sound card audio levels and start soundmodem
   STARTSOUNDMODEM

   #start mheard and listen
   STARTMONITOR

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   #start JNOS
   PTSRPORT=`/usr/sbin/net2kiss -i sm1 /dev/ptmx &`
   CHECK-ERR hard
   PTSPORT=`echo $PTSRPORT | awk -F / '{print $4}'`
   echo -en "[net2kiss on /dev/pts/$PTSPORT ] "
   echo -en "[net2kiss on /dev/pts/$PTSPORT ] " >> $LOG 2>&1
   
   cd $JNOSPATH
   cat $JNOSPATH/autoexec.nos.base | sed s/XXyX/$PTSPORT/g > $JNOSPATH/autoexec.nos
   #echo -en "[jnos] "
   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"
   echo -e "    manually start JNOS now:\n"
   echo -e "          cd /usr/local/jnos; ./jnos"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   #./jnos
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep jnos-d710` ]; then
   #Stop any old processes
   STOPPROCESSES

   #Using the Kenwood D710 for VHF/UHF packet
   PDEV=$VHFD710AXPORT

   #Setting STATE for beacon
   echo "$PDEV" > $KISSPKTRIG

   #Calibrate date/time
   INITIALIZEDATE

   #load any kernel modules
   LOADKISSMODULES

   #Get proper advanced settings based upon the commandline BEACON path
   GETBEACONPATHPREFS $CMDLINE

   #copy over the correct soundmodem config
   echo -en "[d710 vhf conf] "
   echo -en "[d710 vhf conf] " >> $LOG 2>&1
   case $KISSTUNEPROFILE in
      #1 - Paclength of 128 and a window size of 4
      1) /bin/cp -f /etc/ax25/axports-d710-255 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      #2 - Paclength of 128 and a window size of 2
      2) /bin/cp -f /etc/ax25/axports-d710-128 /etc/ax25/axports
         CHECK-ERR hard
         /bin/cp -f /root/LinPac/macro/init.mac-d710 /root/LinPac/macro/init.mac
         CHECK-ERR hard
      ;;
      *) echo -e "\nERROR: in loading vhf-d710-packet, no valid KISSTUNEPROFILE was found"
         echo -e "KISSTUNEPROFILE: $KISSTUNEPROFILE"
      ;;
   esac

   #initialize TNC
   INITD710TNC 

   #start mheard and listen
   STARTMONITOR

   #If the configured beacon enables netrom, etc
   if [ "$ENABLEADVAX25" -eq 1 ]; then
       ADVAX25
     else
      echo "ADVAX25 disabled"
   fi

   #Show the set AX25 paramters
   DISPLAYPARMS

   #start JNOS
   PTSRPORT=`/usr/sbin/net2kiss -i $VHFD710AXINT /dev/ptmx &`
   CHECK-ERR hard
   PTSPORT=`echo $PTSRPORT | awk -F / '{print $4}'`
   echo -en "[net2kiss on /dev/pts/$PTSPORT ] "
   echo -en "[net2kiss on /dev/pts/$PTSPORT ] " >> $LOG 2>&1
   
   cd $JNOSPATH
   cat $JNOSPATH/autoexec.nos.base | sed s/XXyX/$PTSPORT/g > $JNOSPATH/autoexec.nos
   #echo -en "[jnos] "
   echo -e "\nConsider running other programs: "
   echo -e "    as root: /usr/local/sbin/start-linpac.sh d710"
   echo -e "       manually start JNOS now:\n"
   echo -e "          cd /usr/local/jnos; ./jnos"

   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >> $LOG 2>&1
   #./jnos
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $@ | grep alt1beacon` ]; then
   #Stop any old processes
   echo -ne "[stopping orig. beacon] "
   echo -ne "[stopping orig. beacon] " >> $LOG 2>&1 
   $KILLALL beacon
   ALT1BEACON
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $@ | grep alt2beacon` ]; then
   #Stop any old processes
   echo -ne "[stopping orig. beacon] "
   echo -ne "[stopping orig. beacon] " >> $LOG 2>&1 
   $KILLALL beacon
   ALT2BEACON
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $@ | grep alt3beacon` ]; then
   #Stop any old processes
   echo -ne "[stopping orig. beacon] "
   echo -ne "[stopping orig. beacon] " >> $LOG 2>&1 
   $KILLALL beacon
   ALT3BEACON
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $@ | grep no-beacon-digi` ]; then
   #Stop any old processes
   echo -ne "[stopping orig. beacon] "
   echo -ne "[stopping orig. beacon] " >> $LOG 2>&1 
   $KILLALL beacon
   NODIGIEDBEACON
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $1 | grep 1beacon` ]; then
   #Stop any old processes
   1BEACON $2 $3
   exit 0
fi


#------------------------------------------------------------------------------
if [ `echo $1 | grep vhf-d710-rekiss` ]; then
   INITD710TNC
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $1 | grep show-kiss-parms` ]; then
   #ftp://ftp.febo.com/pub/linux_ham/axgetparms
   /usr/local/sbin/axdispparms.sh
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $1 | grep stop` ]; then
   STOPPROCESSES
   echo -e "\n\ndone\n"
   echo -e "\n\ndone\n" >>$LOG 2>&1 
   exit 0
fi

#------------------------------------------------------------------------------
if [ `echo $1 | grep d710-tnc-off` ]; then
   STOPPROCESSES
   TNC-OFF force-d710
   echo -e "\ndone\n"
   echo -e "\ndone\n" >>$LOG 2>&1 
   exit 0
fi

#------------------------------------------------------------------------------
echo -e "\nError: unknown option: $1"
USAGE
exit 1

#-------------------------------------------------------------------------
# Older changes and errata
#-------------------------------------------------------------------------
# 2.46 - 08/27/16 - Added dw-857-psk-packet profile for PSK2400 testing
#
# 2.45 - 06/26/16 - Alsactl command now a variable and a config file variable
#
# 2.44 - 04/27/16 - Minor update in email address (who to blame)
#
# 2.43 - 10/14/15 - Moved all lock files from /var/tmp to /var/lock to avoid
#                   /var/tmp cleanup cron jobs
#                 - Fixed broken  KISSPKTRIG variable for Xastir config
# 2.42 - 09/08/15 - Minor updates to better support DW HF
#                 - Added an echo statement to remind op to not have FT950 in CW mode
#
# 2.41 - 05/26/15 - Changed AX25VHF1KISSPARMS slottime from 10 to 100
#
# 2.40 - 04/14/15 - Noted that tmd710_tncsetup uses a COM port speed of 9600
#
# 2.40 - 03/21/15 - Start direwolf support
#
# 2.37 - 12/14/14 - minor formatting cleanups
#
# 2.36 - 08/24/14 - Added logging capture for ldsped
#                 - made specifying the beacom path manadatory
#
# 2.35 - 05/25/14 - Added some interface checks after bring up
#
# 2.31 - 03/10/14 - Listen wasn't using the LISTEN binary variable
# 2.30 - 12/10/13 - Added delay to optionally not re-enable suspend mode
#
# 2.24 - 11/28/13 - new version of ldsped has deprecated it's -d option
#
# 2.23 - 10/08/13 - Shutdown Ldsped before AX.25 does down or Ldsped will segfault
#
# 2.22 - 09/29/13 - Confirmed that KISSPARMS TXDELAY values are NOT divided by 10
#                   though the man page says otherwise
#
# 2.21 - 09/08/13 - Added a more specific subnet AMPR route for local 
#                   stations over RF vs. the entire 44.x.x.x the AMPR 
#                   network over RF
#
# 2.20 - 08/31/13 - Added LDSPED support
#
# 2.14 - 08/08/13 - Added a TXTAIL delay of 20ms as well
#
# 2.13 - 06/30/13 - Added an MTU of 236 to the netrom interface
#
# 2.12 - 06/15/13 - Updated to use new KLPRC node in the digi path
#
# 2.11 - 05/07/13 - Added the killing of linpac and call to remove stray
#                   connections and avoid any potential kernel panics on 
#                   shutting down ax.25
#
# 2.10 - 02/17/13 - Added support for aprx - APRS-IS object injection
#                   and digipeating support
#                 - restructured the shutdown section a bit to be more
#                   properly ordered
#
# 2.03 - 01/08/13 - Added a PM state file at /var/tmp/power-mangement.state
#
# 2.02 - 12/16/12 - Changed the beacon time to 90
#                 - Added a variable for netrom broadcast timers
#
# 2.01 - 09/09/12 - Moved the variable definitions around to make it easier to
#                   follow, etc.
#                 - Moved older errata to the bottom of the file for easier reading
#
# 2.00 - 09/03/12 - Added a netmask on the nr0 interface
#
# 1.99 - 08/19/12 - Added a axparms setup to fix Unode being run locally
#
# 1.98 - 08/18/12 - Made the netrom interface name a user setable variable
#                 - now defaulting to pre-loading netrom routes upon load
#                 - fixed an error setting the AMPR netmask
#                 - fixed a few explicit ifconfig commands to use the 
#                   IFCONFIG var instead
#
# 1.97 - 08/11/12 - Send out Netromd packets every 60 min vs 30 (follow the 
#                   defaults
#
# 1.96 - 05/10/12 - Made the Xastir VHFD710RFBAUD variable a number to be
#                   clean
#
# 1.95 - 04/30/12 - Updated the NetROM route save comments to rewrite them
#                   and not be permanent
#
# 1.94 - 04/26/12 - Added ifconfig as kissattach doesn't do netmasks properly
#
# 1.93 - 04/19/12 - Change the beacon string to reflect SCLARA and AMPR IP
#
# 1.92 - 04/09/12 - Added US Interface level resetting
#
# 1.91 - 04/04/12 - Added error output for when running the script from a
#                   non-Xwindows enabled terminal
#
# 1.90 - -3/31/12 - Added in HF soundmodem to align with flexible AX.25 
#                   specific settings for HF
#                 - Added PulseAudio and legacy ALSA level checks
#
# 1.85 - 03/21/12 - Found a proper way to disable suspending using Dbus
#                   and the key was NOT running as root.  Gezzz..
#
# 1.84 - 03/18/12 - New attempt to disable suspending as the old mechanism
#                   didn't work
#
# 1.82 - 01/31/12 - Fixed the KISSTUNE function which was using the wrong port
#                   name (should have been say d710 and not /dev/ttyUSB4
#                 - kissparms uses units of ms and NOT 10ms like the man 
#                   mages infer
#                 - Updated the local serial port
#                 - Added more error checking
#                 - fixed terminating kissattach when shutting down
#                 - many cosmetic cleanups for startup/shutdown text
#
# 1.81 - 01/15/12 - abstracted a few more binary programs into variables
#
# 1.80 - 11/15/11 - added per-beacon KISS parameter tuning for different
#                   performance settings (TX delay, etc.) -- It's unclear
#                   if these are correct due to confusion on the UNITS being
#                   used
#                 - removed the unused netrom attach " netbbs"
#                 - added an option to show running KISS parameters
#                 - added setting the ax.25 MTU and window size via the proc 
#                   system
#                 - added the fact that this script is part of the 
#                   hampacketizing-centos script and all of it's licensing
#
# 1.76 - 10/18/11 - added additional kissattach kill
#
# 1.75 - 10/17/11 - Added the enabling of advanced ax25 support on a 
#                   beacon setting basis
#                 - added a reminder about reloading pre-learned Netrom routes
#
# 1.74 - 10/15/11 - added ax25d and netromd support
#                 - updated the beacon string to reflect node support
# 
# 1.73 - 09/12/11 - Added -l to all kissattach commands
#                 - split out the code for a function: TNC-OFF to later 
#                   support changing the VFO from the script
#                 - support stopping the ax25ipd and kissnetd processes
#                   but we don't start them yet for say ax25ipd
#                 - Changed the power management mechanisms
#
# 1.72 - 08/07/11 - change the UNPROTO path to reverseKTUO and KMOC"
#
# 1.71 - 07/03/11 - Added a requirements document section and more 
#                   documentation
#
# 1.70 - 07/01/11 - Made the initsoundcard system aware of which soundcard
#                   it should use for proper enumeration checks
#                 - Updated the soundmodem mixer settings
#                 - added a comment how soundmodem holds down any TX
#                   to first learn the environment - impacts the 1st beacon
#
# 1.60 - 06/12/11 - put in the $KISSPKTRIG variable for less hard coding
#                   Added debugging to 1BEACON
#                   changed the BASEBEACONPATH to nothing
#
# 1.55 - 03/20/11 - changed beacon time to 45 min
#
# 1.54 - 03/14/11 - Changed some variable names like VHFAX25KISSPARM to 
#                   AX25VHFKISSPARM as the original string would match for
#                   HFAX25KISSPARM
#                 
# 1.53 - 02/25/11 - added ALT3 digi path for local stuff, no digi
#                 - added a beacon period variable and change the default 
#                   from 20 to 30 min
#
# 1.52 - 01/16/11 - changed the ALT1 digi path
#
# 1.51 - 12/19/10 - fixed jnos startup
#                 - added disabling ACPI when the packet system is running
#                 - disabled setting the TNC back to 1200 when shutting down
#
# 1.50 - 11/26/10 - added several kissparms for ax.25 optimizations
#                   with extensive comments; also changed to hardware flow control
#
# 1.48 - 10/29/10 - 1beacon now uses the BEACON variable
#                 - always reset the TNC to 1200baud on stop
#                 - renamed aprs to xastir
#                 - still some commands were hard coded to ttyS0 - fixed
#
# 1.47 - 10/26/10 - added another alt. beacon path
#
# 1.46 - 10/10/10 - Adding examples to help output
#
# 1.45 - 08/22/10 - added a BEACONSTRING variable
#                 - added the ALTBEACON option
#
# 1.44 - 08/14/10 - added improved hf-smdm-packet support
#                 - using the LOG variable properly
#                 - supports linpac init.mac switching for VHF vs HF AX25 interfaces
#                 - added D710 9600 packet support
#
# 1.43 - 08/10/10 - added a check for existing ttyS0 connections
#                 - removed old stty command to use tmd710_tncsetup
#                   for setup and shutdown of the TNC
#
# 1.42 - 08/07/10 - added D710 based JNOS
#
# 1.41 - 07/30/10 - better TNC speed abstraction
#                 - added D710 XASTIR APRS support
#                 - added a re-kiss feature if the radio falls out of KISS mode
#
# 1.40 - 06/22/10 - added D710 KISS mode
#
# 1.30 - 06/19/10 - added check for Alsa card enumeration
#                 - added kernel module removal
# 1.20 - 03/28/10 - added HF packet support with switching soundmodem configs
#
# 1.10 - 12/22/09 - added a custom callsign variable
# 1.09 - 10/17/09 - added hard coding of serial port perms
# 1.08 - 09/01/09 - added a one-time beacon feature - 1beacon
#                   with digipeating support
# 1.07 - 08/25/09 - updated the beacon to reflect linpac support
# 1.06 - 08/23/09 - made sndcard init it's own function
# 1.05 - 08/18/09 - removed color support from listen
# 1.04 - 08/15/09 - renamed to packetrig.sh as we'll use this script for
#                   multiple things
#                   - added jnos and aprs support
# 1.03 - 08/11/09 - added a pause to confirm levels are ok
# 1.02 - 08/01/09 - more documentation; send stderr to $LOG
# 1.01 - 06/27/09 - changed beacon text
#                 - now support changing the device name for keeping mheard
#                   clean

# Todo:
# 03/14/11 - need a HF specific /etc/axports/ax25-smdm-hf file
#          - need to setup HF-specific KISS parameters to better follow
#            very lossy links, etc.
#
