#!/bin/sh
# 
# by Karsten Kruse <tecneeq@tecneeq.de>
#

if [ ! _NetBSD = _`uname -s` ] ; then
	echo "Kid, get yerself a System: www.netbsd.org"
	exit 1
fi

show_kb(){
vmstat -s | awk '
/ bytes per page$/ { bpp = $1 }
/ pages managed$/ { totalmem = $1 }
/ pages free$/ { free = $1 }
/ cached file pages$/ { fcache = $1 }
/ cached executable pages$/ { ecache = $1 }
/ swap pages$/ { totalswap = $1 }
/ swap pages in use$/ { swapused = $1 }
END {
    printf("        %10s %10s %10s %10s\n","total","used","free","buffers");
    printf("Mem:    %10d %10d %10d %10d\n",totalmem * bpp / 1024,(totalmem - free) * bpp / 1024,free * bpp / 1024,(fcache + ecache) * bpp / 1024);
    printf("Swap:   %10d %10d %10d\n",totalswap * bpp / 1024,swapused * bpp / 1024,(totalswap - swapused) * bpp / 1024);
}'
}

show_mb(){
vmstat -s | awk '
/ bytes per page$/ { bpp = $1 }
/ pages managed$/ { totalmem = $1 }
/ pages free$/ { free = $1 }
/ cached file pages$/ { fcache = $1 }
/ cached executable pages$/ { ecache = $1 }
/ swap pages$/ { totalswap = $1 }
/ swap pages in use$/ { swapused = $1 }
END {
    printf("        %10s %10s %10s %10s\n","total","used","free","buffers");
    printf("Mem:    %10d %10d %10d %10d\n",totalmem * bpp / 1024 / 1024,(totalmem - free) * bpp / 1024 / 1024,free * bpp / 1024 / 1024,(fcache + ecache) * bpp / 1024 / 1024);
    printf("Swap:   %10d %10d %10d\n",totalswap * bpp / 1024 / 1024,swapused * bpp / 1024 / 1024,(totalswap - swapused) * bpp / 1024 / 1024);
}'
}

case $1 in
    -k) show_kb ; exit ;;
    -m) show_mb ; exit ;;
    -*)  echo "Usage: free [-k|-m]" ; exit 1 ;;
esac

show_kb
