#!/bin/bash

##################################################################################
# Reads telemetry data file from yesterday and output minimum and maximum        #
# values of every data field.                                                    #
# This script should be run once every day at midnight                           #
#                                                                                #
# (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/>. #
#                                                                                #
##################################################################################

APRS_RECEIVED_MESSAGES_DIR=/home/marcel/ham/aprs_utils/aprs_log/
APRS_TELEMETRY_FILE_SUFFIX=_telemetry_PE1RXF-7.dat
MIN_TELEMETRY_FILE=telemetry_PE1RXF-7_min_values.dat
MAX_TELEMETRY_FILE=telemetry_PE1RXF-7_max_values.dat

PUBLISH_DIR=/home/marcel/ham/aprs_utils/publish

# Get current and yesterdays date YYYY-MM-DD
CURRENT_DATE=$(date +"%Y-%m-%d")
YESTERDAY_DATE=$(date --date="yesterday" +"%Y-%m-%d")

APRS_TELEMETRY_FILE="$APRS_RECEIVED_MESSAGES_DIR$YESTERDAY_DATE$APRS_TELEMETRY_FILE_SUFFIX"

SINGLE_SHOT=0

declare -a max
declare -a min

#####################################################################
# Evaluate a floating point number conditional expression.
# Bash itself cannot work with floating point, so we use bc

function float_cond()
{
    local cond=0
    if [[ $# -gt 0 ]]; then
        cond=$(echo "$*" | bc -q 2>/dev/null)
        if [[ -z "$cond" ]]; then cond=0; fi
        if [[ "$cond" != 0  &&  "$cond" != 1 ]]; then cond=0; fi
    fi
    local stat=$((cond == 0))
    return $stat
}

#Read file line by line
while read LINE
    do
    #echo "$LINE"
	#Datafiled should have comma as field seperator, so asume this and put values in array for later testing if data is valid
	IFS=',' read -r -a array <<< "$LINE"

    cnt=0      
    for index in "${!array[@]}"
    do
    
        #First time in the loop: we fill the min and max values with the first values in the array
        if [[ $SINGLE_SHOT -eq 0 ]]; then
        #echo "First: ${array[index]}"
           max[cnt]=${array[index]}        
           min[cnt]=${array[index]}
           
        else
            #bash cannot work with floats, so we use this function instead
            if float_cond "${array[index]} > ${max[cnt]}"; then
            #if [[ ${array[index]} > ${max[cnt]} ]]; then
        #echo "Max: $cnt ${max[cnt]} $index ${array[index]}"                
            max[cnt]=${array[index]}

            fi
            #bash cannot work with floats, so we use this function instead
            if float_cond "${array[index]} < ${min[cnt]}"; then
            #if [[ ${array[index]} < ${min[cnt]} ]]; then
                min[cnt]=${array[index]}
       # echo "Min: $cnt $index ${array[index]}"                
            fi
        fi
        #echo "$cnt"
        cnt=$((cnt+1))  
        
	done	

    #First time through loop is different from all the rest 
    SINGLE_SHOT=1
    
#done < /dev/stdin
done < "$APRS_TELEMETRY_FILE"

#Store minimum values in file (append file)
for index in "${!min[@]}"
    do
    #first data field: remove time from date/time field and no leading comma
    if [[ index -eq 0 ]] ; then    
        min[index]="$(echo "${min[index]}" | cut -b 1-10)"
        echo -n "${min[index]}" >> "$APRS_RECEIVED_MESSAGES_DIR$MIN_TELEMETRY_FILE"
      else
        echo -n ",${min[index]}" >> "$APRS_RECEIVED_MESSAGES_DIR$MIN_TELEMETRY_FILE"
      fi
	done

    #New line
	echo "" >> "$APRS_RECEIVED_MESSAGES_DIR$MIN_TELEMETRY_FILE"
	
#Store maximum values in file (append file)
for index in "${!max[@]}"
    do
      #first data field: remove time from date/time field and no leading comma
      if [[ index -eq 0 ]] ; then
        max[index]="$(echo "${max[index]}" | cut -b 1-10)"
        echo -n "${max[index]}" >> "$APRS_RECEIVED_MESSAGES_DIR$MAX_TELEMETRY_FILE"
      else
        echo -n ",${max[index]}" >> "$APRS_RECEIVED_MESSAGES_DIR$MAX_TELEMETRY_FILE"
      fi
	done	

	#New line
    echo "" >> "$APRS_RECEIVED_MESSAGES_DIR$MAX_TELEMETRY_FILE"
