#!/bin/bash
# (c) 2002-2003 Newisys, Inc.  All rights reserved.
#
# Example shell script to boot the operating system on several platforms.
# This example allows the user to modify the timeout time, the sleep time,
# as well as whether to print verbose output. 
#
# Requires: 
#    -f|--ipFile  A file that contains one Service Processor IP address per line.
#    An ssh connection will be made to execute commands.
#
. ./platform_funcs.sh

typeset -i verbose=0
typeset -i maxWaitTime=5
typeset -i sleepTime=1
ipFile="./sp-ips.txt"

until [ -z "$1" ]
do
  case "$1" in
    -h|--help)
       echo "-v|--verbose   print out helpful messages"
       echo "-m|--maxtime   define max time (mins) to wait on platform cmds to complete"
       echo "               default $maxWaitTime"
       echo "-s|--sleeptime time to sleep (secs) while waiting for commands to complete"
       echo "               default $sleepTime"
       echo "-f|--ipFile    file with a list of SP ip address to execute commands"
       echo "               default $ipFile"
       exit 0
       ;;
    -v|--verbose ) verbose=1 ;;
    -m|--maxtime ) shift; maxWaitTime=$1 ;;
    -s|--sleeptime ) shift; sleepTime=$1 ;;
    -f|--ipFile ) shift; ipFile=$1 ;;
    *) echo "Ignoring $1" ;;
  esac
  shift
done

# error checking
if [ ! -f $ipFile ]
then
  echo "Error. Cannot find file $ipFile"
  exit 1
fi

osCommunicateMsg="OS communicating with SP"
osOnMsg="On"
osOffMsg="Off"

# convert time to seconds
maxWaitTime=$(($maxWaitTime * 60))

if [ $verbose -eq 1 ] ; then 
  echo "verbose     $verbose"
  echo "maxWaitTime $maxWaitTime seconds"
  echo "sleepTime   $sleepTime"
  echo "ipFile      $ipFile"
  echo "osCommunicateMsg $osCommunicateMsg"
  echo "osOnMsg          $osOnMsg"
  echo "osOffMsg         $osOffMsg"
fi

for ip in `cat $ipFile` ; do
  if [ "$verbose" == "1" ] ; then echo "$ip:booting..."; fi
  boot $ip
  rc=$?
  if [ $rc -ne 0 ]
  then
    echo "$ip:Error. Cannot boot platform"
  fi
done
