#!/bin/sh -e
#
#    mem_available: grab the current memory and usage
#
#    Copyright (C) 2008 Canonical Ltd.
#    Copyright (C) 2011-2014 Dustin Kirkland
#
#    Authors: Dustin Kirkland <kirkland@byobu.org>
#
#    This program is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, version 3 of the License.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program.  If not, see <http://www.gnu.org/licenses/>.

__memory_detail() {
	if eval $BYOBU_TEST free >/dev/null 2>&1; then
		free
	else
		vmstat
	fi
}

__memory() {
	local free="" total="" available="" buffers="" cached=""
	local kb_main_used=0 buffers_plus_cached=0 fo_buffers=0 fo_cached=0
	if [ -r /proc/meminfo ]; then
		while read tok val unit; do
			case "$tok" in
				MemTotal:)     total=${val};;
				MemFree:)      free=${val};;
				MemAvailable:) available=${val};;
				Buffers:)      buffers=${val};;
				Cached:)       cached=${val};;
			esac
			# MemAvailable (Linux 3.14+) appears before Buffers/Cached;
			# break early once we have total + available for efficiency.
			[ -n "${total}" -a -n "${available}" ] && break;
		done < /proc/meminfo
	elif [ "$BYOBU_OSTYPE" = "FreeBSD" ]; then
		# FreeBSD support
		page_size=$(sysctl -n vm.stats.vm.v_page_size)
		page_count=$(sysctl -n vm.stats.vm.v_page_count)
		free_count=$(sysctl -n vm.stats.vm.v_free_count)
		cache_count=$(sysctl -n vm.stats.vm.v_cache_count)
		inactive_count=$(sysctl -n vm.stats.vm.v_inactive_count)
		total=$(((${page_size} * ${page_count}) / 1024))
		free=$(((${page_size} * (${free_count} + ${cache_count} + ${inactive_count})) / 1024))
		buffers=0
		cached=0
	elif [ "$BYOBU_OSTYPE" = "OpenBSD" ]; then
		# OpenBSD support: use sysctl and vmstat
		local physmem pagesize free_pages inactive_pages
		physmem=$(sysctl -n hw.physmem 2>/dev/null)
		[ -n "$physmem" ] || return
		total=$((physmem / 1024))
		pagesize=$(sysctl -n hw.pagesize 2>/dev/null)
		# Count both free and inactive pages as available memory
		free_pages=$(vmstat -s 2>/dev/null | awk '/pages free$/ { f=$1 } /pages inactive$/ { i=$1 } END { print f+0, i+0 }')
		if [ -n "$free_pages" ] && [ -n "$pagesize" ]; then
			set -- $free_pages
			free=$(( (${1:-0} + ${2:-0}) * pagesize / 1024 ))
		else
			free=0
		fi
		buffers=0
		cached=0
	elif eval $BYOBU_TEST vm_stat >/dev/null 2>&1; then
		# MacOS support
		# calculation borrowed from http://apple.stackexchange.com/a/48195/18857
		free_blocks=$(vm_stat | grep free | awk '{ print $3 }' | sed -e 's/\.//')
		inactive_blocks=$(vm_stat | grep inactive | awk '{ print $3 }' | sed -e 's/\.//')
		speculative_blocks=$(vm_stat | grep speculative | awk '{ print $3 }' | sed -e 's/\.//')
		free=$((($free_blocks+speculative_blocks)*4))
		inactive=$(($inactive_blocks*4))
		total=$((($free+$inactive)))
		buffers=0
		cached=0
	fi
	if [ -n "$available" ]; then
		# MemAvailable accounts for reclaimable slab memory in addition
		# to free + buffers + cached; matches what 'free -h' reports.
		fo_buffers=$(($total - $available))
	else
		kb_main_used=$(($total-$free))
		buffers_plus_cached=$(($buffers+$cached))
		fo_buffers=$(($kb_main_used - $buffers_plus_cached))
	fi
        fpdiv $((100*${fo_buffers})) "${total}" 0;
        usage=${_RET}
	if [ $total -ge 1048576 ]; then
		fpdiv "$total" 1048567 1
		total=${_RET}
		unit="$ICON_GB"
	elif [ $total -ge 1024 ]; then
		fpdiv "$total" 1024 0
		total=${_RET}
		unit="$ICON_MB"
	else
		unit="$ICON_KB"
	fi
	if [ $usage -gt 90 ]; then
		# Change foreground to yellow, if usage over 90%
		fg="Y"
	else
		fg="W"
	fi
	[ -n "$total" ] || return
	color b g W; printf "%s" "$total"; color -; color g W; printf "%s" "$unit"; color -; color b g "$fg"; printf "%s" "$usage"; color -; color g "$fg"; printf "%s" "$PCT"; color --
}

# vi: syntax=sh ts=4 noexpandtab
