#! /bin/zsh
# /usr/local/sbin/power-save.sh Michael Weber 20120211
# PHC Undervolting and PowerTop1 hints.
# thanks https://wiki.archlinux.org/index.php/Powertop

put() {
  val=$1
  shift
  for i in "$@" ; do
    [ -r "$i" ] || { echo skip $i ; continue }
    if [ "$(cat "$i")" != "$val" ] ; then
      echo -e " \e[1;32m*\e[m $val -> $i"
      echo -n $val > $i
    fi
  done
}

modprobe phc_intel
rfkill block bluetooth
#ethtool -s eth0 wol d

if [ -e /sys/devices/system/cpu/cpu0/cpufreq/phc_controls ] ; then
	put "12:36 11:36 8:16 6:6 136:0" /sys/devices/system/cpu/cpu*/cpufreq/phc_controls
else
	put "36 36 16 6 0" /sys/devices/system/cpu/cpu*/cpufreq/phc_vids
fi

put 3 /sys/devices/system/machinecheck/machinecheck*/tolerant

put 1 /sys/module/snd_hda_intel/parameters/power_save
put ondemand /sys/devices/system/cpu/*/cpufreq/scaling_governor
put 5 /proc/sys/vm/laptop_mode
put auto /sys/devices/*/power/control
put auto /sys/bus/usb/devices/*/power/level
put 2 /sys/bus/usb/devices/*/power/autosuspend
put 1500 /proc/sys/vm/dirty_writeback_centisecs

#powertop 2.2
put auto /sys/bus/pci/devices/*/power/control
put min_power /sys/devices/pci0000:00/0000:00:1f.2/ata?/host?/scsi_host/host?/link_power_management_policy

put 0 /proc/sys/kernel/dmesg_restrict

#put noop /sys/block/*/queue/scheduler
