#!/bin/sh
# /etc/acpi/suspend.sh
# Suspend (via swsusp)

# -----------------------------------------------------------------------------
VMWARE=
if /usr/bin/killall -q -0 vmware; then
    VMWARE=yes
    killall -STOP vmware
fi

##ESDU=`ps axuw | fgrep esd | fgrep /usr/bin | awk '{print $1}'`
ESDPROC=`ls -l /proc/[1-9]*/exe | fgrep /usr/bin/esd`
ESDU=`echo $ESDPROC | cut -d' ' -f3`
su $ESDU -c "esdctl off"
#sleep 1
killall -STOP esd
#
BCM5700=
if fgrep -q bcm5700 /proc/modules; then
    /etc/init.d/laptop-net stop
    killall -9 ifd
    ifconfig eth0 down
    rmmod bcm5700
    BCM5700=yes
fi
#
DRIVERLOADER=
if fgrep -q driverloader /proc/modules; then
    ifdown --force eth1
    ifconfig eth1 down
    rmmod driverloader
    DRIVERLOADER=yes
fi
#
for HC in 0 1 2; do
  for DV in 0 1 2 3 4 5 6 7; do
    echo "scsi remove-single-device $HC $DV 0 0" > /proc/scsi/scsi
  done
done
rmmod usb-storage
#sleep 1
#rmmod bcm5700
hwclock -w -u
sync
sleep 1
#echo 4 > /proc/acpi/sleep
echo > /proc/swsusp/activate
# -----------------------------------------------------------------------------
hwclock -s -u

modprobe usb-storage
modprobe bcm5700
killall -CONT esd
su $ESDU -c "esdctl on"
/etc/init.d/laptop-net start

if [ -n "$DRIVERLOADER" ]; then
    modprobe driverloader
    iwconfig eth1 rate auto mode ad-hoc nick khruzes essid khasa
#   iwconfig eth1 mode ad-hoc nick khruzes rate auto
fi

if [ -n "$VMWARE" ]; then
    sleep 10
    killall -CONT vmware
fi &

# -----------------------------------------------------------------------------

exit 0

# == UNUSED ===================================================================

#DISPLAY=:0.0 xset dpms force off
#echo -n 1 > /proc/acpi/sleep

chvt 9
exec > /dev/tty9 2>&1
clear

if /usr/bin/killall -q -0 vmware; then
    killall -HUP vmware
    sleep 3
    killall -9 vmware
fi

PCMCIA=no
if /usr/bin/killall -q -0 cardmgr; then
    PCMCIA=yes
    /etc/init.d/pcmcia stop
fi


##/sbin/ifconfig eth1 down && /sbin/cardctl eject
/etc/init.d/laptop-net stop
killall -9 ifd
##/etc/init.d/pcmcia stop
##/etc/init.d/hotplug stop
##kill 2>/dev/null `ps ax | fgrep hotplug | awk '{print $1}'` && sleep 4

##ifdown --force eth0
rmmod bcm5700
rmmod usb-uhci
rmmod uhci
rmmod ehci-hcd
rmmod lp
rmmod parport_pc
rmmod parport

cat /etc/swsusp.conf > /proc/sys/kernel/swsusp

#echo "========= modules ==============================================="
#lsmod
lsmod > /tmp/swsusp.$$
#echo "========= mtrr =================================================="
#cat /proc/mtrr
cat /proc/mtrr >> /tmp/swsusp.$$
#echo "========= ready to suspend ========== CR to proceed ============="
#read < /dev/tty9
#echo "========= ready to suspend ========== 5 sec timeout ============="
#sync
#sleep 5

sync
hwclock -w -u
echo go > /proc/swsusp/activate
hwclock -s -u

modprobe lp
modprobe ehci-hcd
modprobe usb-uhci
modprobe bcm5700
#modprobe mousedev
#modprobe usbmouse
##ifup eth0

##/etc/init.d/hotplug start
[ $PCMCIA=yes ] && /etc/init.d/pcmcia start
/etc/init.d/laptop-net start

chvt 5

#/etc/init.d/pcmcia stop
#/etc/init.d/alsa force-stop
#exec /usr/local/sbin/hibernate
