#!/bin/bash

. /etc/rc.d/init.d/functions

# default values go here
HIBERNATE_RESUME_POST_VIDEO=no
SUSPEND_MODULES=""

[ -f /etc/pm/config ] && . /etc/pm/config

# export them all here
export HIBERNATE_RESUME_POST_VIDEO
export SUSPEND_MODULES

GLOBAL_CONFIG_VARIABLES="HIBERNATE_RESUME_POST_VIDEO SUSPEND_MODULES"

source_configs()
{
	for cfg in $(ls -1 /etc/pm/config.d/* 2>/dev/null) ; do
		STR=". $cfg"
		for v in $GLOBAL_CONFIG_VARIABLES ; do
			STR="$STR ; echo x_$v=\"\$(eval echo \$$v)\""
		done
		eval $(bash -c "$STR")
		for v in $GLOBAL_CONFIG_VARIABLES ; do
			eval $v="$(eval echo $(eval echo \$$v) \$x_$v)"
		done
	done
}

source_configs

get_video_type()
{
	vendor=""
	vendor=$(/sbin/lspci -mn|awk '{if ($2 ~ /^"0300"$/ ) {print $3;exit;}}')
	vendor=$(eval echo $vendor)
	[ -z "$vendor" ] && return 1
	case "$vendor" in
		1002)
			echo ATI
			return 0
			;;
		10de)
			echo nVidia
			return 0
			;;
		8086)
			echo Intel
			return 0
			;;
		*)
			echo $vendor
			return 0
			;;
	esac
}

get_lcd_status()
{
	return 0
}

lcd_on()
{
	return 0
}

lcd_off()
{
	return 0
}

get_crt_status()
{
	return 0
}

crt_on()
{
	return 0
}

crt_off()
{
	return 0
}

suspend_video()
{
	return 0;
}

resume_video()
{
	return 0;
}

rotate_video_state()
{
    STATUS="$(get_crt_status) $(get_lcd_status)"
    case "$STATUS" in
    "off off")
        crt_off
        lcd_on
        ;;
    "off on")
        crt_on
        lcd_on
        ;;
    "on on")
        crt_on
        lcd_off
        ;;
    "on off")
        crt_off
        lcd_off
        ;;
    *)
        ;;
    esac
}

take_suspend_lock()
{
	VT=$(/usr/bin/fgconsole)
	chvt 63
	if [ -f /.suspended ]; then
		pid=$(cat /.suspended)
		if [ -d /proc/$pid ]; then
			return 1
		fi
	fi
        echo "$$" > /.suspended
	rm -f /var/run/pm-suspend
	touch /var/run/pm-suspend
	return 0
}

remove_suspend_lock()
{
	rm -f /var/run/pm-suspend
	chvt 1
	chvt $VT
	openvt -- sh -c "usleep $1 ; rm -f /.suspended >/dev/null 2>&1 0<&1" >/dev/null 2>&1 0<&1 &
}

run_hooks()
{
	[ -z "$1" ] && return 0

	[ -f /var/run/pm-suspend ] && . /var/run/pm-suspend
	rm -f /var/run/pm-suspend

	files="/etc/pm/hooks/*"
	if [ "$2" = "reverse" ]; then
		files=$(echo $files | awk '{ for (i=NF; i>=1; i--) print $i }')
	fi
	for file in $files ; do
		[ -x $file ] && $file $1
	done
}

get_power_status()
{
	RETVAL=0
	on_ac_power
	case "$?" in
		"0")
			echo "ac"
			;;
		"1")
			echo "battery"
			;;
		"255")
			echo "error"
			RETVAL=1
			;;
	esac
	return $RETVAL
}

pm_main()
{
	take_suspend_lock || exit 1
	run_hooks "$1"
	sync ; sync ; sync

	case "$1" in
		suspend)
			pm-pmu --suspend || echo -n "mem" > /sys/power/state
			run_hooks resume reverse
			;;
		hibernate)
			echo -n "platform" > /sys/power/disk
			echo -n "disk" > /sys/power/state
			run_hooks thaw reverse
			;;
	esac

	remove_suspend_lock 200

	return 0
}

modunload()
{
	/sbin/lsmod 2>/dev/null | grep -q "$1"
	if [ "$?" == "0" ]; then
		echo "export ${1}_MODULE_LOAD=yes" >> /var/run/pm-suspend
		/sbin/modprobe -r "$1" >/dev/null 2>&1
	fi
}

modreload()
{
	if [ "$(eval echo \$${1}_MODULE_LOAD)" == "yes" ]; then
		/sbin/modprobe "$1" >/dev/null 2>&1
	fi
}

stopservice()
{
	/sbin/service "$1" status 2>/dev/null | grep -c -q running
	if [ "$?" == "0" -a -x "/etc/init.d/$1" ]; then
		echo "export ${1}_SERVICE_ACTIVATE=yes" >> /var/run/pm-suspend
		"/etc/init.d/$1" stop >/dev/null 2>&1
	fi
}

restartservice()
{
	if [ "x$(eval echo \$${1}_SERVICE_ACTIVATE)" == "xyes" \
			-a -x "/etc/init.d/$1" ]; then
		"/etc/init.d/$1" start >/dev/null 2>&1
	fi
}

savestate()
{
	echo "export ${1}_STATE=$2" >> /var/run/pm-suspend
}

restorestate()
{
	eval echo \$${1}_STATE
}
