#!/bin/bash

# PowerManagement Control Script
# This script will be called on certain events from a
# powermanagement daemon with two arguments. The first
# argument contains the command and the second the
# currently active power source.
#
# arg $1: powersave     minimum power consumption
#         performance   maximum performance
#         sleep         prepare for sleep
#         wakeup        recovering from sleep
#
# arg $2: ac            running on ac power
#         battery       running on battery
#
# Options for this scripts will be kept in the central
# configuration file ../config. Variables for each script
# will be introduced by a special unique prefix.

# name        : bdflush
# prefix      : BDFLUSH_
# author      : 
# description : toggle laptop mode in kernel and adjust
#               memory management buffer flush timeouts
# requirements: laptop-mode patch in kernel
# limitations : none 
#
# --- end of public part -- don't change below this line ---

PATH=/bin:/sbin:/usr/bin:/usr/sbin

# source configuration
. config

# kernel default dirty buffer age
DEF_AGE=30
DEF_UPDATE=5

if [ -w /proc/sys/vm/laptop_mode ]; then
  case "$1" in
    powersave)
      AGE=$(($BDFLUSH_freq*$BDFLUSH_maxage))
      echo "1" > /proc/sys/vm/laptop_mode
      echo "30 500 0 0 $AGE $AGE 60 20 0" > /proc/sys/vm/bdflush
      ;;
    performance)
      U_AGE=$(($BDFLUSH_freq*$DEF_UPDATE))
      B_AGE=$(($BDFLUSH_freq*$DEF_AGE))
      echo "0" > /proc/sys/vm/laptop_mode
      echo "30 500 0 0 $U_AGE $B_AGE 60 20 0" > /proc/sys/vm/bdflush
      ;;
    sleep)
      ;;
    wakeup)
      ;;
  esac
fi

exit 0

