#! /bin/sh
# Copyright (c) 1996-2002 SuSE Linux AG, Nuernberg, Germany.
# All rights reserved.
#
# Author: Florian La Roche, 1996
#	  Werner Fink <werner@suse.de>, 1996-2001
#
# Please send feedback to http://www.suse.de/feedback
#
# /etc/init.d/halt (and symlinked to reboot)
#

. /etc/rc.status
. /etc/sysconfig/clock
. /etc/sysconfig/suseconfig

#
# Avoid being interrupted by child or keyboard
#
trap "echo" SIGINT SIGSEGV SIGQUIT SIGTERM
set +e

case "$0" in
	*halt)
		message="The system will be halted immediately."
		case `/bin/uname -m` in
                    i?86)
                        command="halt"
                        if test -e /proc/apm -o -e /proc/acpi -o -e /proc/sys/acpi ; then
                            command="halt -p"
                        else
                            read cmdline < /proc/cmdline
                            case "$cmdline" in
                                *apm=smp-power-off*|*apm=power-off*)  command="halt -p" ;;
                            esac
                        fi
                        ;;
                    *)
                        command="halt -p"
                        ;;
                esac
		;;
	*reboot)
		message="Please stand by while rebooting the system..."
		command="reboot"
		;;
	*)
		echo "$0: call me as \"halt\" or \"reboot\" please!"
		exit 1
		;;
esac

#
# first do local stuff
#
rc_reset
test -e /etc/init.d/halt.local && {
    echo Running /etc/init.d/halt.local
    /bin/sh /etc/init.d/halt.local
    rc_status -v1 -r
}

# Write to wtmp file before unmounting /var
$command -w 

# Set a flag that we had success upto this point
> /success

# Stop blogd before umounting /var
test -x /sbin/blogd && killproc -QUIT /sbin/blogd

echo "Sending all processes the TERM signal..."
killall5 -15
echo -e "$rc_done_up"

sleep 1
test "$1" = "fast" -o -e /fastboot || sleep 4

echo "Sending all processes the KILL signal..."
killall5 -9
echo -e "$rc_done_up"

# Redirect our mesages to default console
test -n "$REDIRECT" && exec 0<> $REDIRECT 1>&0 2>&0

#
# call modules in boot.d via K* symlinks
# (reverse sequence)
#

if test -d /etc/init.d/boot.d/ ; then
    for i in /etc/init.d/boot.d/K*; do
        test -f $i || continue
        if test -x "$i" ; then
            # Active boot scripts, should have their own messages
            $i stop
        else
            # Old boot scripts, may not have any message
            echo Skipping $i, not executable
        fi
    done
fi

# kill splash animation
test "$SPLASH" = yes && /sbin/splash -q

umount -anvt proc

# on umsdos fs this would lead to an error message, so direct errors to
# /dev/null
mount -no remount,ro / 2> /dev/null
sync

# show "the system is down" message
if test "$SPLASH" = yes ; then
    mount -nt proc proc /proc
    /sbin/splash -q -p 65535 -t "isdown"
    umount -n /proc
fi

# Make reboot noise and wait a few seconds to give harddisks the chance
# to really flush their buffers before power goes down.
if test -n "$REDIRECT" ; then
    sleep 1
    case "$HALT_SOUND" in
    "yes"|"quint")
	echo -en "\033[10;393]\a\033[10;262]"
	sleep 1
	;;
    "octave")
	for tone in 524 491 440 393 349 328 295 ; do
	    echo -en "\033[10;${tone}]\a"
	    usleep 125000
	done
	echo -en "\033[10;262]"
	;;
    "no")
	;;
    *)
    	echo -en "\033[10;440]\a"
	usleep 250000
	;;
    esac
else
    test "$HALT_SOUND" = "no" || echo -en "\a"
    sleep 2
fi
echo $message
test "$HALT_SOUND" = "no" || echo -en "\a"

# Set system console back to intial value
test -n "$REDIRECT" -a -n "$CONSOLE" && setconsole $REDIRECT < $CONSOLE

# Now talk to kernel
exec $command -d -f
