#!/bin/sh
# if umount -a fails give services additional time to shut down
fail(){
echo umount failed, try again.
sleep 1
sync
umount -a || sleep 3
}

# Shut down "respawn" services - except getty/1.
for i in $( find /etc/minit -type f -name respawn | sed 's@/respawn$@@' ); do
 if [ ! -e "$i/survive-shutdown" ]; then
  msvc -d "$i"
 fi
done

# emulate sysv shutdown
/etc/minit/runlevel-emulate 0 2

# send sigterm to all processes.
/sbin/killall5 -15

#send sighup to all processes.
/sbin/killall5 -1

sync
# umount everything, mount "/" readonly
/bin/umount -a || fail
/sbin/swapoff -a

# power down.
# params should contain one of "RESTART", "HALT" or "POWER_OFF" 
exec /sbin/hard-reboot $1
