#! /bin/sh
#
# rc.6		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/rc.d/rc.6	1.50	1994-01-15
#
# Author:	Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org>
# Modified by:  Patrick J. Volkerding, <volkerdi@ftp.cdrom.com>
#

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

  # If there are SystemV init scripts for this runlevel, run them.
  if [ -x /etc/rc.d/rc.sysvinit ]; then
    . /etc/rc.d/rc.sysvinit
  fi

  # Set linefeed mode to avoid staircase effect.
  stty onlcr

  echo "Running shutdown script $0:"

  # Find out how we were called.
  case "$0" in
	*0)
		command="halt"
		;;
	*6)
		command=reboot
		;;
	*)
		echo "$0: call me as \"rc.0\" or \"rc.6\" please!"
		exit 1
		;;
  esac

  # Save the system time to the hardware clock using hwclock
  if [ -x /sbin/hwclock ]; then
    echo "Saving the system time to the hardware clock..."
    if [ -e /etc/hardwareclock ]; then
      HC=`cat /etc/hardwareclock | while read LINE ; do
        if [ "$LINE" = "UTC" -o "$LINE" = "utc" ]; then
          echo utc
          break;
        elif [ "$LINE" = "localtime" ]; then
          echo localtime
          break;
        fi
      done`
    else
      # Default to localtime if there is no /etc/hardwareclock:
      HC=localtime
    fi
    # /sbin/hwclock --$HC --systohc
    # The line above *should* work with --localtime, but doesn't.  Omitting
    # the argument defaults it to localtime, so use that.
    if [ "$HC" = "utc" ]; then
      /sbin/hwclock --utc --systohc
    else
      /sbin/hwclock --systohc
    fi
  fi

  # Kill all processes.
  # INIT is supposed to handle this entirely now, but this didn't always
  # work correctly without this second pass at killing off the processes.
  # Since INIT already notified the user that processes were being killed,
  # we'll avoid echoing this info this time around.
  if [ "$1" != "fast" ]; then # shutdown did not already kill all processes
    killall5 -15 
    sleep 5
    killall5 -9
  fi

  # Try to turn off quota:
  if fgrep quota /etc/fstab 1> /dev/null 2> /dev/null ; then
    if [ -x /sbin/quotaoff ]; then
      echo "Turning off filesystem quotas."
      /sbin/quotaoff -a
    fi
  fi

  # Turn off accounting:
  if [ -x /sbin/accton ]; then
	echo "Turning off accounting."
	/sbin/accton
  fi

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

  # Carry a random seed between reboots.
  # Save 512 bytes, which is the size of the entropy pool.
  echo "Saving random seed from /dev/urandom in /etc/random-seed..."
  dd if=/dev/urandom of=/etc/random-seed count=1 bs=512 2> /dev/null

  # Unmount any remote filesystems:
  echo "Unmounting remote filesystems."
  umount -a -tnfs

  # Turn off swap, then unmount local file systems.
  echo "Turning off swap."
  swapoff -a
  echo "Unmounting local file systems."
  # Don't remount UMSDOS root volumes:
  if [ ! "`mount | head -1 | cut -d ' ' -f 5`" = "umsdos" ]; then
    umount -a -tnonfs
    echo "Remounting root filesystem read-only."
    mount -n -o remount,ro /
  else
    umount -a -tnonfs -tnoumsdos
  fi
  # This never hurts:
  sync

  # Shut down RAID devices:
  if [ -r /etc/mdtab ]; then
    echo "Stopping RAID devices."
    /sbin/mdstop -a
  fi

  # See if this is a powerfail situation.
  if [ -f /etc/power_is_failing ]; then
    echo "Turning off UPS, bye."
    /sbin/powerd -q
    exit 1
  fi

  # Now halt (poweroff with APM kernels) or reboot.
  if [ "$command" = "reboot" ]; then
    echo "Rebooting."
    reboot -f
  else
    halt -f -p
  fi

