#!/bin/sh
# handle KEY: run the action /etc/default/acpid-s6 sets for HandleKEY
set -u

HandlePowerKey=poweroff
HandleRebootKey=reboot
HandleSuspendKey=ignore
HandleHibernateKey=ignore
HandleLidSwitch=ignore
if [ -r /etc/default/acpid-s6 ]; then
  . /etc/default/acpid-s6
fi

case "${1:-}" in
  PowerKey)     action="$HandlePowerKey" ;;
  RebootKey)    action="$HandleRebootKey" ;;
  SuspendKey)   action="$HandleSuspendKey" ;;
  HibernateKey) action="$HandleHibernateKey" ;;
  LidSwitch)    action="$HandleLidSwitch" ;;
  *) echo "handle: unknown key '${1:-}'" >&2; exit 2 ;;
esac

echo "handle: $1 -> $action"
case "$action" in
  ignore) ;;
  poweroff|reboot|halt) exec "/sbin/$action" ;;
  suspend) echo mem > /sys/power/state ;;
  hibernate) echo disk > /sys/power/state ;;
  *) echo "handle: unknown action '$action' for Handle$1" >&2; exit 1 ;;
esac
