#!/bin/sh

# update dwm status bar

UPDATE=1

function human_bytes {
        KILO=`expr 1024`
        MEGA=`expr 1024 \* 1024`
        GIGA=`expr 1024 \* 1024 \* 1024`
        if [[ $1 -ge $GIGA ]]; then
                echo -n `echo "scale=2; $1 / $GIGA" | bc` GB/s
        elif [[ $1 -ge $MEGA ]]; then
                echo -n `echo "scale=2; $1 / $MEGA" | bc` MB/s
        elif [[ $1 -ge $KILO ]]; then
                echo -n `echo "scale=2; $1 / $KILO" | bc` KB/s
        else
                echo -n $1 B/s
        fi
}

while true
do
	in=`netstat -b -n -I run0 | tail -1 | awk '{ printf $5 }'`
	out=`netstat -b -n -I run0 | tail -1 | awk '{ printf $6 }'`
	if [ -n "$inprevious" ]; then NETIN=`expr $in - $inprevious`; else NETIN=0; fi 
	if [ -n "$outprevious" ]; then  NETOUT=`expr $out - $outprevious`; else NETOUT=0; fi
	inprevious=$in; NETIN=`human_bytes $NETIN`
	outprevious=$out; NETOUT=`human_bytes $NETOUT`
	TEMPERATURE=`sysctl -n hw.sensors.acpitz0.temp0 | cut -d' ' -f1,2`
	BATTERY=`apm -l`
	AC=`apm -av | cut -d: -f2`
	DATE=`date +'%a %b %d %R %Z %Y'`
	xsetroot -name "Net: Up $NETOUT Down $NETIN Temperature: $TEMPERATURE Battery: $BATTERY% A/C$AC $DATE"
	sleep $UPDATE
done
