#!/bin/bash
###############################################################################
# dwm-power-mode
#
# This program sets power mode (powersave/full).
#
# -----------------------------------------------------------------------------
# DWM Scripts - wrapper for dwm/dmenu/slock
#   (C) 2007-2009 Gerardo García Peña
#   Programmed by Gerardo García Peña
#
#   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; either version 2 of the License, or (at your option)
#   any later version.
#
#   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, write to the Free Software Foundation, Inc., 51
#   Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
#
###############################################################################

. dwm-config-read

if [ -z "$1" ]; then
  error "You must specify power/full (powersave/full speed)."
  exit 1
fi

case "$1" in
power)
    # Set the disks to aggressively save power and use the lowest acoustic
    # level.  Note: Currently Firefox is very poorly behaved and some
    # might find these settings too aggressive.  If so, change “-S 4″ to
    # something larger like -S 24 (two minutes).
    hdparm -B180 /dev/sda

    # set the LCD bright to 60%
    [ -e /proc/acpi/video/VID/LCD/brightness ] \
      && echo 60 > /proc/acpi/video/VID/LCD/brightness

    # Change the ext3 commit times to 1 minute.  This reduces disk activity
    mount -o remount,commit=60 /

    # Set laptop disk write mode
    echo 5 > /proc/sys/vm/laptop_mode

    # Manually set the iwl3945 driver to power savings.
    #echo 5 > /sys/bus/pci/drivers/iwl????/0000\:??\:00.0/power_level

    # Reduce disk activity by waiting up to 10 minutes before doing writes
    echo   90 > /proc/sys/vm/dirty_ratio
    echo    1 > /proc/sys/vm/dirty_background_ratio
    echo 6000 > /proc/sys/vm/dirty_writeback_centisecs

    # Set sound card power savings
    [ -e /sys/module/snd_hda_intel/parameters/power_save ] \
      && echo 10 > /sys/module/snd_hda_intel/parameters/power_save

    # Set SATA to minimum power
    #echo min_power > /sys/class/scsi_host/host0/link_power_management_policy
    #echo min_power > /sys/class/scsi_host/host1/link_power_management_policy
    #echo min_power > /sys/class/scsi_host/host2/link_power_management_policy
    #echo min_power > /sys/class/scsi_host/host3/link_power_management_policy
    #echo min_power > /sys/class/scsi_host/host4/link_power_management_policy

    # Make sure ondemand governor is set
    for i in /sys/devices/system/cpu/cpu?/cpufreq; do
        [ -e "$i/scaling_governor" ] && echo ondemand > "$i/scaling_governor"
    done

    # Remove the webcam driver
    modprobe -r uvcvideo       > /dev/null 2>&1
    modprobe -r sbp2           > /dev/null 2>&1
    modprobe -r ieee1394       > /dev/null 2>&1
    modprobe -r uvcvideo       > /dev/null 2>&1
    modprobe -r videodev       > /dev/null 2>&1
    modprobe -r v4l1_compat    > /dev/null 2>&1
    modprobe -r compat_ioctl32 > /dev/null 2>&1
    modprobe -r v4l2_common    > /dev/null 2>&1

;;
full)
    # Go fast on AC power.  Similar to default Ubuntu settings

    # Set the drive to mostly stay awake
    hdparm -B254 -M 254 /dev/sda

    # Remount ext3 filesystems so the journal commit only happens every 60
    # seconds. By default this is 5 but, I prefer to reduce the disk
    # activity a bit.
    mount -o remount,commit=60 /

    # Turn off the laptop mode disk optimization
    echo 0 > /proc/sys/vm/laptop_mode

    # Manually set the wifi driver to no power savings.
    #echo 6 > /sys/bus/pci/drivers/iwl????/0000\:??\:00.0/power_level

    # Set kernel dirty page value back to default
    echo 10 > /proc/sys/vm/dirty_ratio
    echo  5 > /proc/sys/vm/dirty_background_ratio

    # Only wakeup every 60 seconds to see if we need to write dirty pages
    # By default this is every 5 seconds but, I prefer 60 to reduce disk
    # activity.
    echo 6000 > /proc/sys/vm/dirty_writeback_centisecs

    # Turn off sound card power savings
    [ -e /sys/module/snd_hda_intel/parameters/power_save ] \
      && echo 0 > /sys/module/snd_hda_intel/parameters/power_save

    # Set the SATA to max performance
    #echo max_performance > /sys/class/scsi_host/host2/link_power_management_policy
    #echo max_performance > /sys/class/scsi_host/host3/link_power_management_policy
    #echo max_performance > /sys/class/scsi_host/host4/link_power_management_policy

    # Make sure ondemand governor is set
    for i in /sys/devices/system/cpu/cpu?/cpufreq; do
        [ -e "$i/scaling_governor" ] && echo performance > "$i/scaling_governor"
    done

    # Enable the webcam driver
    #modprobe uvcvideo
;;
*)
    error "Unknown mode '$1'."
    exit 1
esac

exit 0
