#!/bin/bash
#
# Simple suspend script
#
# Copyright 2006 Red Hat, Inc.
#
# Based on work from:
#    Bill Nottingham <notting@redhat.com>
#    Peter Jones <pjones@redhat.com>
#    David Zeuthen <davidz@redhat.com>
#    Richard Hughes <richard@hughsie.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of version 2 of the GNU General Public License as
# published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

# The rule here? Simplicity.

help_options() {
	echo "$(basename "$0") [options]"
	echo
	echo "Options can change how the supend or hibernate is done."
	echo
	echo "Possible actions are:"
	echo
	echo "  --quirk-dpms-on"
	echo "  --quirk-dpms-suspend"
	echo "  --quirk-radeon-off"
	echo "  --quirk-reset-brightness"
	echo "  --quirk-s3-bios"
	echo "  --quirk-s3-mode"
	echo "  --quirk-vbe-post"
	echo "  --quirk-vbemode-restore"
	echo "  --quirk-vbestate-restore"
	echo "  --quirk-vga-mode3"
	echo
}

if [ -n "$EUID" -a "$EUID" != "0" ]; then
	echo This utility may only be run by the root user. 1>&2
	exit 1
fi

export LC_COLLATE=C

#export everything
set -a

# Get the command line options
while [ $# -gt 0 ]
do
	# for backwards compatibility, we export the quirks as both
	# "QUIRK_*" and "DISPLAY_QUIRK_*" (the old form).
	case "${1##--quirk-}" in # just quirks, please
		dpms-on) 	   QUIRK_DPMS_ON="true"
				   DISPLAY_QUIRK_DPMS_ON="true" ;;
		dpms-suspend) 	   QUIRK_DPMS_SUSPEND="true"
				   DISPLAY_QUIRK_DPMS_SUSPEND="true" ;;
		radeon-off) 	   QUIRK_RADEON_OFF="true"
				   DISPLAY_QUIRK_RADEON_OFF="true" ;;
		reset-brightness)  QUIRK_RESET_BRIGHTNESS="true"
				   DISPLAY_QUIRK_RESET_BRIGHTNESS="true" ;;
		s3-bios) 	   QUIRK_S3_BIOS="true"
				   DISPLAY_QUIRK_S3_BIOS="true" ;;
		s3-mode) 	   QUIRK_S3_MODE="true"
				   DISPLAY_QUIRK_S3_MODE="true" ;;
		vbe-post) 	   QUIRK_VBE_POST="true"
				   DISPLAY_QUIRK_VBE_POST="true" ;;
		vbemode-restore)   QUIRK_VBEMODE_RESTORE="true"
				   DISPLAY_QUIRK_VBEMODE_RESTORE="true" ;;
		vbestate-restore)  QUIRK_VBESTATE_RESTORE="true"
				   DISPLAY_QUIRK_VBESTATE_RESTORE="true" ;;
		vga-mode3) 	   QUIRK_VGA_MODE_3="true"
				   DISPLAY_QUIRK_VGA_MODE_3="true" ;;
		none) 		   QUIRK_NONE="true" ;;	# there was never a DISPLAY_QUIRK_NONE
		--help)		   help_options
				   exit 0 ;;
		*)		   echo "info: unknown option '$1' ignored."
	esac
	shift
done
set +a

. /usr/lib/pm-utils/functions

[ -f /sys/power/state ] || exit 1


ACTION=$(basename "$0")
ACTION=${ACTION#pm-}
ACTION=${ACTION/-/_}
METHOD=$ACTION

case "$ACTION" in
	suspend)
		if ! grep -q mem /sys/power/state ; then
			echo "Error: kernel cannot suspend to ram." 1>&2
			exit 1
		fi
		REVERSE=resume ;;
	hibernate)
		if ! grep -q disk /sys/power/state ; then
			echo "Error: kernel cannot suspend to disk." 1>&2
			exit 1
		fi
		REVERSE=thaw
		;;
	suspend_hybrid)
		if ! pm-is-supported --suspend-hybrid; then
			echo "Error: system cannot do hybrid suspend." 1>&2
			exit 1
		fi
		ACTION=suspend
		REVERSE=resume
		;;
	*)
		exit 1
		;;
esac

logger -i -t "pm-$METHOD" "Entering $METHOD. In case of problems, please check /var/log/pm-suspend.log"

export PM_CMDLINE="$@"

pm_main "$METHOD" "$ACTION" "$REVERSE"

exit $?
