#!/bin/sh

# Do the previous day by default
x=$(dispdate -t)
x=$((x-86400))
month=$(dispdate -t $x +%m)
day=$(dispdate -t $x +%d)
year=$(dispdate -t $x +%Y)

# Get month, day and year on command if provided
if [ $# -ge 3 ]; then
 month=$1
 day=$2
 year=$3
fi

# Get log file rollover search limit
limit=40
if [ $# -ge 4 ]; then limit=$4; fi

# Set pattern to match in log files
matchpattern="$month/$day/$year"

# Nomatch file is source addresses of the repeaters
nomatchfile="/usr/ns/cus/apduidnomatch.txt"

# Copies files to desired location and grabs the lines needed
cp /usr/ns/log/nsrecdata.log.1 "/www/flatfiles/apduid/nsrecdata.log.1"
cp /usr/ns/log/nsrecdata.log "/www/flatfiles/apduid/nsrecdata.log"
cp /usr/ns/log/nsrecdata.log.2.gz "/www/flatfiles/apduid/nsrecdata.log.2.gz"

# Greps recdata for yesterday, only lines with apduid, and gets rid of lines of repeater apduid
gunzip <"/www/flatfiles/apduid/nsrecdata.log.2.gz" | grep -e "$matchpattern" | grep -e ' APDUID=' | grep -v -f $nomatchfile > "/www/flatfiles/apduid/apduidtemp.log"
grep -e "$matchpattern" "/www/flatfiles/apduid/nsrecdata.log.1" | grep -e ' APDUID=' | grep -v -f $nomatchfile >> "/www/flatfiles/apduid/apduidtemp.log"
grep -e "$matchpattern" "/www/flatfiles/apduid/nsrecdata.log" | grep -e ' APDUID=' | grep -v -f $nomatchfile >> "/www/flatfiles/apduid/apduidtemp.log"

# This awk is meant to strip the raw line and put all the recdata values into a clean csv format
        # Strips line until SA
awk -F "[, ]+" '{gsub(/^.*SA: |TS=/,"");
        sourceaddress=$1
        # Grabs the time
        starttime = $(NF-1) " " $NF
        # Strips line until APDUID
        gsub(/^.*APDUID=/,"")
        apduid = $1
        # Displays each line in CSV format
        print starttime "," sourceaddress "099," apduid > "/www/flatfiles/apduid/tempfile1.log"
}' "/www/flatfiles/apduid/apduidtemp.log"

# This awk removes duplicate values without having to sort, because sorting would mess with the ordering of the APDUID values.
    # Counts all unique rows
awk '{if (!counts[$0]++)
	{keys[k++] = $0}
}
    # Prints all unique rows
END {for (i = 0; i < k; ++i)
	{key = keys[i]
        print key > "/www/flatfiles/apduid/tempfile2.log"}
}' "/www/flatfiles/apduid/tempfile1.log"

awk '{ print $0 "," NR > "/www/flatfiles/apduid/tempfile3.log" }' "/www/flatfiles/apduid/tempfile2.log"

cat "/www/flatfiles/apduid/tempfile3.log" | sort -t, -nk2,2 -nk4,4 > "/www/flatfiles/apduid/tempfile4.log"

# This awk will add time to any reports that happened at the same time, allowing them to be ingested into the database.
awk -F "[, ]+" 'BEGIN{
     count = 0
     testtime = 0}
{
     # Removes / and : from time format
     gsub("/"," ")
     gsub(":"," ")
     # Displays receive time and report time in corcdrect pattern for mktime which is yyyy mm dd HH MM SS
     starttime = $3 " " $1 " " $2 " " $4 " " $5 " " $6
     # Converts time into epoch
     epochtime = mktime(starttime)

     # This if statement determines if a time value is the same as the previous one, and if it is then adds 6 seconds.
     if (testtime < epochtime)
	{count = 0
	newepoch = epochtime
	testtime = epochtime}
     else
	{count = count + 6
	newepoch = epochtime + count}
     # Converts back to standard time format
     newtime = strftime("%m/%d/%Y %H:%M:%S",newepoch)
     # Prints ouput in csv format as Time,ID,APDUID
     print newtime "," $7 "," $8 > "/www/flatfiles/apduid/apduid.log"
}' "/www/flatfiles/apduid/tempfile4.log"

# This awk counts the amount of reported APDUID values and expected APDUID values
# then finds the performance %
           # If it is the first line, set all the starting values
awk -F [,] '{if (NR == 1)
{count = 0
expect = 0
apduid = $3
pointid = $2}

# If the point ID of this line is the same as the previous line then continue counting
else if ($2 == pointid)
{
     # Sets the difference between last APDUID and current APDUID
     apduiddiff=$3 - apduid
          # If the difference is greater than zero, continue as normal
          if (apduiddiff >0)
               {expect = expect + apduiddiff
               count = count + 1
               apduid = $3
               pointid = $2}
          # If the difference is zero or less, adds 7. This will correctly find the
          # missing amount of reports. ex: old=3 new=1 diff=-2 so expect=7+-2=5 expected
          # values which are 4,5,6,0,1
          else
               {expect = expect + 7 + apduiddiff
               count = count + 1
               apduid = $3
               pointid = $2}
}
# If the point ID is not the same as the previous line then display results
else
{
     print $1","pointid-1","count/expect*100 > "/www/flatfiles/apduid/apduidperformance.log"
     count = 0
     expect = 0
     pointid = $2
     apduid = $3
}}
# at the end the last ID wont be sent because there is not a line so this displays results of last ID
END{
     print $1","pointid-1","count/expect*100 >> "/www/flatfiles/apduid/apduidperformance.log"
}' "/www/flatfiles/apduid/tempfile4.log"

# Remove temporary files
rm -f "/www/flatfiles/apduid/tempfile1.log"
rm -f "/www/flatfiles/apduid/tempfile2.log"
rm -f "/www/flatfiles/apduid/tempfile3.log"
rm -f "/www/flatfiles/apduid/tempfile4.log"
rm -f "/www/flatfiles/apduid/apduidtemp.log"
rm -f "/www/flatfiles/apduid/nsrecdata.log"
rm -f "/www/flatfiles/apduid/nsrecdata.log.1"
rm -f "/www/flatfiles/apduid/nsrecdata.log.2.gz"
