#!/usr/bin/env bash
#
# This script calculates statistical distributions of selected colums/flows
# from a flowfile, e.g., the number of packets, packet size or any other value
# in a flow file. It automatically calculates the bin size and accepts manual
# entries max_value bin_size. 0 counts can be suppressed in the output and the
# flow direction can be selected A:0, B:1 or B:both

source "$(dirname "$0")/t2utils.sh"

usage() {
    echo "Usage:"
    echo "    $SNAME [OPTION...] <FILE_flows.txt>"
    echo
    echo "Optional arguments:"
    echo "    -c col        Column name or number"
    echo "    -d dir        Select one flow direction only (A or B)"
    echo "    -m max        Maximum value to bin (requires -s option)"
    echo "    -n            No bin statistics"
    echo "    -s size       Bin size (requires -m option)"
    echo "    -t            gnuplot mode"
    echo "    -0            Suppress 0 counts"
    echo
    echo "Help and documentation arguments:"
    echo "    -h, --help    Show this help, then exit"
}

# Default values
DIR=2
NPRT=1
T2PLT=0

while [ $# -ne 0 ]; do
    case "$1" in
        -c)
            validate_next_arg "$1" "$2"
            COL="$2"
            shift
            ;;
        -m)
            validate_next_num "$1" "$2"
            MAX="$2"
            shift
            ;;
        -s)
            validate_next_num "$1" "$2"
            BINSIZE="$2"
            shift
            ;;
        -0) ZERO=1;;
        -n) NPRT=0;;
        -t) T2PLT=1;;
        -d|--dir)
            validate_next_arg "$1" "$2"
            case "$2" in
                A|0) DIR=0;;
                B|1) DIR=1;;
                *)
                    printerr "Invalid direction '$2'"
                    abort_with_help
                    ;;
            esac
            shift
            ;;
        -h|-\?|--help) usage; exit 0;;
        *)
            if [ ! -f "$1" ]; then
                abort_option_unknown "$1"
            fi
            FLOWFILE="$1"
            ;;
    esac
    shift
done

if [ ! -f "$FLOWFILE" ]; then
    abort_required_file
fi

if [ -z "$COL" ]; then
    printerr "Column name or number is required"
    abort_with_help
fi

if [[ -n "$BINSIZE" && -z "$MAX" ]] || [[ -n "$MAX" && -z "$BINSIZE" ]]; then
    printerr "-m and -s options must be used simultaneously"
    abort_with_help
fi

$TAWK -t -H -v pos="$COL" -v fl="$DIR" -v maxval="$MAX" -v bin="$BINSIZE" -v sup="$ZERO" -v nprt="$NPRT" -v t2plt="$T2PLT" '

    BEGIN {
        m = 0
        ss = 0
        s = 0
        var = 0

        if (pos !~ /^[0-9]+$/) {
            _validate_col(COL[pos], pos)
            pos = COL[pos]
        }
    }

    fl == 0 &&  bitsanyset($flowStat, 0x01) { next } # Ignore B flows

    fl == 1 && !bitsanyset($flowStat, 0x01) { next } # Ignore A flows

    {
        i = strtonum($pos)
        #if (!i) next
        ++a[i]
        if (i > m) m = i
        s += i
        ss++
    }

    END {
        if (pos !~ /^[0-9]+$/ || !pos) exit 1

        if (!maxval) {
            maxval = 10^(int(log(m)/log(10)) + 1)
            bin = maxval/10
        }

        if (nprt) print "bin", "count", "relCnt"

        cc = 0
        s2 = ss / 2
        sm = s / ss

        for (i = 0; i < maxval; i += bin) {
            c = 0
            for (j = i; j < i + bin; j++) {
                c += a[j]
                k = sm - a[j]
                var += k * k
            }
            if (cc < s2) {
                cc += c
                ccm = c
                im = i
                ime = i + bin
            }

            if (nprt && (!sup || c)) {
                if (t2plt) {
                    printf "%d\t%d\t%f\n", i, c, 100*c/ss
                    if (bin > 1) printf "%d\t%d\t%f\n", i+bin-1, c, 100*c/ss
                } else {
                    print i "-" i+bin-1, c, 100*c/ss
                }
            }
        }

        im1 = im
        if (im1) im1--
        med = im1 + (s2-cc+ccm)/ccm * (ime-im)
        stddev = sqrt(var/ss)
        skew = (sm - med)/stddev
        if (!t2plt) {
            printf "----------------------------------\n"
            printf "#Flows : %lu\n", ss
            printf "Col Sum: %lu\n", s
            printf "Max Col: %lu\n", m
            printf "Median : %f\n", med
            printf "Mean   : %f\n", sm
            printf "Std Dev: %f\n", stddev
            printf "KP Skew: %f\n", skew
        }
    }' "$FLOWFILE"
