#!/bin/bash
# -----------------------------------------------------------------------------
# $Id: pwrctl,v 1.1.1.1 2001/12/07 11:31:53 sleemburg Exp $
#
# This script is invoked by pmud to configure the system for a
# given power level. The desired level is indicated by the first
# argument and can take the following values:
#
# minimum = minimum power
# medium  = medium power 
# maximum = full power  
# sleep   = prepare for sleep
# wakeup  = system woke up after a sleep
# warning = low battery condition detected, issue a warning to users
#
# the second argument gives the current power source, and can take the
# following values:
#
# ac
# battery
#
# This script is invoked when the AC power is connected or disconnected, 
# and also immediately after sleep. If the script /etc/power/pwrctl-local
# is present and executable, it will be called by this script before the
# main body of this script is executed. If pwrctl-local returns 1, then
# the main body of this script is NOT executed, in all other cases the
# main body of this script will be executed.
#
# Note that if you leave pwrctl-local writable by others than root (which
# should be the owner) you have created a serious security hole!
#
# You can edit this file, but it's better to edit /etc/power/pwrctl-local
# as that file will not be overwritten on upgrades.
# -----------------------------------------------------------------------------
# $Log: pwrctl,v $
# Revision 1.1.1.1  2001/12/07 11:31:53  sleemburg
# Initial CVS import of the unreleased pmud-0.8 to apmud (new project name
# because of a name clash at sourceforge.net).
#
# Revision 1.6  2000/12/12 08:56:57  stephan
# support for iBook and Pismo (same as other G3's)
#
# Revision 1.5  2000/10/09 14:33:40  stephan
# wakebay added
#
# Revision 1.4  2000/05/11 14:54:45  stephan
# pmud 0.6 changes
#
# Revision 1.3  2000/03/25 21:26:32  stephan
# pmud-0.5 changes
#
# Revision 1.2  2000/03/09 13:01:50  stephan
# formatting and call to pwrctl-local
#
# Revision 1.1  2000/01/06 13:48:19  stephan
# Initial revision
# -----------------------------------------------------------------------------
logger=/usr/bin/logger
localfun=/etc/power/pwrctl-local

function do_warn()
{
	local msg="Low battery, system will go down..."

	(
	/usr/X11R6/bin/xmessage -center -timeout 15 "$msg" || \
	/usr/bin/wall "$msg"
	) &
}

function pwrctl_G3() 
{
	case "$1" in
	minimum)
	    	# min power, set disk to spin down after 1 minute
    		hdparm -p -S 12 /dev/hda
	;;
	medium)
    		hdparm -p -S 12 /dev/hda
    	;;
	maximum)
		case "$2" in
		ac)
			# on mains, do not spin down
    			hdparm -p -S 0 /dev/hda
		;;
		*)
    			# on battery, set disk to spin down after 5 minute
    			hdparm -p -S 60 /dev/hda
		;;
		esac
    	;;
	warning)
		do_warn
	;;
	lid-closed)
    	;;
	lid-opened)
    	;;
	sleep)
    	;;
	wakeup)
		[ -f /proc/sys/dev/cdrom/info ] && {
			device=$(cat /proc/sys/dev/cdrom/info | (
				IFS=":"
				while read var val
				do
					[ "$var" = "drive name" ] && {
						echo $val
						break
					}
				done
				))
				[ ! -z "$device" ] && {
					/sbin/wakebay /dev/${device}
				}
		}
	;;
	*)
		$logger -p daemon.error -t pwrctl "$0: invalid arg $1"
	;;
	esac
}

# -----------------------------------------------------------------------------
# On the 3400, for minimum power, we put the CPU into nap mode
# (rather than doze mode) when it is idle.  This reduces power
# consumption but means that DMA is no longer cache coherent.
# Therefore we have to disable DMA, including the ethernet.
# We also turn the ethernet off during sleep.
# -----------------------------------------------------------------------------

function pwrctl_3400() 
{
	case "$1" in
	minimum)
 	   	ifconfig eth0 down
    		hdparm -d0 -S 12 /dev/hda
    		hdparm -d0 /dev/hdc
    		echo 1 >/proc/sys/kernel/powersave-nap
    	;;
	medium)
    		echo 0 >/proc/sys/kernel/powersave-nap
    		hdparm -d1 -p -S 12 /dev/hda
    		hdparm -d1 /dev/hdc
    		ifconfig eth0 up
    	;;
	maximum)
    		echo 0 >/proc/sys/kernel/powersave-nap
		case "$2" in
		ac)
			# on mains, do not spin down
    			hdparm -d1 -p -S 0 /dev/hda
		;;
		*)
    			# on battery, set disk to spin down after 5 minute
    			hdparm -d1 -p -S 60 /dev/hda
		;;
		esac
    		hdparm -d1 /dev/hdc
    		ifconfig eth0 up
    	;;
	warning)
		do_warn
	;;
	sleep)
    		ifconfig eth0 down
    	;;
	wakeup)
    		ifconfig eth0 up
	;;
	*)
		$logger -p daemon.error -t pwrctl "$0: invalid arg $1"
	;;
	esac
}

# -----------------------------------------------------------------------------
# main
# -----------------------------------------------------------------------------

[ -x $localfun ] && {
	$logger -p daemon.info -t pwrctl "calling $localfun $*"

	$localfun $*

	case $? in
	0)
		$logger -p daemon.debug -t pwrctl "continuing with main"
	;;
	1)
		$logger -p daemon.debug -t pwrctl "skipping main"
		exit 0
	;;
	*)
		$logger -p daemon.error -t pwrctl "error in $localfun"
	;;
	esac
}

case "$PMUVERSION" in
9)	pwrctl_3400 $1 $2 ;;
1[012])	pwrctl_G3   $1 $2 ;;
*) 
	$logger -p daemon.error -t pwrctl "no function for PMU $PMUVERSION"
;;
esac >>/var/log/pwrctls 2>&1

exit 0
