#!/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 subdirectories of /etc/apm
# rather than edit this script directly, unless you have a good reason.
#
# If the order in which a set of scripts are run is important, put
# the scripts in /etc/apm/scripts.d and symlink to these from
# /etc/apm/suspend.d, /etc/apm/resume.d and/or /etc/apm/other.d
# using symlink names that result in the appropriate order for
# suspend events, resume-from-suspend events, and other events,
# respectively.  Scripts in /etc/apm/event.d are run for every event
# and are told the event type in the first and second arguments.
#
# 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

# The following doesn't yet work, because current kernels (up to
# 2.4.18) do not support rejection of APM events.  Supporting this
# will require changes both to the kernel and to apmlib.  We will
# re-enable this when that has been done.
# -- cph@debian.org
#
#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

if [ "$1" = "suspend" ]; then
    if [ -d /etc/apm/suspend.d ]; then
        run-parts --arg="${1}" --arg="${2}" /etc/apm/suspend.d
    fi
elif [ "$1" = "resume" -a "$2" = "suspend" ]; then
    if [ -d /etc/apm/resume.d ]; then
        run-parts --arg="${1}" --arg="${2}" /etc/apm/resume.d
    fi
else
    if [ -d /etc/apm/other.d ]; then
        run-parts --arg="${1}" --arg="${2}" /etc/apm/other.d
    fi
fi

run-parts --arg="${1}" --arg="${2}" /etc/apm/event.d

exit 0
