#!/bin/bash
#########################################################
# 							#
# This is BashStyle-NG 7.8				#
#							#
# Licensed under GNU GENERAL PUBLIC LICENSE v3    	#
#							#
# Copyright 2007 - 2009 Christopher Bratusek		#
#							#
#########################################################

white=$(bs-ng-echo $white)
orange=$(bs-ng-echo $orange)
iceblue=$(bs-ng-echo $iceblue)
green=$(bs-ng-echo $green)

NICE_IGNORE=20

_help ()
{

	echo -e "\n${white}Usage:\n"
	echo -e "${orange}showcpuload${white} |${green} ! no options !\n"

}

_cpu_load ()
{

	t="0"

	while read cpu ni; do
		if [[ $ni -le $NICE_IGNORE ]]; then
			t="$t + $cpu"
		fi
		if [[ ${cpu%%.*} -eq 0 ]]; then
			break
		fi
	done < <(ps -Ao "%cpu= ni="| sort -r)


	cpu=$(echo "$t" | bc)

	if [[ ! "${cpu#.}x" = "${cpu}x" ]]; then
		cpu="0${cpu}"
	fi

	cpu=${cpu%%.*}

	if [[ $cpu -gt 100 ]]; then
		cpu=100
	fi

	if [[ $cpu -lt 16 ]]; then
		color=${iceblue}
	elif [[ $cpu -lt 26 ]]; then
		color=$(bs-ng-echo $turqoise)
	elif [[ $cpu -lt 41 ]]; then
		        color=$(bs-ng-echo $smoothgreen)
	elif [[ $cpu -lt 61 ]]; then
		color=${green}
	elif [[ $cpu -lt 81 ]]; then
		color=$(bs-ng-echo $yellow)
	else	color=$(bs-ng-echo $red)
	fi

	if [[ $cpu -lt 10 ]]; then
		prepend=00
	elif [[ $cpu -lt 100 ]]; then
		prepend=0
	fi

	if [[ $enabcol == true ]]; then
		echo -e "$color$prepend$cpu"
	else	echo $prepend$cpu
	fi

}

case $1 in

	*help )

		_help

	;;

	* )

		_cpu_load

	;;

esac
