#!/bin/sh

_openbsd() {
	TOTAL="$(free | awk '/^Mem:/ {print $2}')"
	MUSED="$(top -b -n 1 | grep -o 'Real.*' | sed 's/Real: //' | sed 's/\/.*//')"
	printf "$MUSED" | egrep "[0-9]{4}" > /dev/null && FUSED="$(printf "$MUSED" | cut -c -2 | sed 's/./.&/2')G" || FUSED=$MUSED
	printf "$FUSED/$TOTAL\n"
}

_freebsd() {
	TOTAL="$(freecolor -om | awk '/^Mem:/ {print $2}')"
	MUSED="$(freecolor -om | awk '/^Mem:/ {print $3}')"
	printf "$MUSED" | egrep "[0-9]{4}" > /dev/null && FUSED="$(printf "$MUSED" | cut -c -2 | sed 's/./.&/2')G" || FUSED=""$MUSED"M"
	printf "$TOTAL" | egrep "[0-9]{4}" > /dev/null && TOTAL="$(printf "$TOTAL" | cut -c -2 | sed 's/./.&/2')G" || FUSED=$TOTAL
	printf "$FUSED/$TOTAL\n"
}

_linux() {
	TOTAL="$(free -h | awk '/^Mem:/ {print $2}')"
	MUSED="$(free -h | awk '/^Mem:/ {print $3}')"
	printf "$MUSED" | grep "[0-9]{4}" > /dev/null && FUSED="$(printf "$MUSED" | cut -c -2 | sed 's/./.&/2')G" || FUSED=$MUSED
	printf "$FUSED/$TOTAL\n" | sed 's/Gi/GB/g' | sed 's/Mi/MB/g'
}

case $(uname) in
	Linux) _linux ;;
	OpenBSD) _openbsd ;;
	FreeBSD) _freebsd ;;
esac
