#!/bin/bash

. run_profile.txt
. $base_dir/plans/commands/common.fns

kernel_info

SUT=`hostname -s`

initrd=""
if [ "$kconfig" != "" ]; then
	if grep "CONFIG_BLK_DEV_INITRD=y" $kconfig >/dev/null 2>&1; then
		filename="initrd-$kernel_label.img"
		initrd="--initrd=$filename"
		echo "using initrd, filename:$filename"
	fi
fi

echo boottool --boot-once --title "$kernel_label" "$initrd"
boottool --boot-once --title "$kernel_label"
err=$?

if [ $err -ne 0 ]; then
    echo "Error:  Lilo failed to set next kernel."
    echo "EXIT STATUS(lilo):  $err"
    return $err
fi

# set autoreboot on kernel panic
echo 60 > /proc/sys/kernel/panic

# Hack to workaround bug in NFS causing portmap issues if there
# is a mounted filesystem and a cronjob writing to it
echo Powercycling machine

umount -la -t nfs
umount -la -t nfs4

echo /sbin/shutdown now -r
/sbin/shutdown now -r
err=$?

if [ $err -ne 0 ]; then
    echo "Error:  Failed to shutdown."
    echo "EXIT STATUS(shutdown):  $err"
    return $err
fi

## Wait for a while as we reboot
echo sleep 300
sleep 300
#
## If we haven't rebooted by now, something must be wrong.
echo "Warning:  Haven't rebooted after 300 sec."

sync
sleep 30

if [ -f $all_suts_dir/$SUT/bin/power ]; then
    echo "Initiating a remote power cycle event"
    echo "$all_suts_dir/$SUT/bin/power cycle"
    $all_suts_dir/$SUT/bin/power cycle
    err=$?
    if [ $? -ne 0 ]; then
        echo "Error:  Remote power of $SUT failed"
        echo "EXIT STATUS(power):  $err"
        return $err
    fi
fi

# Wait for a while as we reboot
echo sleep 300
sleep 300

# If we've hit this point, something is extremely broken
echo "Error:  Cannot restart $SUT.  Terminating."
echo "EXIT STATUS(boot_kernel):  -1"
exit -1

