#!/bin/sh -e
# enable SATA power link management in powersave mode if we have enough RAM
# Author: Martin Pitt <martin.pitt@ubuntu.com>

# check available memory, since this is painful for low-memory systems which
# have to swap often
read _ memtotal _ < /proc/meminfo
if [ "`uname -m`" = "x86_64" ] && [ "$memtotal" -lt 2000000 ]; then
   echo "Less than 2 GB of RAM, not enabling SATA power link management" >&2
   exit 0
fi
if [ "$memtotal" -lt 1000000 ]; then
   echo "Less than 1 GB of RAM, not enabling SATA power link management" >&2
   exit 0
fi

# see kernel/Documentation/scsi/link_power_management_policy.txt
if [ "$1" = true ]; then
    v=min_power
else
    v=max_performance
fi

for p in /sys/class/scsi_host/*/link_power_management_policy ; do
    [ -f "$p" ] && echo $v > "$p"
done

