#!/bin/bash
#
# Copyright 2006-2007 Richard Hughes <richard@hughsie.com>
# Copyright 2007 Peter Jones <pjones@redhat.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.

. /usr/share/pm-utils/functions

vbetool=$(type -p vbetool)
vbe() {
	if [ -z "$vbetool" ]; then
		echo "vbetool not found" 1>&2
		return 1
	fi
	$vbetool "$@"
}

radeontool=$(type -p radeontool)
radeon() {
	if [ -z "$radeontool" ]; then
		echo "radeontool not found" 1>&2
		return 1
	fi
	$radeontool "$@"
}

getproperty() {
	property=$1
	hal-get-property --udi /org/freedesktop/Hal/devices/computer --key $property 2> /dev/null
}

resume_video()
{
	if [ "x${NO_AUTO_QUIRKS}" = "x" ]; then
		DISPLAY_QUIRK_RADEON_OFF=`getproperty "power_management.quirk.radeon_off"`
		DISPLAY_QUIRK_VBE_POST=`getproperty "power_management.quirk.vbe_post"`
		DISPLAY_QUIRK_VBESTATE_RESTORE=`getproperty "power_management.quirk.vbestate_restore"`
		DISPLAY_QUIRK_VBEMODE_RESTORE=`getproperty "power_management.quirk.vbemode_restore"`
		DISPLAY_QUIRK_DPMS_ON=`getproperty "power_management.quirk.dpms_on"`
	fi

	if [ "${DISPLAY_QUIRK_RADEON_OFF}" == "true" ]; then
		radeon dac on
		radeon light on
	fi
	# We might need to do one or many of these quirks
	if [ "${DISPLAY_QUIRK_VBE_POST}" == "true" ]; then
		vbe post </dev/tty0
		usleep 100000
	fi
	if [ "${DISPLAY_QUIRK_VBESTATE_RESTORE}" == "true" ]; then
		vbe vbestate restore < /var/run/vbestate
	fi
	if [ "${DISPLAY_QUIRK_VBEMODE_RESTORE}" == "true" ]; then
		vbe vbemode set `cat /var/run/vbemode`
	fi
	if [ "${DISPLAY_QUIRK_DPMS_ON}" == "true" ]; then
		vbe dpms on
	fi
}


case "$1" in
	resume)
		resume_video
		;;
	thaw)
		if [ "x$HIBERNATE_RESUME_POST_VIDEO" == "xyes" ]; then
			resume_video
		fi
		;;
esac

exit $?
