#!/bin/sh

# Blatantly stolen from the installer
ask() {
  answer=''
  while [ y != "$answer" -a n != "$answer" ]; do
    echo -n "$1"
    read answer
    [ Y = answer ] && answer=y
    [ N = answer ] && answer=n
    [ -z "$answer" ] && answer=$2
  done
}

if [ "$1" = "test" ]; then
  echo "Force an update of the workstation"
  exit 0
fi

# Unload kexec here, in case auto-updater wants to reboot
# (We really don't want cluster machines ending up back in recovery-mode)
[ -x "/sbin/kexec" ] && /sbin/kexec -u
echo "Running auto-updater, please wait..."
/usr/sbin/athena-auto-update
if [ $? != 0 ]; then
    echo "Something went wrong.  Press Enter to return to the menu."
    read dummy
    exit 0
fi
ask "You are strongly encouraged to reboot.  Reboot now? (y/n) " y
if [ "$answer" = "y" ]; then
    echo "OK, rebooting..."
    /sbin/reboot
fi
echo "Please reboot as soon as possible.  Press Enter to return to the menu."
read dummy
