#!/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/lib/pm-utils/functions


reset_brightness() {
	for bl in /sys/class/backlight/* ; do
		[ -d $bl ] || continue
		[ -f $bl/brightness ] || continue
		BR=$(cat $bl/brightness)
		echo 0 > $bl/brightness
		echo $BR > $bl/brightness
	done
}

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 "$@"
}

resume_video()
{
	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
		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
	if [ "${DISPLAY_QUIRK_RESET_BRIGHTNESS}" == "true" ]; then
		reset_brightness
	fi
}


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

exit $?
