#!/bin/bash
###########################################################################
#                                                                         #
#                         Powersave Daemon                                #
#                                                                         #
#          Copyright (C) 2004,2005 SUSE Linux Products GmbH               #
#                                                                         #
# 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 you   #
# 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 St, Fifth Floor, Boston, MA 02110-1301 USA                  #
#                                                                         #
###########################################################################

# invoke acpi suspend and APM suspend to disk
#   the name of this script is a leftover from non-APM days...
# 
# return codes:
#   0 - successfully suspended
#   1 - error suspending
# 126 - unknown parameter

# ./myecho is a small'n'dirty echo replacement, that gives us the return
# code of open/write/close. Regular "echo" just gives 0 or 1.
MYECHO=${0%/*}/myecho
NOTIFY=${0%/*}/powersave-notify

S2RAM_NOT_SUPPORTED="Suspend to ram is not supported on your machine.
If you know that it should work, you might override this detection with
setting SUSPEND2RAM_FORCE=\"yes\" in /etc/powersave/sleep.
See http://en.opensuse.org/S2ram for more information.
Thanks!"

S2RAM_FB_ONLY="Suspend to RAM on this machine only works if no framebuffer is used.
Change your bootloader configuration to use text mode (vga=0) and reboot."

if [ -z "$1" ];then
    echo "This script is invoked by the powersave daemon, do not invoke from console"
    exit 126
fi

. $SYSCONF_DIR/sleep
HWCLOCK="/sbin/hwclock"
OPT="--hctosys"
# where is the s2ram binary installed?
S2RAM="/usr/sbin/s2ram"
S2RAM_LOG=/var/log/suspend2ram.log

# blank the "suspend console"
deallocvt 63

# save the old loglevel
read LOGLEVEL DUMMY < /proc/sys/kernel/printk
# set the loglevel so we see the progress bar.
# if the level is higher than needed, we leave it alone.
if [ $LOGLEVEL -lt 5 ]; then
	echo 5 > /proc/sys/kernel/printk
fi

case "$1" in
    suspend2disk)
	IMG_SZ=0
	read IMG_SZ < /sys/power/image_size	
	$MYECHO /sys/power/state disk
	RET=$?      # 28=ENOSPC, "not enough swap."
	#
	# the logic here is:
	# if image_size > 0 (without kernel support, IMG_SZ will be zero),
	# and we got ENOSPC, then try again with image_size set to zero.
	if [ $RET -eq 28 -a $IMG_SZ -ne 0 ]; then # try again with minimal image size
		echo 0 > /sys/power/image_size
		$MYECHO /sys/power/state disk
		RET=$?
		echo $IMG_SZ > /sys/power/image_size
        fi
	[ "$SUSPEND2DISK_RESTORE_CLOCK" == "yes" ] && $HWCLOCK $OPT
	;;
    suspend2ram)
	# if DISABLE_USER_SUSPEND2RAM == yes, we never get called.
	S2RAM_OPTS="";
	if [ "$SUSPEND2RAM_FORCE" == "yes" ]; then
		echo "SUSPEND2RAM_FORCE is set to yes. Good luck..." >> $S2RAM_LOG
		S2RAM_OPTS="--force"
		[ "$SUSPEND2RAM_VBE_POST" == "yes" ] && S2RAM_OPTS="$S2RAM_OPTS --vbe_post"
		[ "$SUSPEND2RAM_VBE_SAVE" == "yes" ] && S2RAM_OPTS="$S2RAM_OPTS --vbe_save"
		if [ -n "$SUSPEND2RAM_ACPI_SLEEP" ]; then
			declare -i X
			X="$SUSPEND2RAM_ACPI_SLEEP"
			S2RAM_OPTS="$S2RAM_OPTS --acpi_sleep $X"
		fi
		echo "  S2RAM_OPTS are set to: '$S2RAM_OPTS'" >> $S2RAM_LOG
	fi;

	if [ -x $S2RAM ]; then
		echo "Executing '$S2RAM $S2RAM_OPTS'" >> $S2RAM_LOG
		$S2RAM $S2RAM_OPTS >> $S2RAM_LOG 2>&1
		RET=$?
		case $RET in
			127) $NOTIFY $S2RAM_NOT_SUPPORTED & ;;
			126) $NOTIFY $S2RAM_FB_ONLY & ;;
		esac
	else
		echo "$S2RAM not found, falling back to echo." >> $S2RAM_LOG
		$MYECHO /sys/power/state mem
		RET=$?
	fi
	echo "==== finished, return code $RET ====" >> $S2RAM_LOG
	[ "$SUSPEND2RAM_RESTORE_CLOCK" == "yes" ] && $HWCLOCK $OPT
	;;
    standby)
	$MYECHO /sys/power/state standby
	RET=$?
	[ "$STANDBY_RESTORE_CLOCK" == "yes" ] && $HWCLOCK $OPT
	;;
    *)  echo "Wrong parameter in $0"
	RET=126
	;;
esac

# restore previous loglevel
echo $LOGLEVEL > /proc/sys/kernel/printk
exit $RET
