#!/bin/sh

if [ $# -lt 1 ]; then
	echo usage: $0 [options] inputuri...
	echo See manpage for more details.
	exit
fi

top_count=10
send=1
sort="bytes"
filter=""
addr="v4"
threads=4
ignoresource=""
ignoredest=""

while getopts "t:f:sdn:bapA:hSD" opt; do
	case $opt in
		A)
			addr=$OPTARG
			;;
		f)
			filter=$OPTARG
			;;
                S)
                        ignoresource="-S"
                        ;;
                D)
                        ignoredest="-D"
                        ;;
		s)
			send=1
			;;
		d)
			send=0
			;;
                t)
                        threads=$OPTARG
                        ;;
		n)
			top_count=$OPTARG
			;;
		b)
			sort="bytes"
			;;
		a)
			sort="app"
			;;
		p)
			sort="pkts"
			;;
		h)
			echo usage: $0 [options] inputuri...
			echo Type \'man tracetopends\' for more details.
			exit 1
			;;
		/?)
			echo "Invalid option: -$OPTARG"
			exit 1
			;;
		:)
			echo "Option -$OPTARG requires an argument"
			exit 1
			;;
	esac
done

sort_index=0

if [ $send = 1 ]; then
	if [ $sort = "bytes" ]; then
		sort_index=4
	elif [ $sort = "pkts" ]; then
		sort_index=3
	else
		sort_index=5
	fi
fi
	
if [ $send = 0 ]; then
	if [ $sort = "bytes" ]; then
		sort_index=7
	elif [ $sort = "pkts" ]; then
		sort_index=6
	else
		sort_index=8
	fi
fi

exec 		

shift $(($OPTIND - 1))

if [ $addr = "mac" ]; then
	printf "%18s %16s %16s %16s %16s %16s %16s %16s\n" \
	"MAC Address" \
	"Last Seen" \
	"Src Pkts" \
	"Src Bytes" \
	"Src Payload" \
	"Dst Pkts" \
	"Dst Bytes" \
	"Dst Payload"
fi

if [ $addr = "v4" ]; then
	printf "%16s %16s %16s %16s %16s %16s %16s %16s\n" \
	"IPv4 Address" \
	"Last Seen" \
	"Src Pkts" \
	"Src Bytes" \
	"Src Payload" \
	"Dst Pkts" \
	"Dst Bytes" \
	"Dst Payload"
fi

if [ $addr = "v6" ]; then
	printf "%40s %16s %16s %16s %16s %16s %16s %16s\n" \
	"IPv6 Address" \
	"Last Seen" \
	"Src Pkts" \
	"Src Bytes" \
	"Src Payload" \
	"Dst Pkts" \
	"Dst Bytes" \
	"Dst Payload"
fi

if [ "$filter" = "" ]; then
	traceends -t $threads -A $addr $ignoredest $ignoresource $@ | { trap '' int; sort -n -k $sort_index -r -s; } | { trap '' int; head -n $top_count; }
else
	traceends -t $threads -A $addr -f "$filter" $ignoredest $ignoresource $@  | { trap '' int; sort -n -k $sort_index -r -s; } | { trap '' int; head -n $top_count; }
fi

exit 0 

