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

. /etc/init.d/functions

runcmd() {
   gprintf "%s " $1
   shift
   if [ "$BOOTUP" = "color" ]; then
      $* && echo_success || echo_failure
   else
      $*
   fi
   echo
}

# If we are in Aurora, let it show a nice msg telling the user that
# the system is halted...
if [ -x /etc/aurora/Monitor ] && /etc/aurora/Monitor query; then
    . /lib/aurora/functions
    # See how we were called.
    case "$0" in
    *halt)
	chvt 1
        ;;
    *reboot)
	echo "Please stand by while rebooting the system..."
	# Just make sure Aurora have time enough to display it before we kill
	# Aurora!
	sleep 1
        ;;
    esac
    # Reset output from Aurora...
    exec 0<&3
    exec 1>&4
    exec 2>&5
fi

# See how we were called.
case "$0" in
  *halt)
	message="Das System ist heruntergefahren"
	command="halt"
	;;
  *reboot)
	message="Bitte einen Moment Geduld. Neustart wird initialisiert..."
	command="reboot"
	;;
  *)
	gprintf "%s: call me as 'rc.halt' or 'rc.reboot' please!\n" $0
	exit 1
	;;
esac
if [ -n "$1" ]; then
 case "$1" in
   *start)
   	;;
   *)
   	gprintf "Benutzung: (halt|reboot) {start}\n"
	exit 1
	;;
 esac
fi

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

runcmd "Sende allen Prozessen das TERM Signal..." /sbin/killall5 -15
sleep 5
runcmd "Sende allen Prozessen das KILL Signal..."  /sbin/killall5 -9

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

# Sync the system clock.
ARC=0
SRM=0
UTC=0

if [ -f /etc/sysconfig/clock ]; then
   . /etc/sysconfig/clock

   # convert old style clock config to new values
   if [ "${CLOCKMODE}" = "GMT" ]; then
      UTC=true
   elif [ "${CLOCKMODE}" = "ARC" ]; then
      ARC=true
   fi
fi

CLOCKDEF=""
CLOCKFLAGS="--systohc"

case "$UTC" in
   yes|true)
    CLOCKFLAGS="$CLOCKFLAGS -u";
    CLOCKDEF="$CLOCKDEF (utc)";
   ;;
   no|false)
    CLOCKFLAGS="$CLOCKFLAGS --localtime";
    CLOCKDEF="$CLOCKDEF (localtime)";
   ;;
esac

case "$ARC" in
     yes|true)
     	CLOCKFLAGS="$CLOCKFLAGS -A";
     	CLOCKDEF="$CLOCKDEF (arc)";
     ;;
esac
case "$SRM" in
     yes|true)
	CLOCKFLAGS="$CLOCKFLAGS -S";
	CLOCKDEF="$CLOCKDEF (srm)";
     ;;
esac

runcmd "Syncronisiere Hardware-Uhr mit der System-Zeit" /sbin/hwclock $CLOCKFLAGS

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

[ -x /sbin/accton ] && runcmd "Deaktiviere Accounting: " /sbin/accton

[ -x /sbin/quotaoff ] && runcmd "Deaktiviere Quotas: " /sbin/quotaoff -a

# first remove entry /initrd/loopfs as it can't be unmounted :(
mtab=$(fgrep -v "/initrd/loopfs[^/]" /etc/mtab)
(IFS= ; echo $mtab > /etc/mtab)

# Unmount file systems, killing processes if we have to.
sig=
retry=3
remaining=`awk '!/(^#|proc|loopfs|autofs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
while [ -n "$remaining" -a "$retry" -gt 0 ]
do
	if [ "$retry" -lt 3 ]; then
		LANG=C runcmd "Unmounte Dateisysteme (wiederholt): "  umount -a -f -t noproc
	else
		LANG=C runcmd "Unmounte Dateisysteme: "  umount -a -f -t noproc
	fi
	sleep 2
	remaining=`awk '!/(^#|proc|loopfs|autofs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
	[ -z "$remaining" ] && break
	/sbin/fuser -k -m $sig $remaining >/dev/null
	sleep 5
	retry=$(($retry-1))
	sig=-9
done

# turn off raid
if [ -x /sbin/raidstop -a -f /etc/raidtab ]; then
    # we can not use raidstop -a here because this will only stop
    # devices listed in the default config file which is not always
    # the case. So we look only for the active raid devices
    if [ -f /proc/mdstat ] ; then
	mddevs=$(grep ^md /proc/mdstat | awk '{ print $1 }')
	for mddev in $mddevs ; do
	    runcmd "Deaktiviere RAID fr $mddev: " raidstop /dev/$mddev
	done
        unset mddev mddevs
    fi
    #runcmd "Turning off RAID: " /sbin/raidstop -a
fi

[ -f /proc/bus/usb/devices ] && umount /proc/bus/usb

# Remount read only anything that's left mounted.
#gprintf "Remounting remaining filesystems (if any) readonly\n"
mount | awk '/ext2|reiserfs/ { print $3 }' | while read line; do
    mount -n -o ro,remount $line
done

runcmd "Unmounte proc-Dateisystem: " umount /proc


# See if this is a powerfail situation.
if [ -f /etc/powerfail ]; then
  gprintf "APCUPSD will now power off the UPS!\n"
  echo
  /etc/apcupsd/apccontrol killpower
  echo
  gprintf "Please ensure that the UPS has powered off before rebooting\n"
  gprintf "Otherwise, the UPS may cut the power during the reboot!!!\n"
  echo
  sleep 120
  exit 1
fi

# Now halt or reboot.
gprintf "%s\n" $message
if [ -f /fastboot ]; then
 gprintf "On the next boot fsck will be skipped.\n"
elif [ -f /forcefsck ]; then
 gprintf "On the next boot fsck will be forced.\n"
fi

HALTARGS="-i -d -p"
if [ -f /halt ]; then
    HALTARGS="-i -d"
fi

eval $command $HALTARGS
