#! /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.50	1994-01-18
#
# Author:	Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#

  # Set the path.
  PATH=/sbin:/etc:/bin:/usr/bin

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

	# Init goes single user AFTER this script exits.
	init 1

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

	# Sync disks and go single user.
	# Pray no process writes to disk anymore..
	sync ; sleep 2 ; 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 `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
