#! /bin/sh
#
# powerfail	This script is run when the UPS tells the system
#		the power has gone. Tell everybody, sync the disks
#		and drop into single user mode as fast as possible.
#		This script is also being run when the power comes
#		up again (if it does in time!)
#
# Version:	@(#) /etc/powerfail 1.01 26-Oct-1993
#
# Author:	Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

  # Source function library.
  . /etc/init.d/functions

  # See what happened.
  case "$1" in
    start)
	# Tell everybody.
	wall "THE POWER IS DOWN! SHUTTING DOWN SYSTEM!"

	if [ -f /etc/init.d/nfs ]
	then
		# Stop NFS services.
		echo "Stopping NFS services.."
		/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
	/sbin/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.
	set `/sbin/runlevel` > /dev/null
	if [ "$1" != S ]
	then
		exit 0
	fi

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

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

  exit 0
