#!/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.
#
# Author:       Miquel van Smoorenburg, <miquels@drinkel.nl.mugnet.org>
#               Modified for RHS Linux by Damien Neil
#

export NOLOCALE=1

WITHOUT_RC_COMPAT=1

# Source function library.
. /etc/init.d/functions

unset TEXTDOMAIN TEXTDOMAINDIR

unset action
action()
{
	echo -n "$1 "
	shift
	$*
	local rc=$?
	if [ "$BOOTUP" = color ]; then
		[ $rc = 0 ] && echo_success || echo_failure
	fi
	echo -e '\r'
	return $rc
}

# See how we were called.
case "$0" in
	*halt|*poweroff)
		message="The system is halted"
		command=/sbin/poweroff
		;;
	*reboot)
		message="Please stand by while rebooting the system..."
		command=/sbin/reboot
		;;
	*)
		msg_usage "${0##*/}: call me as \"halt\", \"reboot\" or \"poweroff\" please!"
		exit 1
		;;
esac

if [ -n "$1" ]; then
	case "$1" in
		*start)
			;;
		*)
			msg_usage "(halt|reboot|poweroff) {start}"
			exit 1
			;;
	esac
fi

# Kill all processes.
[ "${BASH+bash}" != bash ] || enable kill

action "Sending all processes the TERM signal..." /sbin/killall5 -15

sleep 5
action "Sending all processes the KILL signal..."  /sbin/killall5 -9

# Write to wtmp file before unmounting /var
halt -w

# Sync clock
/etc/init.d/clock stop

# Unmount non-/dev tmpfs.
UnmountFilesystems 3 5 \
	'$2 != "/dev" && $3 == "tmpfs" {print $2}' \
	"Unmounting tmpfs filesystem" \
	"Unmounting tmpfs filesystem (retry)"

# Turn off swap, then unmount file systems.
SWAPS=`awk '! /^Filename/ { print $1 }' /proc/swaps`
[ -n "$SWAPS" ] && action "Turning off swap:" swapoff $SWAPS

QUOTAOFF=/sbin/quotaoff
[ -x "$QUOTAOFF" ] && action "Turning off quotas:" "$QUOTAOFF" -a

ACCTOFF=/sbin/accton
[ -x "$ACCTOFF" ] && action "Turning off accounting:" "$ACCTOFF"

# Unmount supermount and autofs*.
UnmountFilesystems 3 5 \
	'$2 != "/" && (($3 == "supermount") || ($3 == "autofs") || ($3 == "autofs4")) {print $2}' \
	"Unmounting automount filesystem" \
	"Unmounting automount filesystem (retry)"

# Unmount loopback stuff first.
UnmountFilesystems 3 5 \
	'$2 != "/" && $1 ~ /^\/dev\/loop/ {print $2}' \
	"Unmounting loopback filesystem" \
	"Unmounting loopback filesystem (retry)"

# Unmount all the rest.
UnmountFilesystems 3 5 \
	'$2 != "/" && $3 != "proc" && $3 != "devfs" && $3 != "loopfs" && !($1 ~ /^none/) {print $2}' \
	"Unmounting filesystem" \
	"Unmounting filesystem (retry)"

# Turn off lvm.
/etc/rc.d/scripts/lvmstop

# Turn off raid.
/etc/rc.d/scripts/raidstop

[ -f /proc/bus/usb/devices ] && umount -n /proc/bus/usb >/dev/null 2>&1

# Remount read-only anything that's left mounted.
action "Remounting remaining filesystems (if any) read-only:" umount -anrf
action "Remounting root filesystem read-only:" mount -n -o remount,ro /

action "Unmounting proc filesystem:" umount -n /proc

# See if this is a powerfail situation.
UPSCTL=/etc/apcupsd/apccontrol
if [ -x "$UPSCTL" -a -f /etc/apcupsd/powerfail ]; then
	action "Attempting to turn the UPS off:" "$UPSCTL" killpower
	message="The system is halted"
	command=/sbin/poweroff
fi

UPSCTL=/sbin/upsdrvctl
if [ -x "$UPSCTL" -a -f /etc/killpower ]; then
	action "Attempting to turn the UPS off:" "$UPSCTL" shutdown
	message="The system is halted"
	command=/sbin/poweroff
fi

if [ "$command" = /sbin/poweroff ]; then
	# Modprobe apm for automatic poweroff at ATX cases.
	/sbin/modprobe apm >/dev/null 2>&1
fi

if [ -f /fastboot ]; then
	echo "On the next boot fsck will be skipped."
elif [ -f /forcefsck ]; then
	echo "On the next boot fsck will be forced."
fi

# Now halt or reboot.
echo "$message"
$command -i -d
