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

show_cpu_load_help ()
{

	white=$(bs_ng_echo $white)
	orange=$(bs_ng_echo $orange)
	green=$(bs_ng_echo $green)

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

}

show_cpu_load ()
{

	case $1 in
		*help )
			show_cpu_load_help
		;;

		* )
			NICE_IGNORE=20
			t="0"

			while read cpu ni; do
				if [[ $ni == *-* || $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=$(bs_ng_echo $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=$(bs_ng_echo $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
		;;
	esac

}
