#!/bin/bash
# Script to prepare VAIO FX405 for software suspend with some extra features
# (c) 2002 Markus Gaugusch - http://gaugusch.at/vaio/
# You can re-distribute and change this screept freely!
# It is just an idea of how to do suspension!
#
# Written for usage with SuSE linux
# To be used by following activation script (started at boot in background)
#
#!/bin/bash
#while read CLASS TYPE x COUNT; do
#   case $TYPE in
#   PWRF)
#      echo "power button pressed" 
#      export DISPLAY=:0.0
#      su markus -c "rxvt -fg yellow -bg black -e /home/markus/bin/susp" ;;
#   SBTN)
#      echo "sleep button pressed" ;;
#   LID)
#      echo "lid switch pressed" ;;
#   esac 
#done < /proc/acpi/event
#

function do_exit()
{
	echo
	echo Press any key to continue
	read -n 1 x
	exit $1
}

# last resort for esound
killall -9 esd &>/dev/null
HANDLESOUND=1
HANDLEUSB=1
if [ $HANDLESOUND -eq 1 ] ; then
	if [[ `lsmod` != *snd-via82xx* ]] ; then
		rcalsasound stop
	fi
	if [[ `lsmod` == *snd-via82xx* ]] ; then
		rcalsasound stop
		if [[ `lsmod` == *snd-via82xx* ]] ; then
			echo Could not remove sound modules!
			do_exit 1
		fi
	fi
	rmmod snd-page-alloc &>/dev/null
fi
# autofs doesn't work after resume
rcautofs stop
if [ $HANDLEUSB -eq 1 ] ; then
	#rchotplug stop
	rmmod uhci
	rmmod usb-storage
	#rmmod usb-core
fi
rmmod ide-cs &>/dev/null
if [ -n "`mount | grep "type \(nfs\|smb\)"`" ] ; then
	echo "Trying to umount network filesystems ..."
	for i in `mount | grep "type \(nfs\|smb\)" | cut -d " " -f 3` ; do
		umount $i
	done
	if [ -n "`mount | grep "type \(nfs\|smb\)"`" ] ; then
		echo "Found mounted NFS or SMB filesystem, please umount!"
		exit
	fi
fi
for i in cf usb cd ; do
	if [ -n "`mount | grep misc.$i`" ] ; then
		umount /misc/$i || { echo "Umount of /misc/$i failed - exiting" ; exit ; } 
	fi
done
# eject pcmcia cards - I had problems here
cardctl eject &>/dev/null
# remove soft-modem drivers - they reset the machine if used after suspend!!
rmmod hsfserial hsfengine hsfvia hsfosspec &>/dev/null
# pcmcia de-stabilizes system after resume
#rcpcmcia stop
# stop sound daemon - sound drivers don't survive suspend as well
killall esd &>/dev/null
# sync clock to cmos
hwclock --systohc
#rmmod powernowk7 &>/dev/null
rmmod videodev &>/dev/null
rmmod ohci1394 raw1394 ieee1394 &>/dev/null
# acpid takes 100% cpu after resume, so we stop it here
#killall acpid
hdparm -u0 /dev/hda &>/dev/null
rcifplugd stop
killall ifplugd
ifconfig eth0 down
killall dhcpcd &>/dev/null
splash -s -u 62 /etc/bootsplash/markus.cfg
#echo 204 582 616 25 0xf2b700 > /proc/sys/kernel/splash_progress
echo Syncing ...
sync
echo Suspending ...
chvt 63
# suspend the machine ( 1 1 0 = reboot after suspend, 1 0 0 = power off)
reboot=0
[ "$1" == "1" ] && reboot=1
#reboot=$((reboot+2+0)) # show progress (2), beep (16)
s=/proc/swsusp
for i in $s/[b-s]* ; do
	echo 0 > $i
done
echo 1 > $s/beeping
echo $reboot > $s/reboot
echo 80 > $s/image_size_limit
echo 1 >$s/default_console_level
echo 1 >$s/enable_escape
#echo 3 2817 2048 3 > /proc/sys/kernel/swsusp
lsmod > /tmp/lastlsmod
#echo -n "0 $reboot 0 0 80" > /proc/sys/kernel/swsusp
##############################################################################
fbtruetype "Good bye ..."
shutdown -z now
########################################################################
#sleep 7 # give time to suspend
echo --------------------------------------------------------------------
echo Restoring clock ...
hwclock --hctosys
hdparm -u1 /dev/hda &>/dev/null
# restart esd for user markus
#su markus -c "esd -nobeeps &"
#usb modules
echo Renewing IP ...
rcifplugd start &
chvt 7 
splash -s -u 62
if [ $HANDLESOUND -eq 1 ] ; then
	rcalsasound start
fi
if [ $HANDLEUSB -eq 1 ] ; then
	modprobe uhci
	modprobe usb-storage
#	rchotplug start
fi
rcautofs start
cardctl insert
#rcpcmcia start

# restart hotplug service
# renew dhcp ip
# restore clock
# dhcpcd -n &
# and restart acpid
#acpid
if [[ `ip a show dev eth0` == *inet* ]] ; then
	echo Syncing with ntp server ...
	netdate ts2.univie.ac.at
else
	echo "NO NET CONNECTION!"
fi
do_exit

