#!/bin/bash
#
# chkconfig: 0 0 99
# description: This deals with final shutdown in power out situations
# config: /etc/ups/

# Red Hat provides no appropriate call out for this functionality,
# so we just reproduce the whole of the halt script, along with
# the call out to the UPS powerdown functionality.
# Since the UPS powerdown would mostly duplicate whats in the ups script,
# the extra code has been added to that script - this just
# calls that appropriately

# Believe it or not, the way these things are called actually depends
# on whether a grep from the rc script finds certain magic words
# within this script.  One of those magic words is daemon and so this
# comment makes sure that its called in the appropriate fashion :-)

POWERDOWNFLAG=/etc/killpower

# Check that we really are doing a shutdown - which is a start
# for this script
if [ "$1" != "start" ]
then
    exit 0
fi


if [ ! -f $POWERDOWNFLAG ] 
then
    # just let the shutdown continue normally
    echo "Powerdown sequence is not required"
    exit 0
fi

### set up some stuff thats going to be incorrect otherwise
message="The system is halted"
command="halt"
	
### --- included standard halt script
#!/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/rc.d/init.d/functions

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

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

runcmd "Sending all processes the TERM signal..." /sbin/killall5 -15
sleep 5
runcmd "Sending all processes the KILL signal.."  /sbin/killall5 -9

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

# Save mixer settings, here for lack of a better place.
grep -q "\(sparcaudio\|sound\)" /proc/devices
if [ $? = 0 -a -x /bin/aumix-minimal ]; then
   runcmd "Saving mixer settings" /bin/aumix-minimal -f /etc/.aumixrc -S
fi

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

[ -x /sbin/accton ] && runcmd "Turning off accounting" /sbin/accton

[ -x /sbin/quotaoff ] && runcmd "Turning off quotas" /sbin/quotaoff -a

# Unmount file systems, killing processes if we have to.
sig=
retry=3
remaining=`awk '!/(^#|proc|loopfs|^none|^\/dev\/root| \/ )/ {print $2}' /proc/mounts`
while [ -n "$remaining" -a "$retry" -gt 0 ]
do
	if [ "$retry" -lt 3 ]; then
		runcmd "Unmounting file systems (retry)"  umount -a -f -t noproc
	else
		runcmd "Unmounting file systems"  umount -a -f -t noproc
	fi
	sleep 2
	remaining=`awk '!/(^#|proc|loopfs|^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

mount -n -o remount,ro /

# 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 "Turning off RAID for $mddev" raidstop /dev/$mddev
	done
        unset mddev mddevs
    fi
    #runcmd "Turning off RAID" /sbin/raidstop -a
fi

runcmd "Unmounting proc file system" umount /proc

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

### Here is the callout to force the UPS to powerdown
/etc/rc.d/init.d/upsd powerdown

# Now halt or reboot.
echo "$message"
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
eval $command -i -d -p
