# (c) 2002-2003 Newisys, Inc.  All rights reserved.
#
################################################################################
# This file contains the following utility platform functions to be used in scripts:
# shutdown
# boot
# reboot
################################################################################
# The following must be defined for all functions:
#
#verbose
#maxWaitTime (seconds)
#sleepTime   (seconds)
#
################################################################################
# Also need to define: osOffMsg
#
# Try to shut down the operating system gracefully.
# As a last resort, turn the power off.
#
# params:
# ip  - SP ip to execute the command on
#       if not present, assume we are running from the SP...
#
# return 0 if os is off
# return 1 if os not off
################################################################################
function shutdown(){
  local ip=$1
  local currTime=`date +%s`
  local finalTime=$(($maxWaitTime + $currTime))
  local pre_cmd=""

  if [ "${ip}XX" != "XX" ]
  then
    pre_cmd="ssh $ip"
  fi

  cmd="$pre_cmd platform set os state shutdown -f -W -q"
  if [ $verbose -eq 1 ] ; then echo "$cmd"; fi
  $cmd || true

  local currState=`$pre_cmd platform get os state || true`

  while [ "$currState" != "$osOffMsg" ] && [ $currTime -lt $finalTime ]
  do
    if [ $verbose -eq 1 ] ; then echo "sleeping $sleepTime"; fi
    sleep $sleepTime
    currTime=`date +%s`
    currState=`$pre_cmd platform get os state || true`
  done

  currState=`$pre_cmd platform get os state || true`

  if [ "$currState" == "$osOffMsg" ]
  then
    return 0
  fi

  cmd="$pre_cmd platform set os power state off -f -W -q"
  if [ $verbose -eq 1 ] ; then echo "$cmd"; fi
  $cmd || true

  currTime=`date +%s`
  finalTime=$(($maxWaitTime + $currTime))
  currState=`$pre_cmd platform get os state || true`

  while [ "$currState" != "$osOffMsg" ] && [ $currTime -lt $finalTime ]
  do
    if [ $verbose -eq 1 ] ; then echo "sleeping $sleepTime"; fi
    sleep $sleepTime
    currTime=`date +%s`
    currState=`$pre_cmd platform get os state || true`
  done

  if [ "$currState" == "$osOffMsg" ]
  then
    return 0
  fi

  return 1
}

################################################################################
# need to define: osOffMsg osCommunicateMsg 
#
# Try to boot the operating system.
# We are going to wait twice the amount of time since we can reboot
# from the OS "communicating" state...which means the OS must shutdown.
#
# ip  - SP ip to execute the command on
#       if not present, assume we are running from the SP...
#
# return 0 if success
# return 1 if it cannot be shutdown
# return 2 if it cannot be booted into osCommunicateMsg
################################################################################
function boot(){
  local ip=$1
  local currTime=`date +%s`
  local finalTime=$(( ($maxWaitTime * 2) + $currTime))
  local pre_cmd=""

  if [ "${ip}XX" != "XX" ]
  then
    pre_cmd="ssh $ip"
  fi

  local currState=`$pre_cmd platform get os state || true`

  if [ "$currState" != "$osOffMsg" ]
  then
    shutdown $ip
    rc=$?
    if [ $rc -ne 0 ]
    then
      return 1
    fi
  fi

  cmd="$pre_cmd platform set os state boot -W -q"
  if [ $verbose -eq 1 ] ; then echo "$cmd"; fi
  $cmd || true

  currTime=`date +%s`
  finalTime=$(( ($maxWaitTime * 2) + $currTime))

  while [ "$currState" != "$osCommunicateMsg" ] && [ $currTime -lt $finalTime ]
  do
    if [ $verbose -eq 1 ] ; then echo "sleeping $sleepTime"; fi
    sleep $sleepTime
    currTime=`date +%s`
    currState=`$pre_cmd platform get os state || true`
  done

  currState=`$pre_cmd platform get os state || true`

  if [ "$currState" == "$osCommunicateMsg" ]
  then
    return 0
  fi

  return 2
}

################################################################################
# need to define: osCommunicateMsg 
#
# We are going to wait twice the amount of time since we can reboot
# from the OS "communicating" state...which means the OS must shutdown.
#
# ip  - SP ip to execute the command on
#       if not present, assume we are running from the SP...
#
# return 0 if success
# return 1 if can't get in the communicating state
################################################################################
function reboot(){
  local ip=$1
  local currTime=`date +%s`
  local finalTime=$(( ($maxWaitTime * 2) + $currTime))
  local pre_cmd=""

  if [ "${ip}XX" != "XX" ]
  then
    pre_cmd="ssh $ip"
  fi

  cmd="$pre_cmd platform set os state reboot -W -q"
  if [ $verbose -eq 1 ] ; then echo "$cmd"; fi
  $cmd || true

  local currState=`$pre_cmd platform get os state || true`

  while [ "$currState" != "$osCommunicateMsg" ] && [ $currTime -lt $finalTime ]
  do
    if [ $verbose -eq 1 ] ; then echo "sleeping $sleepTime"; fi
    sleep $sleepTime
    currTime=`date +%s`
    currState=`$pre_cmd platform get os state || true`
  done

  currState=`$pre_cmd platform get os state || true`

  if [ "$currState" == "$osCommunicateMsg" ]
  then
    return 0
  fi

  return 1
}

