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

show_mem_help ()
{
	white=$(bs_ng_echo $white)
	orange=$(bs_ng_echo $orange)
	iceblue=$(bs_ng_echo $iceblue)
	green=$(bs_ng_echo $green)

	echo -e "\n${white}Usage:\n"
	echo -e "\n${orange}show_mem ${white}|${green} --used ${iceblue}[display used memory in mb]\
	\n${orange}show_mem ${white}|${green} --free ${iceblue}[display free memory in mb]\
	\n${orange}show_mem ${white}|${green} --percent-used ${iceblue}[display used memory in %]\
	\n${orange}show_mem ${white}|${green} --percent-free ${iceblue}[display free memory in %]" | column -t
	echo ""
	tput sgr0

}

show_mem_used ()
{

	used=$(free -m | grep 'buffers/cache' | awk '{print $3}')
	if [[ $used -lt 1000 ]]; then
		echo 0$used
	elif [[ $used -lt 100 ]]; then
		echo 00$used
	else	echo $used
	fi

}

show_mem_used_percent ()
{

	free | {
		read
		read m t u f s b c;
		f=$[$f + $b + $c]
		f=$[100-100*$f/$t]
		if [ $f -gt 100 ]; then
			f=100
		fi
		echo ${f}%
		}

}

show_mem_free ()
{

	free=$(free -m | grep 'buffers/cache' | awk '{print $4}')
	if [[ $free -lt 1000 ]]; then
		echo 0$free
	elif [[ $free -lt 100 ]]; then
		echo 00$ree
	else	echo $free
	fi

}

show_mem_free_percent ()
{

	free | {
		read
		read m t u f s b c;
		f=$[$f + $b + $c]
		f=$[100-100*$f/$t]
		if [ $f -gt 100 ]; then
			f=100
		fi
		echo $((100-${f}))%
		}

}

show_mem () {

	case $1 in
		*used )
			show_mem_used
		;;

		*free )
			show_mem_free
		;;

		*used-percent )
			show_mem_used_percent
		;;

		*free-percent )
			show_mem_free_percent
		;;

		* )
			show_mem_help
		;;
	esac

}
