#! /bin/sh
#
# rc.halt	This file is executed by init when it goes into runlevel
#		0 (halt) or runlevel 6 (reboot). It kills all processes,
#		unmounts file systems and then either halts or reboots.
#
# Version:	@(#)/etc/init.d/reboot	2.00	1998-09-02
#
# Author:	Pavel Troller <patrol@sinus.cz> for the SINUX distribution
# Based upon similar script by Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org>
#

  # Set the path.
  PATH=/sbin:/usr/sbin:/bin:/usr/bin
  
  # Used in dosreboot.
  lilo=""

  # Find out how we were called.
  case "$0" in
	*halt)
		message="The system is halted..."
		command="halt -p"
		;;
	*dosreboot)
		message="Please stand by, rebooting the system to WINDOWS..."
		command="reboot -f"
		lilo="lilo -R win"
		;;
	*reboot)
		message="Please stand by while rebooting the system..."
		command=reboot
		;;
	*)
		echo "$0: call me as \"halt\" or \"reboot\" please!"
		exit 1
		;;
  esac

  # For safety, if some dummy script would try to stop us
  if [ "$1" != "start" ] ; then
    echo "$0: Usage: $0 start"
    exit 1
  fi

  # Test to see if the root partition is read-only. If it is, simplify the things.
  if echo -n >> "Testing filesystem status"; then
    rm -f "Testing filesystem status"
  
    # Kill all processes.
    echo "System `basename $0` is in progress, please wait..."
    echo "Sending all processes the TERM signal.."
    kill -15 -1
    sleep 5
    echo "Sending all processes the KILL signal.."
    kill -9 -1

    # Try to turn off quota and accounting.
    if [ -x /usr/sbin/quotaoff ]
    then
  	echo "Turning off quota..."
	/usr/sbin/quotaoff -a
    fi
    if [ -x /usr/sbin/accton ]
    then
	echo "Turning off accounting.."
	/usr/sbin/accton
    fi

    # Before unmounting file systems write a reboot record to wtmp.
    halt -w

    # If we want to reboot to the dos, tell the LILO about it.
    if [ "$lilo" != "" ] ; then
      eval $lilo
    fi

    # Turn off swap, then unmount file systems.
    echo "Turning off swap"
    swapoff -a
    sync
    sleep 3
    echo "Unmounting file systems"
    umount -a
    mount -n -o remount,ro /

  fi # Root R/W

  # Now halt or reboot.
  [ -f /etc/fastboot ] && echo "On the next boot fsck will be skipped."
  echo -n "The Final Countdown: "
  for i in 3 2 1 ; do echo -n $i.. ; sleep 1 ; done ; echo 0
  echo "$message"
  eval $command -d
