#!/bin/sh
# hibernate
# It is used to hibernate the system at mobline software stack run at 
# crown-beach platform.
#
# Copyright 2007 Xu Martin <martin.xu@intel.com>
#
# This file 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 your 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
#

#lock acpid
touch /var/lock/acpid

#creat the symbol file.
#note: if only one kernel exists at moblin, 
#and never update the kernel mannual 
#the file only need to creat one time. 
#That may save about 1 second at shutdown
[ -e /fast-resume-symbol ] && rm -f /fast-resume-symbol
/usr/sbin/grub-create-symbol /proc/kallsyms /fast-resume-symbol 
if [ "$?"  -ne 0 ]
then
#normal hibernate
	rm -f /fast-resume-symbol
	/usr/sbin/pm-hibernate
	rm /var/lock/acpid
	exit 0
fi

#enable acpi_enable work round at grub
echo "M" >> /fast-resume-symbol

/sbin/swapon -s | grep sda3 > /dev/null 2>&1 || /sbin/swapon /dev/sda3 > /dev/null 2>&1

#shrink the slab and cached memory
echo 2 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/drop_caches
sleep 1
echo 2 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/drop_caches
sleep 1
echo 2 > /proc/sys/vm/drop_caches
echo 1 > /proc/sys/vm/drop_caches
sleep 1
echo 0 > /proc/sys/vm/drop_caches
echo 0 > /proc/sys/kernel/printk

#enable USB persist
find /sys -name persist | grep usb | xargs -I target -n 1 /bin/sh 'echo -n 1 > target'

/usr/sbin/pm-hibernate

#unlock acpid
rm /var/lock/acpid

