#! /bin/sh

# $Copyright:	$
# Copyright (c) 1984, 1985, 1986, 1987, 1988, 1989, 1990 
# Sequent Computer Systems, Inc.   All rights reserved.
#  
# This software is furnished under a license and may be used
# only in accordance with the terms of that license and with the
# inclusion of the above copyright notice.   This software may not
# be provided or otherwise made available to, or used by, any
# other person.  No title to or ownership of the software is
# hereby transferred.

#	Copyright (c) 1984 AT&T
#	  All Rights Reserved

#	THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
#	The copyright notice above does not evidence any
#	actual or intended publication of such source code.

#ident	"$Header: powerfail.sh 1.8 89/10/10 $@(#)initpkg:./powerfail.sh	1.1"
if u3b2
then echo "#	powerfail
" >powerfail
elif i386
then cat <<'@' >powerfail
#!/bin/sh
# TIME is the number of minutes the UPS can guarantee power.
PATH=/bin:/usr/bin
TIME=15

WARNTIME=`expr $TIME - 1`

LIST=`who -r`
A=`expr "$LIST" : ".*run-level \(.\)"`

case $1 in
powerfail)
	if [ $A = S ]
	then
	    echo\
	    "Power failure: on standby power - shutdown in $WARNTIME minutes."
	else
	    echo\
	    "Power failure: on standby power - shutdown in $WARNTIME minutes."\
		| /etc/wall &
	fi
	# Send SIGPWR to all processes
	/etc/powerfail kill &
	kill -19 -1
	;;
powerok)
	# Kill shutdown process
	LIST=`ps -ef | grep "/etc/powerfail[ 	][ 	]*kill"\
	    | grep -v grep | sed -e "s/[a-zA-Z ]*\([0-9]*\).*/\1/"`
	kill -9 $LIST
	if [ $A = S ]
	then
	    echo "Power back on, power fail shutdown canceled."
	else
	    echo "Power back on, power fail shutdown canceled." | /etc/wall &
	fi
	;;
kill)
	# Only for use from with in the powerfail script
	sleep `expr $WARNTIME \* 60`
	if [ $A = S ]
	then
	    echo\
	    "Power failure: on standby power, shutting down, log off now."
	else
	    echo\
	    "Power failure: on standby power, shutting down, log off now."\
		| /etc/wall &
	fi
	sleep 60
	sync
	cd /
	sync
	/etc/init 0
	;;
* )
	echo "Usage: `basename $0` powerfail" 2>&1
	echo "	or `basename $0` powerok" 2>&1
	exit 1
	;;
esac
exit 0
@
fi
