#!/bin/bash
#
# Copyright 2008-2009 Daisuke SUZUKI <daisuke@linux.or.jp>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# setup tuxonice configuration

. "${PM_FUNCTIONS}"
[ -f "/etc/sysconfig/tuxonice" ] && . "/etc/sysconfig/tuxonice"

TOI_ROOT=/sys/power/tuxonice
TOI_UI_ROOT=/sys/power/tuxonice/user_interface

if [ ! -d ${TOI_ROOT} ]; then
  exit 0;
fi

set_toi_image_size_limit()
{
    if [ "x$TOI_IMAGE_SIZE_LIMIT" = "xnocache" ] ; then
        local field val units result
        local total=0 cached=0 buffers=0 free=0 swaptotal=0 swapfree=0
        while read field val units ; do
            [ "x$field" = "xMemTotal:" ] && total=$val
            [ "x$field" = "xMemFree:" ] && free=$val
            [ "x$field" = "xBuffers:" ] && buffers=$val
            [ "x$field" = "xCached:" ] && cached=$val
            [ "x$field" = "xSwapTotal:" ] && swaptotal=$val
            [ "x$field" = "xSwapFree:" ] && swapfree=$val
        done < /proc/meminfo
        result=$((($total-$cached-$free+$swaptotal-$swapfree+10240)/1024))
        echo $result > $TOI_ROOT/image_size_limit
    else
        [ -n "$TOI_IMAGE_SIZE_LIMIT" ] && echo $TOI_IMAGE_SIZE_LIMIT > $TOI_ROOT/image_size_limit
    fi
}

set_toi_enable_escape()
{
    [ -n "$TOI_ESCAPE" ] && [ -f $TOI_UI_ROOT/enable_escape ] && \
    echo $TOI_ESCAPE > $TOI_UI_ROOT/enable_escape
}   

case "$1" in
	hibernate)
		set_toi_image_size_limit
		set_toi_enable_escape
		;;
	*)
		;;
esac

exit 0
