#!/bin/sh
# power - start/stop either acpi or apm daemon
#
# chkconfig: 345 64 10
# description:  Listen and dispatch power events from the kernel (APM or ACPI)
# config: /etc/sysconfig/acpi
#		
# Do not load RH compatibility interface.
WITHOUT_RC_COMPAT=1

CONFIG=/etc/sysconfig/acpi

# Source function library.
. /etc/init.d/functions

RETVAL=0

load_modules()
{
	# ripped from /etc/rc.d/scripts/load_modules
	[ -s "$CONFIG" ] || return
	(cat "$CONFIG"; echo) | while read module args; do
		# Ignore empty lines and comments.
		[ -n "${module##\#*}" ] || continue
		action "Loading module (power) $module:" \
			modprobe "$module" $args 2>/dev/null
	done
}

select_service()
{
	# prefer ACPI, fallback to APM
	[ -d /proc/acpi -a -x /usr/sbin/acpid ] && echo acpid && return
	[ -f /etc/sysconfig/sysconfig/apmd -a -x /usr/sbin/apmd ] && \
		echo apmd && return
	exit 0
}

start()
{
	load_modules
	pass start
}

pass()
{
	service `select_service` $1
	RETVAL=$?
	return $RETVAL
}

# See how we were called.
case "$1" in
	start)
		start
		;;
	stop|reload|restart|condstop|condrestart|condreload|status)
		pass "$1"
		;;
	*)
		msg_usage "${0##*/} {start|stop|reload|restart|condstop|condrestart|condreload|status}"
		RETVAL=1
esac

exit $RETVAL
