#!/usr/bin/env bash

################################
# Shows info about the CPU
#
# Dependencies:
# - [notify-send]
#
# @return {Number(%)}: CPU usage
################################

dir=$(dirname $0)
source $dir/util.sh

full=""
short=""
status=0

read cpu a b c previdle rest < /proc/stat
prevtotal=$(calc "$a + $b + $c + $previdle")
sleep 0.5
read cpu a b c idle rest < /proc/stat
total=$(calc "$a + $b + $c + $idle")

CPU=$(calc "100 * (($total - $prevtotal) - ($idle - $previdle)) / ($total - $prevtotal)")

full="$CPU%"
short=$full

if [ $CPU -ge 90 ]; then
	status=33
fi

case $BLOCK_BUTTON in

	# right click: show packages
	3)
		n=16
		summary=$(ps -eo pcpu,pmem,pid,comm --sort=-pcpu | head -$n)
		notify-send "Top $n CPU-eaters" "$summary"
	;;

esac

echo $full
echo $short
exit $status
