#! /bin/sh
# Copyright (c) 2002 SuSE GmbH Fuerth, Germany.  All rights reserved.
#
# Author: Bodo Bauer <bb@suse.de>
#         Christian Zoz <zoz@suse.de>
#
# /etc/init.d/apmd
#
### BEGIN INIT INFO
# Provides:       apmd
# Required-Start: $remote_fs $syslog
# Required-Stop:  $remote_fs $syslog
# Default-Start:  2 3 5
# Default-Stop:
# Description:    APM-Daemon
### END INIT INFO

. /etc/rc.status
. /etc/sysconfig/powermanagement

# Check the following variables to be numerical
TMP=$APMD_WARN_LEVEL; declare -i APMD_WARN_LEVEL=$TMP
TMP=$APMD_WARN_STEP; declare -i APMD_WARN_STEP=$TMP
TMP=$APMD_CHECK_TIME; declare -i APMD_CHECK_TIME=$TMP

# The echo return value for success (defined in /etc/rc.status).
return=$rc_done
case "$1" in
    start)
        echo -n "Starting apmd"
        if [ $APMD_WARN_LEVEL -gt 0 -a $APMD_WARN_LEVEL -lt 100 ] ; then 
            OPTS=" -w $APMD_WARN_LEVEL"
            test "$APMD_WARN_ALL" = "yes" && OPTS="$OPTS -W"
            test $APMD_WARN_STEP -gt 0 -a $APMD_WARN_STEP -lt 100 \
              && OPTS="$OPTS -p $APMD_WARN_STEP"
            test $APMD_CHECK_TIME -gt 0 && OPTS="$OPTS -c $APMD_CHECK_TIME"
        else
            OPTS=" -q"
        fi
        test "$APMD_DEBUG" != "no" && OPTS="$OPTS -v"
        test -x /usr/sbin/apmd_proxy && OPTS="$OPTS -P /usr/sbin/apmd_proxy"
	if [ -e /proc/apm ] || /sbin/modprobe -vs apm ; then
            echo -n " ($OPTS) "
            startproc /usr/sbin/apmd $OPTS
	    rc_status -v
	else
	    echo -n ": no APM support in kernel"
	    rc_status -s
            rc_failed 7
	fi
        ;;
    stop)
	echo -n "Shutting down apmd"
        killproc -TERM /usr/sbin/apmd
        rc_status -v
        ;;
    try-restart)
        $0 status >/dev/null &&  $0 restart
        # Remember status and be quiet
        rc_status
        ;;
    restart)
        ## Stop the service and regardless of whether it was
        ## running or not, start it again.
	$0 stop
	$0 start
        # Remember status and be quiet
        rc_status
	;;
#    reload|force-reload)
#	rc_failed 3
#        rc_status -v
#        ;;
    status)
        # Status has a slightly different for the status command:
        # 0 - service running
        # 1 - service dead, but /var/run/  pid  file exists
        # 2 - service dead, but /var/lock/ lock file exists
        # 3 - service not running

        echo -n "Checking for apmd: "
        # NOTE: checkproc returns LSB compliant status values.
        checkproc /usr/sbin/apmd
        rc_status -v
        ;;
    *)
        echo "Usage: $0 {start|stop|restart|status}"
	rc_failed 3
	rc_status -v
esac
rc_exit
