#!/bin/bash

###test -f /usr/sbin/laptop_mode || exit 0

# ac on/offline event handler

###/usr/sbin/laptop_mode auto

if grep -q on /proc/acpi/ac_adapter/AC/state; then
    echo "ignoring ac adapter plugged in; it can do nothing but good."
    exit
fi

if grep open /proc/acpi/button/lid/LID/state; then
    echo "ignoring loss of on-line power because laptop is probably in use.";
    exit
fi

RUNLEVEL=`/sbin/runlevel | cut -c3`
if [ "$RUNLEVEL" == "6" ]; then
    RUNLEVEL=0
fi

if [ "$RUNLEVEL" == "0" ]; then
    echo "ignoring loss of power's request to suspend; system is shutting down."
    exit
fi

# lid is closed and power is lost. laptop will sleep
echo 3 > /proc/acpi/sleep
