#!/bin/sh

# refuse to sleep on USB or NFS, since there are resume problems
if grep -q " / nfs" /proc/mounts || egrep -q '^/dev/sd.. / ' /proc/mounts; then
	exit 1
fi

if [ "$#" == "1" ]; then
	TIMEOUT=$1
fi

# Kill any processes that interfere with proper sleeping
KILLPROCS='sfmike sfspeaker wmmixer aumix madplay xawtv'

echo "Checking for processes to kill..."
for i in ${KILLPROCS} ; do
	if killall $i >& /dev/null ; then
		echo -n " $i "
	fi
done
echo ""

# Kill processes that have these devices open
# Uncomment these lines if you want this functionality
#
# KILLDEVS=''
# fuser -k ${KILLDEVS}


TIMEOUT=${TIMEOUT:-0}

if uname -r |grep -q '^2\.6' ; then
	if [ -f /sys/power/sleeptime ] ; then
	    echo ${TIMEOUT} > /sys/power/sleeptime
	    echo 'mem' > /sys/power/state
	else
	    echo ${TIMEOUT} > /proc/sys/pm/suspend
	fi
else
	echo ${TIMEOUT} > /proc/sys/pm/suspend_timeout
	echo > /proc/sys/pm/suspend

	# after waking up reset the timeout 
	echo 0 > /proc/sys/pm/suspend_timeout
fi

# post resume
#/usr/local/sbin/wakeup &
