#!/bin/sh

#
# Arrange for "prepare" to be run on the way down, and then shutdown.
#
. /etc/emulab/paths.sh

echo "$ETCDIR/prepare.sh" > /bootcmd

#
# XEN vms will just restart, so halt to really stop it.
# Luckily, OpenVZ VMs respect halt as well, so we can always halt
# inside of VMs.
#
dohalt=0
if [ -e "$BOOTDIR/vmname" ]; then
    dohalt=1
fi

reboot=1
if [ $# -eq 1 ]; then
    if [ "$1" = "-noreboot" ]; then
	reboot=0
    fi
fi
if [ $reboot -eq 1 ]; then
    if [ $dohalt -eq 1 ]; then
	shutdown -h now "Halting with Prepare"
    else
	shutdown now "Rebooting with Prepare"
    fi
fi

exit 0
