#!/bin/bash
#
# Copyright (c) 2010-2011 Red Hat, Inc.
#
# This program is free software: you can redistribute it and/or
# modify it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 2 of
# the License, or (at your option) any later version.
#
# This program 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.

PATH=/sbin:/usr/sbin:$PATH

if [ -z "$RESULT_SERVER" ] ; then
	echo "RHTS test $TEST wants to reboot your system"
	echo -n "Press Enter to reboot or hit CTRL-C to stop"
	read Dummy
else
        # preserve $OUTPUTfile across reboots
        if [ -n "$OUTPUTFILE" ]; then
	    echo "export OUTPUTFILE=$OUTPUTFILE" >> /var/cache/rhts/$TESTID/ENV
	fi
fi

if efibootmgr &>/dev/null ; then
    os_boot_entry=$(efibootmgr | awk '/BootCurrent/ { print $2 }')
    # fall back to /root/EFI_BOOT_ENTRY.TXT if it exists and BootCurrent is not available
    if [[ -z "$os_boot_entry" && -f /root/EFI_BOOT_ENTRY.TXT ]] ; then
        os_boot_entry=$(</root/EFI_BOOT_ENTRY.TXT)
    fi
    if [[ -n "$os_boot_entry" ]] ; then
        logger -s "efibootmgr -n $os_boot_entry"
        efibootmgr -n $os_boot_entry
    else
        logger -s "Could not determine value for BootNext!"
    fi
fi

if [ -n "$RESULT_SERVER" ] ; then
    beahsh rebooting "Rebooting from task"
fi
/sbin/shutdown -r now

# Wait for the shutdown to kill us..  we don't want control to go back
#  to the test harness.
while true; do
    sleep 666
done
