#!/bin/bash
#
# Sort proto file for biggest/smallest talkers (in terms of number of packets)

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

usage() {
    printf "Usage:\n"
    printf "    $SNAME [OPTION...] file_protocols.txt\n\n"
    printf "Optional arguments:\n"
    printf "    -p=P        only output ports/protocols with probability higher than P%%\n"
    printf "    -n=N        only output top (N>0) or bottom (N<0) N ports/protocols\n"
    printf "    -l2         do not output statistics for L2/3 protocols\n"
    printf "    -l4         do not output statistics for L4 protocols\n"
    printf "    -tcp        do not output statistics for TCP\n"
    printf "    -udp        do not output statistics for UDP\n"
    printf "    -udplite    do not output statistics for UDP-Lite\n"
    printf "    -sctp       do not output statistics for SCTP\n"
    printf "    -h, --help  display this help, then exit\n"
}

# Default values
TOPFUNC=head
L2=1;
L4=1;
PROTO=(TCP UDP UDP-Lite SCTP)

while [ $# -gt 0 ]; do
    case "$1" in
        # -p=P, -p P
        -p=*)
            P="${1#*=}"
            ;;
        -p)
            validate_next_float "$1" "$2"
            P="$2"
            shift
            ;;
        # -n=N, -n=-N, -n N -n -N
        -n=*)
            N="${1#*=}"
            ;;
        -n=-*)
            N="${1#*=-}"
            TOPFUNC=tail
            ;;
        -n)
            validate_next_int "$1" "$2"
            if [ $2 -ge 0 ]; then
                N="$2"
            else
                N="$((-$2))"
                TOPFUNC=tail
            fi
            shift
            ;;

        -l2) L2=0;;
        -l4) L4=0;;

        -tcp    ) PROTO=(${PROTO[@]/TCP/});;
        -udp    ) PROTO=(${PROTO[@]/UDP/});;
        -udplite) PROTO=(${PROTO[@]/UDP-Lite/});;
        -sctp   ) PROTO=(${PROTO[@]/SCTP/});;

        -h|-\?|--help)
            usage
            exit 0
            ;;
        *)
            if [ ! -f "$1" ]; then
                abort_option_unknown "$1"
            fi
            FILE="$1"
            ;;
    esac
    shift
done

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

# Number of rows
[ -z "$N" ] && N=$(wc -l "$FILE" | $AWK '{print $1}')

if [ $L2 -eq 1 ] && [ ! -z "$(grep "L2/3" "$FILE")" ]; then
    printf "L2/3 Protocol\tPackets\tProbability[%%]\tDescription\n"
    $AWK -F"\t" -v P="$P" '
        $1 ~ /^0x/ && $3 > P { print }
        $0 ~ /^# L4/ { exit }
    ' "$FILE" | sort -nrk2 | $TOPFUNC -"$N"
    echo
fi

if [ $L4 -eq 1 ]; then
    printf "L4 Protocol\tPackets\tProbability[%%]\tDescription\n"
    $AWK -F"\t" -v P="$P" '
        $1 ~ /^0x/ { next }
        $1 ~ /^[0-9]/ && $3 > P { print }
        $0 ~ /^# Total TCP/ { exit }
    ' "$FILE" | sort -nrk2 | $TOPFUNC -"$N"
    echo
fi

for ((i = 0; i < ${#PROTO[@]}; i++)); do
    printf "${PROTO[i]} Port\tPackets\tProbability[%%]\tDescription\n"
    $AWK -F"\t" -v T="${PROTO[i]}" -v S="${PROTO[i+1]}" -v P="$P" '{
        if (A == 0 && $0 !~ "# Total " T) next
        else A = 1
        if ($1 ~ /^[0-9]/ && $3 > P) print
        else if ($0 ~ "# Total " S) exit
    }' "$FILE" | sort -nrk2 | $TOPFUNC -"$N"
    echo
done
