#!/bin/zsh
# Filename:      samsung_s4
# Purpose:       Suspend-to-Disk (STD, ACPI S4) script for Samsung X20 laptop
# Authors:       grml-team (grml.org), (c) Michael Prokop <mika@grml.org>
# Bug-Reports:   see http://grml.org/bugs/
# License:       This file is licensed under the GPL v2.
# Latest change: Mit Nov 01 11:08:48 CET 2006 [mika]
################################################################################

. /etc/grml/lsb-functions

if [ $UID != 0 ] ; then
   eerror Error: become root before starting $0 >& 2 ; eend 1
   exit 100
fi

FG_CONSOLE=$(fgconsole)
VBETOOL=''
XID=$(pidof X)
TTY=$(tty)

if [ -n "$XID" ] ; then
  if [ -z "$XUSER" ] ; then
    XUSER=$(awk 'BEGIN{RS="\\000";FS="="}($1 == "USER"){print $2}' < /proc/$XID/environ)
    if [ -z "$XUSER" ] ; then # we are running something like [xgk]dm, ugly workaround:
      XUSER=$(who | grep ':0' | awk '{print $1}' | sort -u)
      ewarn "X-User not identified. Assuming user $XUSER is running X." ; eend 0
      ewarn "If you want to override uid run \"XUSER=your_user ${0}\"." ; eend 0
      # XUSER=$(getent passwd 1000 | cut -d: -f1)
    fi
  fi
fi

if [[ $(tty) == /dev/tty* ]] ; then # we are running on console
  CONSOLE=1
fi

if grep -q 'resume=' /proc/cmdline ; then
  # suspend
  einfo "grml - going to standby (S4)..."
  echo "[" `date` "] Go to standby [S4]" >> /var/log/acpi_events
  touch /tmp/lidbtn
  chvt 1
  hwclock --systohc
  echo disk > /sys/power/state

  # wakeup
  einfo "grml - resuming from standby (S4)..."
  echo "[" `date` "] Wakeup from standby [S4]" >> /var/log/acpi_events
  hwclock --hctosys
  vbetool post
  if [ -x /etc/init.d/915resolution ] ; then
     /etc/init.d/915resolution start
  elif [ -x /etc/init.d/855resolution ] ; then
     /etc/init.d/855resolution start
  fi

  # change back to original terminal
  chvt $FG_CONSOLE

  # lock screen
  if [ -z "$NOLOCK" ] ; then
    if [ -n "$XID" ] ; then # lock X
      su $XUSER -c "xlock -mode blank"
    fi

    if [ -n "$CONSOLE" ] ; then # lock console
      CONSOLEUSER=$(ls -l $TTY | awk '{print $3}')
      su - "$CONSOLEUSER" -c "TERM=linux tput clear; vlock -a"
    fi
  fi
else
  eerror "Error: no resume=/dev/... parameter in /proc/cmdline found."
  eerror "Please specify the resume parameter in your bootloader (lilo/grub)."
  eerror "Otherwise suspending will not be possible."
  eerror "Exiting."
fi

[ -n "$VBETOOL" ] && rm -f $VBETOOL_FILE

## END OF FILE #################################################################
