#!/bin/sh
#
# Copyright (c) 2014-2016 University of Utah and the Flux Group.
# 
# {{{EMULAB-LICENSE
# 
# This file is part of the Emulab network testbed software.
# 
# This file is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
# 
# This file is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public
# License for more details.
# 
# You should have received a copy of the GNU Affero General Public License
# along with this file.  If not, see <http://www.gnu.org/licenses/>.
# 
# }}}
#

MFS_DEBUG=${MFS_DEBUG:-'n'}
if [ "$MFS_DEBUG" = "y" ]; then
    set -x
fi

if [ -r /etc/emulab/paths.sh ]; then
	. /etc/emulab/paths.sh
else
	BINDIR=/etc/testbed
	BOOTDIR=/etc/testbed
	ETCDIR=/etc/testbed
fi

# Enable IPoD
if [ -x $BINDIR/rc.ipod ]; then
    $BINDIR/rc.ipod
fi

if [ "$1" = "fail" ]; then
    echo "Emulab pxeboot process failed, dropping to shell. Exit to retry."
    PS1='(pxeboot) ' /bin/sh -i </dev/console >/dev/console 2>&1
fi

BOSSIP=`$BINDIR/tmcc bossinfo | awk '{ print $2 }'`

#
# XXX hang out a little longer, in case the BOOTING event has not made
# it to stated. Having BOOTING arrive after PXEWAIT can cause problems.
#
sleep 1

echo "`date`: entering pxewait"
bootwhat=`$BINDIR/bootinfoclient -s $BOSSIP`
echo "`date`: pxewait returns: $bootwhat"

case $bootwhat in
mfs:*frisbee)
    echo "continuing with frisbee..."
    #
    # XXX we might have been forced out of the BOOTING state when we
    # waited, so force us back in again (and more importantly, make sure
    # we are in the RELOAD opmode.
    #
    $BINDIR/tmcc state BOOTING
    exit 0
    ;;
*)
    echo "rebooting..."
    $BINDIR/tmcc state SHUTDOWN
    sleep 1
    reboot
    ;;
esac

exit 0
