#!/bin/sh
#
#pragma ident	"@(#)i.powerconf	1.6	09/06/16 SMI"
#
# Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
#

PATH="/usr/bin:/usr/sbin:${PATH}" export PATH

write_sed_script()
{
cat > /tmp/power.sed.$$ << EOF
/^#ident.*$/d
/^# Putting an entry in this file will only be effective if the driver$/d
/^# for the device supports device power management. After the file is$/d
/^# modified, pmconfig(1M) command must be executed to activate the new$/d
/^# change.$/d
/^# Fields must be separated by white space or semicolons$/d
/^# Note that physical dependents are automatically considered$/d
/^# by the power management framework.$/d
/^# Name[ 	][ 	]*.*$/d
/\/dev\/kbd/d
/\/dev\/mouse/d
/^# NOTE: The entries below are only used when no window system is running.$/d
/^# When running the window system, monitor power management is done$/d
/^# by the screen saver functions.$/d
/^# NOTE: The entries below are only used when no windowing environment$/d
/^# NOTE: The entry below is only used when no windowing environment$/d
/^# is running.  When running windowing environment, monitor power$/d
/^# management is controlled by the Screen Saver functions.$/d
/^# management is controlled by the window system.$/d
EOF
}

while read src dest
do
	if [ ! -f $dest ] ; then
		cp $src $dest
	else
		grep '@(#)power.conf' $src > /tmp/p.$$
		write_sed_script
		sed -f /tmp/power.sed.$$ < $dest >> /tmp/p.$$
		cp -f /tmp/p.$$ $dest
		rm -f /tmp/power.sed.$$ /tmp/p.$$
		grep -w 'autopm' $dest > /dev/null 2>&1
		if [ $? != 0 ]; then
			grep -w 'autopm' $src >> $dest
		fi
		grep -w 'device-dependency-property' $dest > /dev/null 2>&1
		if [ $? != 0 ]; then
			grep -w 'device-dependency-property' $src >> $dest
		fi

		if [ `uname -p` = i386 ]; then
			grep -w 'cpupm' $dest > /dev/null 2>&1
			if [ $? != 0 ]; then
				grep -w 'cpupm' $src >> $dest

				grep -w 'cpu-threshold' $dest > /dev/null 2>&1
				if [ $? != 0 ]; then
					grep -w 'cpu-threshold' $src >> $dest
				fi
			fi
		fi
	fi
done

exit 0
