#!/bin/sh
# Run when power fails or is restored.

# See what happened.
PATH=/sbin:/bin:/usr/sbin:/usr/bin
case "$1" in
    start)
	# Tell everybody.
	/usr/bin/wall "THE POWER IS DOWN! SHUTTING DOWN SYSTEM!"

	if [ -x /etc/init.d/nfs ];then
		/etc/init.d/nfs stop
	fi

	# Don't allow users to login.
	echo "POWER FAILURE" > /etc/nologin

	# Tell init to go single user.
	trap "" SIGTERM
	init -t5 S

	# Sync disks and go single user.
	# Pray no process writes to disk anymore..
	sync; sleep 4; sync

	exit 0
	;;
    stop)
	# Ok, power is good again. Say so on the console.
	echo "The power is back; going multi-user."

	# If we're not single user don't try to restore.
	[ $RUNLEVEL != S ] && exit 0

	# Go back to previous runlevel.
	init $PREVLEVEL 2> /dev/null

	# And allow users to log in.
	rm -f /etc/nologin
	;;
    *)
	echo "Usage: $0 {start|stop}"
	exit 1
	;;
esac
exit 0
