#!/bin/bash
#
# /etc/rc.shutdown - system shutdown script
#

# Load configuration
if [ -f /etc/rc.d/rc.conf ]; then
  . /etc/rc.d/rc.conf
fi

/bin/stty onlcr

echo "Shutting down system..."

if [ "$PREVLEVEL" = "2" ]; then
	echo -n "Shutting down services:"
	for service in $SERVICES; do
		echo -n " $service"
		/usr/sbin/sstop $service
	done
	echo
fi

echo "Unmounting remote filesystems."
/bin/umount -a -O _netdev

# Terminate all processes
/sbin/killall5 -15
/usr/bin/sleep 5
/sbin/killall5 -9

# Save random seed
/bin/dd if=/dev/urandom of=/var/lib/urandom/seed count=1 2> /dev/null

# Save system clock
if [ -x /sbin/hwclock ]; then
	echo "Saving system time..."
	if [ "$HWCLOCK" = "UTC" ]; then
		/sbin/hwclock --utc --systohc
	else
		/sbin/hwclock --localtime --systohc
	fi
fi

/sbin/halt -w

echo "Turning off swap."
/sbin/swapoff -a

echo "Unmounting local filesystems."
/bin/umount -a -r

echo "Remouting root filesystem read-only."
/bin/mount -n -o remount,ro /

# Power off or reboot
if [ "$RUNLEVEL" = "0" ]; then
	/sbin/poweroff -d -f -i
else
	echo "Rebooting ROOT..."
	/sbin/reboot -d -f -i
fi

# EOF
