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

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

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

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

echo "Sending all processes the TERM signal..."
kill -15 -1
sleep 5
echo "Sending all processes the KILL signal.."
kill -9 -1

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

# Turn off swap
echo "Turning off swap"
swapoff -a

#### start of ppdd modification  1  ####

REPLY=1
if [ -f /sbin/ppddmchk ]; then
	ppddmchk / /dev/ppdd0
	REPLY=$?
fi
if [ $REPLY = 0 ]; then
    swapoff /swap/file
    echo "Remounting ppdd file systems in read only mode"
    mount | awk '/ppdd[^0]/ { print $1 }' | while read line; do
	echo remount $line
        mount -n -o ro,remount $line
    done
    echo "Unmounting and checksum ppdd file systems"
    mount | awk '/ppdd[^0]/ { print $1 }' | while read line; do
	echo umount $line
	sleep 1
	umount -r $line
	if [ -f /initrd/csum ]; then
	    if [ -f /initrd/check/$line ]; then
	        echo checksum $line
	        ppddsetup -dc $line
	        sleep 2
	    else
	        ppddsetup -d $line
	    fi
        else
	    ppddsetup -d $line
	fi
    done
fi
sleep 3

####  end of ppdd modification  1  ####

# Unmount other file systems.
echo "Unmounting file systems"
umount -a
mount -n -o remount,ro /

echo "Remounting remaining filesystems (if any) readonly"
mount | awk '/ext2/ { print $3 }' | while read line; do
    mount -n -o ro,remount $line
done

#### start of ppdd modification  2  ####

if [ $REPLY = 0 ]; then
    if [ -f /initrd/csum ]; then
        if [ -f /initrd/block0 ]; then
	    if [ -f /initrd/check/dev/ppdd0 ]; then
                echo checksum /dev/ppdd0
                ppddcsum -rg /dev/ppdd0
                sleep 2
            fi
        fi
    fi
    ppddsetup -f /dev/ppdd0
fi
sleep 5

#### end of ppdd modification  2  ####

# Now halt or reboot.
echo "$message"
[ -f /fastboot ] && echo "On the next boot fsck will be skipped."
eval $command -d

