Laptop waking up in bag: go back to sleep script

I have a Debian-derived distro (Kubuntu 8.04) running on a Dell Inspiron laptop. This laptop often wakes from RAM suspension while in my bag. I would like to run a script, upon RAM suspension wake, that would detect if the lid switch is closed. If so, then the script should put the machine back in RAM suspension. Something like this:

wait 5 seconds
if (lid==open) {
  go to ram suspension
}
return

I do not know how to poll the lid switch, nor do I know how to suspend to RAM from within a script. I have googled but cannot find this. Can someone point me to a RTFM where TFM shows how to do these two actions? Note that other than a short C course in university and a smithering of PHP I really know nothing about computers. I will ask on my distro's mailing list how to have the script run at wake. Thanks in advance.