#!/bin/sh
# 
# apmd_proxy - program dispatcher for APM daemon
# Craig Markwardt (craigm@lheamail.gsfc.nasa.gov) 21 May 1999
# Debian-specific version rewritten by Avery Pennarun.
#
# This shell script is called by the APM daemon (apmd) when the state
# of any power management function has changed.  The exact events that
# trigger the calling of apmd_proxy depend on how apmd was configured
# at compile time.
#
# In Debian, you should add scripts to /etc/apm/event.d (or the legacy
# /etc/apm/suspend.d and resume.d directories) rather than editing this
# script directly, unless you have a good reason.
#
# apmd_proxy is called with specific arguments that describe the event
# that has occurred.  It is this script's responsibility to query the
# hardware or the APM service (via /proc/apm) for more information,
# and to take the appropriate action.
#
# For example, apmd will call "apmd_proxy suspend system" just before
# the system is scheduled to go into suspend mode.
#
# If the APM kernel module supports it, apmd_proxy can return an error
# code for the suspend and standby events, indicating whether the
# pending mode should be rejected.  For example, apmd_proxy may decide
# if, based on CPU or network activity, a suspend should be rejected.
#
#   RETURN VALUE:
#     0 - nominal return; suspend and standby events are accepted
#     1 - reject a suspend or standby (MUST HAVE APM KERNEL SUPPORT)
#
# Here are the calling sequences for apmd_proxy:
#
# apmd_proxy start              - APM daemon has started
# apmd_proxy stop               - APM daemon is shutting down
# apmd_proxy suspend critical   - APM system indicates critical suspend (*)
# apmd_proxy suspend system     - APM system has requested suspend mode
# apmd_proxy suspend user       - User has requested suspend mode
# apmd_proxy standby system     - APM system has requested standby mode 
# apmd_proxy standby user       - User has requested standby mode
# apmd_proxy resume suspend     - System has resumed from suspend mode
# apmd_proxy resume standby     - System has resumed from standby mode
# apmd_proxy resume critical    - System has resumed from critical suspend
# apmd_proxy change battery     - APM system reported low battery
# apmd_proxy change power       - APM system reported AC/battery change
# apmd_proxy change time        - APM system reported time change (*)
# apmd_proxy change capability  - APM system reported config. change (+)
#
# (*) - APM daemon may be configured to not call these sequences
# (+) - Available if APM kernel supports it.
#
# *******************************************************************

set -e

SUSPEND_ON_AC=false
[ -r /etc/apm/apmd_proxy.conf ] && . /etc/apm/apmd_proxy.conf

if [ "${SUSPEND_ON_AC}" = "false" -a "${2}" = "system" ] \
	&& on_ac_power >/dev/null; then
    # Reject system suspends and standbys if we are on AC power
    exit 1  # Reject (NOTE kernel support must be enabled)
fi

# old-style /etc/apm/suspend.d and resume.d directories; note that these
# scripts usually don't check their command line parameters, since older
# versions of apmd didn't provide any!
#
if [ "$1" = "suspend" -a -d /etc/apm/suspend.d ]; then
    run-parts /etc/apm/suspend.d
fi
if [ "$1,$2" != "resume,suspend" -a -d /etc/apm/resume.d ]; then
    run-parts /etc/apm/resume.d
fi

# new-style event.d directory.  All new event scripts go here.
#
run-parts --arg="${1}" --arg="${2}" /etc/apm/event.d

exit 0
