#!/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                  #
#                                                                         #
###########################################################################

# first get helper functions (e.g. DEBUG, load_scheme, ...)
. "${0%/*}/helper_functions"
. "${PUB_SCRIPT_DIR}/sleep_helper_functions"

set_variables "suspend2disk"

## EV_ID="$4" # set in helper_functions

echo "========we are back from suspend, cleaning up.========" >> $LSMOD_LOG
# work around a kernel bug: suspend sometimes fails if swappiness < 100
swappiness=`awk -F': ' '/^swappiness:/{print $2}' ${STATE}`
[ -n "$swappiness" ] && echo $swappiness > /proc/sys/vm/swappiness

# if suspend failed or succeeded does not really matter, we restore the grub
# state to what it was before suspend.
grub-once-restore

#############################################################################
# if the system is configured for 915resolution, we first patch the BIOS,
# then force the switch back to the previous console
if [ -e /etc/default/915resolution ]; then
    . /etc/default/915resolution
    if [ "$MODE" != "auto" ] &&
      ([ "$MODE" = "" ] || [ "$XRESO" = "" ] || [ "$YRESO" = "" ]); then
	echo "===== Your 915resolution hasn't been configured! =====" >> $LSMOD_LOG
	echo "Please read /usr/share/doc/915resolution/README.Debian" >> $LSMOD_LOG
    else
        SWITCH_VT=yes
        BIOS_INIT="/etc/init.d/915resolution"
        echo "patching video bios..." >> $LSMOD_LOG
        [ -x $BIOS_INIT ] && $BIOS_INIT start >> $LSMOD_LOG
    fi
fi


restore_after_sleep "suspend2disk"

SWAPON=`awk -F': ' '/^swapon:/{print $2}' ${STATE}.resume`
if [ -n "$SWAPON" ]; then
    DEBUG "deactivating suspend partition '$SWAPON'" DIAG
    echo "deactivating suspend partition '$SWAPON'" >> $LSMOD_LOG
    swapoff $SWAPON
fi

echo "=======================================" >> $LSMOD_LOG
echo "restore_after_suspend_to_disk: finished" >> $LSMOD_LOG

# $SCRIPT_RETURN is called in restore_after_sleep.
EXIT 0
