#!/bin/bash

##################################################################################
# Main loop of PE1RXF-APRS-server.                                               #
# Here it all starts. From here all functions are called.                        #
#                                                                                #
# (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"

web_interface_config_file="/home/marcel/ham/web-interface/config/pe1rxf-aprs-server.cfg"

ConfigFileMD5_previous=0

while :
do
    
    # First check if configuration file has changed. If so, update the processes with the new changes.
    # Ontherwise, do nothing.
    ConfigFileMD5=$(md5sum $ConfigFile | cut -d ' ' -f1)
    if [ $ConfigFileMD5 != $ConfigFileMD5_previous ]; then
        echo "Configuration file has changed."
        
        # Update beacon files and corresponding crontab entries
        /home/marcel/ham/aprs_utils/set_beacon.sh -c $ConfigFile
        
        ConfigFileMD5_previous=$ConfigFileMD5
    fi
    
    # If configuration file on web interface exists, we copy it to the program directory
    # and remove it from the web interface
    if [ -f "$web_interface_config_file" ]; then
        cp $web_interface_config_file $ConfigFile
        rm -f $web_interface_config_file
        
        # change beacon settings
        /home/marcel/ham/aprs_utils/set_beacon.sh -c $ConfigFile
    fi
    
    echo
    echo "Loop repeats every 1 seconds. Press [CTRL+C] to stop.."
    sleep 1
    
done
