#!/bin/bash

##################################################################################
# Send an APRS message consrructed from the given parameters.                    #
#                                                                                #
# (C)2021 M.T. Konstapel https://meezenest.nl/mees                               #
#                                                                                # 
# This file is part of PE1RXF-APRS-server.                                       #
#                                                                                #
# NOTE: the ax25 utility 'beacon' has a bug: the first few times it is called    #
#       the send string ends with some random characters. After these first few  #
#       times it all comes good and the utility works fine.                      #
#                                                                                #
#    PE1RXF-APRS-server is free software: you can redistribute it and/or modify  #
#    it under the terms of the GNU General Public License as published by        #
#    the Free Software Foundation, either version 3 of the License, or           #
#    (at your option) any later version.                                         #
#                                                                                #
#    PE1RXF-APRS-server is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of              #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               #
#    GNU General Public License for more details.                                #
#                                                                                #  
#    You should have received a copy of the GNU General Public License           #
#    along with PE1RXF-APRS-server  If not, see <https://www.gnu.org/licenses/>. #
#                                                                                #
##################################################################################

source /home/marcel/ham/aprs_utils/config.shlib; # load the config library functions
ConfigFile=/home/marcel/ham/aprs_utils/pe1rxf-aprs-server.cfg

# Directory where APRS messages are stored
APRS_RECEIVED_MESSAGES_DIR=/home/marcel/ham/aprs_utils/aprs_log/
MESSAGE_FILE=aprs_send_messages.log

############################################################
# Help                                                     #
############################################################
Help()
{
   # Display Help
   echo "Send APRS message."
   echo
   echo "Syntax: set_beacon.sh [-i <interface>|-c <call>|-p <path>|-m <message>]"
   echo "Options:"
   echo "-i     AX.25 interface (ax0 or ax1)"
   echo "-c     Destination call"
   echo "-p     Path: 0=none, WIDE2-1, WIDE2-2, WIDE3-3 or call of digipeater"
   echo "-m     Message to send."
   echo
}

############################################################
############################################################
# Main program                                             #
############################################################
############################################################

# Set variables
Interface="0"
Call="0"
Path="0"
Message=""

############################################################
# Process the input options.                               #
############################################################
# Get the options
while getopts i:c:p:m: flag; do
   case "${flag}" in
     i) # Interface
         Interface=${OPTARG};;
     c) # Destination Call
         Call=${OPTARG};;
     p) # Path
         Path=${OPTARG};;
     m) # Message
         Message=${OPTARG};;         
     \?) # Invalid option
         echo "Syntax error!"
         Help
         exit;;         
   esac
done

CURRENT_DATE_TIME=$(date +"%Y-%m-%d %H:%M:%S")

if [ "$Interface" == "ax0" ]; then
    
    # Log message
    echo "$CURRENT_DATE_TIME,ax0,$Call,APRX29,$Message" >> $APRS_RECEIVED_MESSAGES_DIR$MESSAGE_FILE
    
    # if call is less than 9 characters, add spaces at the end (according to the APRS protocol)
    printf -v Call %-9.9s "$Call"
    if [ "$Path" == "0" ]; then
        APRS_FRAME="PE1RXF-1>APRX29::$Call:$Message"
    else
        APRS_FRAME="PE1RXF-1>APRX29,$Path::$Call:$Message"
    fi
    AX0_APRS_TRANSMIT_DIR="$(config_get ax0_transmit_directory $ConfigFile)"
    echo "$APRS_FRAME" > "$AX0_APRS_TRANSMIT_DIR"message.msg
    #echo "$APRS_FRAME $AX0_APRS_TRANSMIT_DIR"
    
else
    # Log message
    echo "$CURRENT_DATE_TIME,ax1,$Call,APRX29,$Message" >> $APRS_RECEIVED_MESSAGES_DIR$MESSAGE_FILE
    
    # if call is less than 9 characters, add spaces at the end (according to the APRS protocol)
    printf -v Call %-9.9s "$Call"
    if [ "$Path" == "0" ]; then
        APRS_FRAME="PE1RXF-2>APRX29::$Call:$Message"
        args[0]=-d
        args[1]="APRX29"
        args[2]=-s
        args[3]=ax1
        args[4]=":$Call:$Message"
    else
        APRS_FRAME="PE1RXF-2>APRX29,$Path::$Call:$Message"
        args[0]=-d
        args[1]="APRX29 $Path"
        args[2]=-s
        args[3]=ax1
        args[4]=":$Call:$Message"
    fi
    #echo "$APRS_FRAME"

    #"$APRS_CMD" > tmp.tmp
    
    /usr/sbin/beacon "${args[@]}"
    
fi
