#!/bin/sh

echo "0 0 0 0">/proc/sys/kernel/printk

# save the brightness value
BRN=`cat /proc/acpi/asus/brn`

# discover video card's ID
ID=`/usr/bin/lspci | grep VGA | awk '{ print $1 }' | sed -e 's@0000:@@' -e 's@:@/@'`

# securely create a temporary file
TMP_FILE=`mktemp /tmp/video_state.XXXXXX`
trap 'rm -f $TMP_FILE' 0 1 15

# switch to virtual terminal 1 to avoid graphics
# corruption in X
chvt 1
/sbin/hwclock --systohc

# write all unwritten data (just in case)
sync

# dump current data from the video card to the
# temporary file
cat /proc/bus/pci/$ID > $TMP_FILE

# suspend
#echo -n mem > /sys/power/state
s2ram -f -p

/sbin/hwclock --hctosys

# restore video card data from the temporary file
# on resume
cat $TMP_FILE > /proc/bus/pci/$ID

# restore screen brightness
echo $BRN > /proc/acpi/asus/brn

sleep 3

chvt 7

# remove temporary file
rm -f $TMP_FILE

if [ "`cat /proc/acpi/asus/wlan`" = "1" ]; then 
export MAIN_DIALOG="
<window title=\"WLAN\">
<hbox>
<vbox>
<text>
<label>WLAN-device is on ...</label>
</text>
</vbox>
</hbox>
</window>"
{
sleep 5
killall gtkdialog
} | gtkdialog -c --program MAIN_DIALOG
else
export MAIN_DIALOG="
<window title=\"WLAN\">
<hbox>
<vbox>
<text>
<label>WLAN-device is off ...</label>
</text>
</vbox>
</hbox>
</window>"
{
sleep 5
killall gtkdialog
} | gtkdialog -c --program MAIN_DIALOG
fi
fi
exit 0
