#!/bin/bash

##################################################################################
# Send beacon via APRS channel 2 (ax1)                                           #
#                                                                                #
# (C)2021 M.T. Konstapel https://meezenest.nl/mees                               #
#                                                                                # 
# This file is part of PE1RXF-APRS-server.                                       #
#                                                                                #
#    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"
BEACON_FILE="$(config_get ax1_beacon_file $ConfigFile)"

if [ -f "$BEACON_FILE" ]; then
    BEACON_TEXT=$(cat "$BEACON_FILE")
    
    # The BEACON_FILE holds the command for sending a beacon. So make this file part of the script.
    # I shouldn't really do this, as it is a potential security risk. But the BEACON_FILE is as safe as this script itself.
    # The BEACON_FILE can not be altered via the web interface, but only by the main program. So it should be ok.
    # But this makes the code potential self altering, which is bad! Realy realy bad!
    # But than again, is it really not the same as calling an external program from the script, as this program can also be alterred.
    # Anyway, enough of my rambling, let's continue with the script.
    
    # To be safe, lets do some minimal safety checks before executing the external file:
    
    # BEACON_FILE should only have one line:
    NR_OF_LINES=$(awk 'END { print NR }' $BEACON_FILE)
    # And this line should contain the beacon command:
    RESULT=$(grep "/usr/sbin/beacon" $BEACON_FILE)
    
    if [ -n "$RESULT" ] && [ $NR_OF_LINES -eq 1 ]; then
        # Send the beacon
        . $BEACON_FILE
    fi
    
fi
