#!/bin/bash

##################################################################################
# Background process started by the initializing script start_aprs_server.sh     #
#                                                                                #
# It processes all incomming traffic and filters out heard stations.             #
#                                                                                #
# (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/>. #
#                                                                                #
##################################################################################

# The script aprs_utils.sh starts a filters means of tail | grep on the log file of aprx.
# The output of this command goes to this script via stdin. This script reads this line by line.

# Directory where APRS data files are stored
APRS_DATA_DIR=/home/marcel/ham/aprs_utils/aprs_log/
APRS_STATIONS_HEARD_DIRECT=aprs_stations_heard_direct.log
APRS_STATIONS_HEARD_DIGIPEATED=aprs_stations_heard_digipeated.log

# If files do not exist, create them
if [ ! -f "$APRS_DATA_DIR$APRS_STATIONS_HEARD_DIRECT" ]; then
    touch $APRS_DATA_DIR$APRS_STATIONS_HEARD_DIRECT
fi
if [ ! -f "$APRS_DATA_DIR$APRS_STATIONS_HEARD_DIGIPEATED" ]; then
    touch $APRS_DATA_DIR$APRS_STATIONS_HEARD_DIGIPEATED
fi

# Maybe there is an old temporary file still pressent: remove it
for ACTIVE_FILE in $APRS_STATIONS_HEARD_DIRECT $APRS_STATIONS_HEARD_DIGIPEATED
do
    rm $APRS_DATA_DIR$ACTIVE_FILE".tmp"
done

# Get current date YYYY-MM-DD
#CURRENT_DATE=$(date +"%Y-%m-%d")
CURRENT_DATE_TIME=$(date +"%Y-%m-%d %H:%M:%S")
LAST_EPOCH_DATE="$(date -d"$CURRENT_DATE_TIME" +%s)"

#Read file line by line and send acknowledge if needed
while read LINE
    do

        # Start filter for received messages to PE1RXF (all sufixes)
        # Messages addressed to PE1RXF are stored in file /home/marcel/ham/aprs_utils/aprs_received_messages.log
        MESSAGE_FOR_PE1RXF="$(grep "::PE1RXF"<<<$LINE)"
        if [[ $MESSAGE_FOR_PE1RXF ]]; then
            #echo "Message from PE1RXF"
            /home/marcel/ham/aprs_utils/process_aprs_messages.sh <<<$MESSAGE_FOR_PE1RXF
        fi

       # Get current date YYYY-MM-DD (do this every time, because a day may already have passed)
       CURRENT_DATE_TIME=$(date +"%Y-%m-%d %H:%M:%S")
       CURRENT_DATE=$(date +"%Y-%m-%d")
 
       # figure out who sent the message
       # Check channel 1 (internal radio)
       CALL="$(grep -o -P "(?<=PE1RXF-1  d \*).+?\>"<<<$LINE)"
       PORT="ax0"
       
       # If call is empty, frame not from channel 1, maybe from channel 2?
       if [ -z $CALL ]; then
            CALL="$(grep -o -P "(?<=PE1RXF-2  d \*).+?\>"<<<$LINE)"
            PORT="ax1"
       fi
       
       # Lets do something with the frame from channel 1 or 2
       if [[ $CALL ]]; then
       
            # Remove last character from string
            CALL=${CALL::-1}
            
            # Get full path of message
            if [ $PORT == "ax0" ]; then
                FULL_PATH="$(grep -o -P '(?<=PE1RXF-1  d \*).+?:'<<<$LINE)"
            else
                FULL_PATH="$(grep -o -P '(?<=PE1RXF-2  d \*).+?:'<<<$LINE)"
            fi
            
            FULL_PATH="$(grep -o -P '(?<=\>).*'<<<$FULL_PATH)"
            # Remove last character from string
            FULL_PATH=${FULL_PATH::-1}

            # Messages direct from sender do not contain a '*' in the path
            DIRECT="$(grep '*' <<<$FULL_PATH)"
            if [ -z $DIRECT ]; then
                DIRECT="true"
            else
                unset DIRECT
            fi
            # First call in path is destination
            DESTINATION="$(awk -F "," '{ print $1} ' <<<$FULL_PATH)"
            
            #echo "Port: " $PORT     
            #echo "Call:" $CALL
            #echo "Path:" $FULL_PATH
            #echo "Destination: "$DESTINATION
        
            # Select correct log file to use: direct or digipeated
            if [ $DIRECT ]; then
                ACTIVE_FILE=$APRS_STATIONS_HEARD_DIRECT
            else
                ACTIVE_FILE=$APRS_STATIONS_HEARD_DIGIPEATED
            fi

            # get line number where call occurs (will return NULL if call is never heard)
            LINE_NUMBER="$(grep -n $PORT","$CALL "$APRS_DATA_DIR$ACTIVE_FILE" | cut -d : -f 1)"
            #LINE_NUMBER=2
            
            # If call already heard: replace line
            if [[ $LINE_NUMBER ]]; then
                # replace found line with new date and time            
                #SED_CMD="'$LINE_NUMBER c\\""$CURRENT_DATE_TIME"",""$CALL"","$DESTINATION"' ""$APRS_DATA_DIR""$ACTIVE_FILE"
                #echo $SED_CMD
                RESULT="$(sed -i "$LINE_NUMBER c\\$CURRENT_DATE_TIME,$PORT,$CALL,$DESTINATION" $APRS_DATA_DIR$ACTIVE_FILE )"
            # If call not heard before: add to file
            else
                echo "$CURRENT_DATE_TIME"",""$PORT"",""$CALL"",""$DESTINATION" >> "$APRS_DATA_DIR""$ACTIVE_FILE"
            fi
            
       
       fi
       
             
        # Do maintenance on all log files (every 10 minutes)
        # Remove all heard calls older than 24 hours
        EPOCH_DATE="$(date -d"$CURRENT_DATE_TIME" +%s)"
        
        EPOCH_COUNTER=$((EPOCH_DATE-LAST_EPOCH_DATE))
        
        if [ "$EPOCH_COUNTER" -gt "600" ]; then
            LAST_EPOCH_DATE="$EPOCH_DATE"
        
            for ACTIVE_FILE in $APRS_STATIONS_HEARD_DIRECT $APRS_STATIONS_HEARD_DIGIPEATED
            do
                touch $APRS_DATA_DIR$ACTIVE_FILE".tmp"
                LINE_NR=1
                while read FILE_LINE; do
                    
                    # reading each line
                    
                    # echo "Line No. $LINE_NR : $FILE_LINE"
                    LINE_DATE="$(awk -F "," '{ print $1} ' <<<$FILE_LINE)"
                    EPOCH_LINE_DATE="$(date -d"$LINE_DATE" +%s)"
                    # echo $EPOCH_LINE_DATE
                    ELAPSED_TIME=$((EPOCH_DATE-EPOCH_LINE_DATE))
                    # echo $ELAPSED_TIME            
                    
                    # Entry older than 24 hours? Remove entry
                    if [ "$ELAPSED_TIME" -lt "86400" ]; then
                        #RESULT="$(sed -e "$LINE_NR d" $APRS_DATA_DIR$ACTIVE_FILE >> $APRS_DATA_DIR$ACTIVE_FILE".tmp")"
                        echo $FILE_LINE >> $APRS_DATA_DIR$ACTIVE_FILE".tmp"
                        # echo "OUD"
                    fi
                    
                    LINE_NR=$((LINE_NR+1))
                    
                done < $APRS_DATA_DIR$ACTIVE_FILE
                cp $APRS_DATA_DIR$ACTIVE_FILE".tmp" $APRS_DATA_DIR$ACTIVE_FILE
                rm $APRS_DATA_DIR$ACTIVE_FILE".tmp"
        
            done
        fi
        # END: remove all heard calls older than 24 hours
       
    
done < /dev/stdin

