#!/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_begin_msg()
{
	[ -z "$*" ] || printf '%s ' "$*"
}

action_end_msg()
{
	local rc=$1
	if [ "$BOOTUP" = color ]; then
		[ $rc = 0 ] && echo_success || echo_failure
	fi
	echo
}

action_passed_msg()
{
	[ "$BOOTUP" != color ] ||
		echo_passed
	echo
}

action()
{
	action_begin_msg "$1"
	shift
	$*
	local rc=$?
	action_end_msg "$rc"
	return $rc
}

sendsigs()
{
	action_begin_msg 'Asking all remaining processes to terminate'
	killall5 -15
	local rc=$?
	if [ "$rc" = 2 ]; then
		action_passed_msg
		return
	else
		action_end_msg "$rc"
	fi

	local seq=5
	if [ "$rc" = 0 ]; then
		for seq in 1 2 3 4 5; do
			killall5 -18 || break
			sleep 1
		done
	fi

	[ "$seq" = 5 ] || return 0
	action_begin_msg 'Killing all remaining processes'
	killall5 -9
	rc=$?
	if [ "$rc" = 2 ]; then
		action_passed_msg
	else
		action_end_msg "$rc"
	fi
}

# 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
sendsigs

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

ACCTOFF=/sbin/accton
[ -x "$ACCTOFF" ] && "$ACCTOFF" off

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

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

# 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

# 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 != "/" && $2 != "/dev" && $3 != "proc" && $3 != "loopfs" && !($1 ~ /^none/) {print $2}' \
	'Unmounting filesystem' \
	'Unmounting filesystem (retry)'

# Turn off LVM.
/etc/rc.d/scripts/lvm_stop

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

# Turn off multipath devices.
/etc/rc.d/scripts/multipath_stop

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

# 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 /

# 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 -d -f -i
