#! /bin/sh
# Copyright (c) 1996 S.u.S.E. GmbH Fuerth, Germany.  All rights reserved.
#
# Author: Florian La Roche <florian@suse.de>, 1996
#	  Werner Fink <werner@suse.de>, 1996
#
#	Updated for "apcupsd"
#	  Andre M. Hedrick <hedrick@astro.dyer.vanderbilt.edu>, 1997
#
# /sbin/init.d/halt (and symlinked to reboot)
#

#. /etc/rc.config

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 \"halt\" or \"reboot\" please!"
		exit 1
		;;
esac

#check HWM FPGA shutdown state
sysshutdown="halt"
if [ -e /tmp/shutdown_to_sleep ]; then
     sysshutdown="sleep"
     rm -f /tmp/shutdown_to_sleep
fi


#Trun off RAID LEDs
if [ "$command" = "halt" ]; then
    #Check color of HDD-1 LED 0 and Trun it off
    /nn/bin/raidled_util 0 1
    if [ $? -eq 1 ] | [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 0 1 3
    fi
    
    /nn/bin/raidled_util 0 0
    if [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 0 0 3
    fi
    #Check color of RAID-Status LED 1 and Trun it off
    /nn/bin/raidled_util 1 1
    if [ $? -eq 1 ] | [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 1 1 3
    fi
    
    /nn/bin/raidled_util 1 0
    if [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 1 0 3
    fi
    #Check color of HDD-2 LED 2 and Trun it off
    /nn/bin/raidled_util 2 1
    if [ $? -eq 1 ] | [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 2 1 3
    fi
    
    /nn/bin/raidled_util 2 0
    if [ $? -eq 0 ]; then
	   /nn/bin/raidled_util 2 0 3
    fi
fi

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

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

if [ "$1" = "fast" ]; then
	sleep 1
else
	sleep 5
fi

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

echo "Turning off swap."
sync ; sync
swapoff -a

echo "Unmounting file systems"
umount -av

if [ $sysshutdown = "sleep" ] && [ "$command" = "halt" ]; then
    if cat /proc/mdstat | grep active >& /dev/null; then
	echo "Stopping RAID"
	mdadm --stop /dev/md{1,2,3,5,6,7,8,9,10,11,12}
    fi
fi

echo "Flush the hard drive cache"
#examine HDD-1 and HDD-2
/sbin/mdadm --examine /dev/sdb1 >& /dev/null
if [ $? -eq 0 ]; then
   hdparm -F /dev/sdb
fi

/sbin/mdadm --examine /dev/sdd1 >& /dev/null
if [ $? -eq 0 ]; then
   hdparm -F /dev/sdd 
fi

# Do not park the hard drive for 450
# This seems to cause issues with the md
# driver used for RAID
#echo "Parking the hard drive"
#hdparm -Y /dev/sdb
#hdparm -Y /dev/sdd >& /dev/null

# maybe we use Multiple devices
if test -f /etc/mdtab -a -x /sbin/mdadd ; then
   echo -n "Disable Multiple Devices"
   /sbin/mdstop -a
   echo "."
fi

# See if this is a powerfail situation.
if [ -f /opt/etc/apcupsd/powerfail ]; then
  echo
  echo "Apcupsd will now power off the UPS!"
  echo
  /opt/etc/apcupsd/apccontrol killpower
  echo
fi


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

echo $message

# Halt the system by sleep or power shutdown 
if [ $sysshutdown = "sleep" ] && [ "$command" = "halt" ]; then
    echo
    echo "Power may be safely disconnected!"
    sleep  4294967295
fi
exec $command -d -f
