#!/bin/bash

# Simple fan control for Buffalo Linkstation Pro LS-XHL using hard drive
# temperature, by teknohog at iki.fi

# Requires the sim_buffalo kernel module to access /proc/buffalo with custom
# kernels, from
# http://tyche.pu-toyama.ac.jp/~a-urasim/lsxhl/kernel-memo.html

# Limits 40 and 50 are usually recommended. IMHO the fast speed sounds less
# annoying, having more spread-out noise than a whine, so a more aggressive
# control is better. This can be achieved by t_low > t_high.

TEMP_LOW=42
TEMP_HIGH=50
FANCTL=/proc/buffalo/gpio/fan/control
INTERVAL=60

while true; do
	# In case of smartctl error, default to slow fan
	TEMP=`smartctl /dev/sda --all | awk '$1 == "194" {print $10}'` || TEMP=$TEMP_HIGH

	if [ $TEMP -lt $TEMP_LOW ]; then
		echo stop > $FANCTL
	elif [ $TEMP -gt $TEMP_HIGH ]; then
		echo fast > $FANCTL
	else
		echo slow > $FANCTL
	fi
	
	sleep $INTERVAL
done
