#! /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	2.47 Sat Jan 13 13:37:26 PST 2001
#
# Author:	Miquel van Smoorenburg <miquels@drinkel.nl.mugnet.org>
# Modified by:  Patrick J. Volkerding, <volkerdi@slackware.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

# Detect SGI Visual Workstation, since hwclock will make those freeze up:
if fgrep -l Cobalt-APIC /proc/interrupts 1> /dev/null 2> /dev/null ; then
  echo "SGI Visual Workstation detected.  Not running hwclock."
# Save the system time to the hardware clock using hwclock
elif [ -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

# Shut down the NFS server:
if [ -x /etc/rc.d/rc.nfsd ]; then
  /etc/rc.d/rc.nfsd stop
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 NFS or SMB filesystems:
echo "Unmounting remote filesystems."
umount -a -r -t nfs,smbfs

# 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 -r -t nonfs
  echo "Remounting root filesystem read-only."
  mount -n -o remount,ro /
else
  umount -a -r -t nonfs,noumsdos,nosmbfs
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

# sleep 1 fixes problems with some hard drives that don't
# otherwise finish syncing before reboot or poweroff
sleep 1

# This is to ensure all processes have completed on SMP machines:
wait

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

