#!/bin/bash
# thanks to Luke Smith

update() {
	sum=0
	for arg; do
		read -r i <"$arg"
		sum=$((sum + i))
	done
	cache=${XDG_CACHE_HOME:-$HOME/.cache}/${1##*/}
	[ -f "$cache" ] && read -r old <"$cache" || old=0
	printf %d\\n "$sum" >"$cache"
	printf %d\\n $((sum - old))
}

tx=$(update /sys/class/net/enp4s0/statistics/tx_bytes)
rx=$(update /sys/class/net/enp4s0/statistics/rx_bytes)

[ "$1" = "down" ] && printf "%4sB\\n" $(numfmt --to=iec $rx) && exit
[ "$1" = "up" ] && printf "%4sB\\n" $(numfmt --to=iec $tx) && exit
