#!/bin/sh
# Default acpi script that takes an entry for all actions
# Paul Kinsler
# 200510xx much hacking about from Slack 10.2 original version

# makes / a separator inside "" marks... watch out!
IFS=${IFS}/
set $@
IFS=${IFS%/}

XLOGGER=logger
XLOGGER=echo
XFILESUSPENDED=/var/tmp/suspended

# ${XLOGGER} "pk ACPI :: $1 : $2 : $3 : $4 : $5  :: ${XFILESUSPENDED}"

case "$1" in
  button)
    case "$2" in
      power) 
          if [ ! -f "${XFILESUSPENDED}" ] ; then
            #/sbin/init 0
            /etc/rc.d/rc.hibernate sleep disk
            mv /var/tmp/BP /var/tmp/BP.$(date +%F)
            echo "$* $XSTATE $(date)" > ${XFILESUSPENDED}
            echo shutdown > /sys/power/disk
            echo disk     > /sys/power/state
            # The above should get me a recovery to THIS point; 

            # run this manually until I check where it should be auto-run from
            # note it rmmod/modprobe button so that the button events 
            # (post suspend) dont get mysteriously lost.
            #
            /etc/rc.d/rc.hibernate wake disk
            mv -f ${XFILESUSPENDED} ${XFILESUSPENDED}.power
          else
            echo acpi-handler: power button, but ${XFILESUSPENDED} exists
            # /etc/rc.d/rc.hibernate wake disk
            mv -f ${XFILESUSPENDED} ${XFILESUSPENDED}.power
          fi
         ;;
      lid)
        XSTATE=$(cat /proc/acpi/button/lid/LID/state | awk '{print$2}')
        XBROKEN=yes
          case $XSTATE in 
          open)
            ${XLOGGER} "ACPI button lid $XSTATE (count $5)"
            if [ -f "${XFILESUSPENDED}" ] ; then
              mv -f ${XFILESUSPENDED} ${XFILESUSPENDED}.lid
              /etc/rc.d/rc.hibernate wake memory
              #if [ "${XBROKEN}" = "no" ] ; then
              #  # restore any modules removed before suspend (battery)
              #  if [ -f ${XFILESUSPENDED}.rmmod ] ; then
              #    for II in $(cat ${XFILESUSPENDED}.rmmod)
              #    do 
              #      /sbin/modprobe $II
              #    done
              #  fi
              #  # I'm not sure whether this should be here...
              #  /usr/sbin/855resolution 38 1280 768
              #fi
            fi
            ;;
          closed)
            ${XLOGGER} "ACPI button lid $XSTATE (count $5)"
            /etc/rc.d/rc.hibernate sleep memory
            # if [ "${XBROKEN}" = "no" ] ; then
            #  rm -f ${XFILESUSPENDED}.rmmod
            #  for II in battery
            #  do 
            #    /sbin/rmmod $II # in case it breaks the suspend
            #    echo ${II} >> ${XFILESUSPENDED}.rmmod
            #  done
            #  sleep 1 # paranoia
            #fi
            echo "$* $XSTATE $(date)" > ${XFILESUSPENDED}
            if [ "${XBROKEN}" = "no" ] ; then
              echo -n "mem" > /sys/power/state
            fi
            ;;
         esac
         ;;
      *) ${XLOGGER} "ACPI button $2 is not defined"
         ;;
    esac
    ;;
  ac_adapter)
    if [ "$4" = "00000000" ] ; then
      ${XLOGGER} "ACPI ac_adapter $2 is disconnected: $4"
    elif [ "$4" = "00000001" ] ; then
      ${XLOGGER} "ACPI ac_adapter $2 is (re)connected: $4"
    else
      ${XLOGGER} "ACPI ac_adapter with $4 is unknown"
    fi
    ;;
  processor)
    if [ "$3-$4" = "00000080-00000000" ] ; then
      ${XLOGGER} "ACPI processor $2 alert: $3 $4"
    else
      ${XLOGGER} "ACPI processor $2 mystery: $3 $4"
    fi
    ;;
  battery)
    ${XLOGGER} "ACPI battery $2 $3 $4" 
    ;;
  *)
    ${XLOGGER} "ACPI group $1 / action $2 is not defined"
    ;;
esac
