#!/bin/bash

#ipmi-set-view.sh

#Configure or View the settings on Dell and SuperMicro IPMI cards
#dranch@trinnet.net

# 09/10/09 - added power on/off support
# 08/04/09 - added reset support
# 07/10/09 - disable $SUDO for reads
# 06/15/09 - make root required only for sets
#
# 04/20/09 - added a test to make sure -r or -n was specified
#
# 02/17/09 - Added a destination IP ping test
#
# 02/05/09 - updated to support Dell ipmi
#            - Dell doesn't require hardcoded DGW MAC
#            - added UID=0 check
#            - added setting IPMI user password 
#            - disable setting BMC MAC for remote setups as it's already
#              working
#            - Added extensive command line option support
#            - Added IPMI event list support
#
# 02/10/08 - disabled GARP
# 12/11/06 - Tool now supports the configuration of remote IPMI devices
#            assuming that they have an IP address assigned
# 11/27/06 - Added a 3 second sleep after the IPMI time clear
# 11/14/06 - Added IPMI date view and set
# 10/27/06 - Added ipmi module testing
# 10/19/06 - dynamically finds the ipmitool binary
# 09/06/06 - Added the optional support to write ipaddr and macaddr

#Todo:
#
#   support command line options for local vs. remote


# -----------------------------------------------------------------
# User changable variables
#
#     These variables can be overridden via the CMDLINE 
# -----------------------------------------------------------------

#Enable Debugging if desired
DEBUG=1

#Location of the IPMITOOL
IPMITOOL=`which ipmitool`

#IPMI Config type - local or remote - default is LOCAL
IPMITYPE=local

#OPTIONAL - Local IPMI settings to set the IP address of the BMC
#  Used when the BMC hasn't been already programed
#
#NEWIPADDR="10.0.8.177"

#BMC IP netmask
#  Supermicro:  netmask is not settable via the ipnmac DOS tool so 
#               lets do them here
#
NETMASK="255.255.254.0"

#DGW for the BMC
#
DGWIP="10.0.0.1"


#BMC MAC address:
#  Dell IPMI allows for dynamic arping 
#
#  Old supermicro ipmi cards require the MAC address of the DGW to be
#   statically set
#
#  Leave blank otherwise
DGWMAC=""

#MAC address of BMC
#  Only used when the BMC hasn't been already programed
#  Dells seem to be pre-programmed
#  Supermicros are blank and have to be programmed
#
#  Leave blank otherwise
MACADDR=""


#SNMP community for SNMP traps
SNMPCOMMUNITY="changeme"


#Default Supermicro IPMI login is ADMIN:ADMIN
#Default Dell IPMI login is root:calvin
#
REMOTEIPMIUSER="root"
REMOTEIPMIPASSWD="calvin"
NEWREMOTEIPMIPASSWD="changeme2"

#Destination IPMI server to test for IPs in use
#xdh1-ipmi.admin.zvents.com
TESTIPMISERVER=10.0.8.146

# -----------------------------------------------------------------
# End of User changable variables
# -----------------------------------------------------------------

#support the disabling of using $SUDO
SUDO="sudo"


# ---------------------
# Function: usage
# ---------------------
function usage {
     echo -e "\nipmi-s-v.sh\n"
     echo -e "\nUsage:"
     echo -e "         -r <remote IPMI address to connect to else assume local>"
     echo -e "         -p <IPMI password> or <prompt> for interactive passwd"
     echo -e "         -n <new remote IPMI address to set the BMC to>\n"
     echo -e "         -a : Action"
     echo -e "            -a <v:iew> IPMI values"
     echo -e "            -a <s:et base IPMI values : all settings are manually set"
     echo -e "            -a <powerstatus | poweron | poweroff | reset> on the hardware"
     echo -e " "
     #echo -e "        If an option isn't given, the script will be interactive"
     echo -e "\nAbort.\n"
     exit 1
}

# ---------------------
# Function: debug
# ---------------------
function debug {
  if [ $DEBUG -eq 1 ]; then
     echo -e "\nDEBUG:\n"
     echo -e "         CMDLINE:            $0"
     echo -e "         IPMITOOL:           $IPMITOOL"
     echo -e "         REMOTEIPMIUSER:     $REMOTEIPMIUSER"
     echo -e "         REMOTEIPMIPASSWD:   $REMOTEIPMIPASSWD"
     echo -e "         REMOTEIPMIHOST:     $REMOTEIPMIHOST"
     echo -e "         NEWREMOTEIPMIHOST:  $NEWREMOTEIPMIHOST"
     echo -e "         OPTARG:             $OPTARG"
     echo -e " "
  fi
}

# ---------------------
# Function: ipmimodtest
# ---------------------

function ipmimodtest {
  if [ -z "`/sbin/lsmod | grep ipmi_si`" ]; then
     echo -e "\nRequired IPMI modules not loaded.  Loading.."
     /root/ipmimod.sh
  fi
}

# ------------------------
# Function: ipmiremotetest
# ------------------------
function ipmiremotetest {
  #Test to make sure the IPMI username:password is correct
  #  user 2 username can be displayed with 'ipmitool user list 1'
  #
  if [ $DEBUG = 1 ]; then
     echo -e "\nDEBUG:"
     echo -e "        starting ipmiremotetest"
     echo -e " "
  fi

  echo -e "\nAny long delays at this stage indicate the remote BMC either has a different passwd or IPMI LAN access disabled"
  if [ $DEBUG = 1 ]; then
     echo -e "\nDEBUG:"
     echo -e "        Testing stock IPMI passwd: $REMOTEIPMIPASSWD"
     echo -e " "
  fi

  if [ -z "`$IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user test 2 16 $REMOTEIPMIPASSWD 2>&1 | grep -i success`" ]; then
     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "  failed to login using the $REMOTEIPMIPASSWD REMOTEIPMIPASSWD password"
     fi

      if [ -n "`$IPMITOOL -U $REMOTEIPMIUSER -P $NEWREMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user test 2 16 $NEWREMOTEIPMIPASSWD 2>&1 | grep -i success`" ]; then     
         if [ $DEBUG = 1 ]; then
            echo -e "\nDEBUG:"
            echo -e "\nNotice: Remote BMC using new password"
         fi
         REMOTEIPMIPASSWD=$NEWREMOTEIPMIPASSWD 
        else
          echo -e "\nERROR:  Both configured IPMI username:password combos don't work."
          echo -e "\n       Try using the -p option and specifying a new password.\nAborting\n"
          exit 1
      fi 
     else
      if [ $DEBUG = 1 ]; then
         echo -e "\nDEBUG:"
         echo -e "        success with IPMI passwd: $REMOTEIPMIPASSWD"
         echo -e "        IPMI passwd check using paswd: $REMOTEIPMIPASSWD : `$IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user test 2 16 $REMOTEIPMIPASSWD 2>&1`"
         echo -e " "
      fi
  fi
}

# ------------------
# Function: ipmiview
# ------------------
function ipmiview {

  #HACK - don't use $SUDO for reads
  if [ $DEBUG = 1 ]; then
     echo -e "\nDEBUG:"
     echo -e "         Overriding SUDO variable"
  fi
  SUDO=""
  
  #LOCAL - get IPMI settings
  if [ $IPMITYPE = "local" ]; then
     echo -e "\nCurrent -local- IPMI Info and LAN settings"
     echo -e "---------------------------------"
     $SUDO $IPMITOOL -I open mc info 
     $SUDO $IPMITOOL -I open lan print 1 

     echo -e "\nCurrent -local- IPMI users"
     echo -e "--------------------------"
     $SUDO $IPMITOOL -I open user list 1 

    
     echo -e "\nCurrent -local- IPMI events"
     echo -e "---------------------------"
     $SUDO $IPMITOOL -I open sel
     echo " "
     $SUDO $IPMITOOL -I open sel list

     echo -e "\nCurrent -local- IPMI date and time"
     echo -e "----------------------------------"
     $SUDO $IPMITOOL -I open sel time get
  fi

  #REMOTE - get IPMI settings
  if [ $IPMITYPE = "remote" ]; then
     #Make sure you have a good IPMI username:password
     ipmiremotetest

     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "         REMOTEIPMIUSER:     $REMOTEIPMIUSER"
        echo -e "         REMOTEIPMIPASSWD:   $REMOTEIPMIPASSWD"
        echo -e "         REMOTEIPMIHOST:     $REMOTEIPMIHOST"
        echo -e "         NEWREMOTEIPMIHOST:  $NEWREMOTEIPMIHOST"
        echo -e "         OPTARG:             $OPTARG"
        echo -e " "
     fi

     echo -e "\nCurrent -remote- IPMI MC and LAN settings"
     echo -e "----------------------------------"
     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "    Variables: SUDO: $SUDO :: REMOTEIPMIUSER: $REMOTEIPMIUSER :: REMOTEIPMIPASSWD: $REMOTEIPMIPASSWD :: REMOTEIPMIHOST: $REMOTEIPMIHOST"
        echo -e "        success with IPMI passwd: $REMOTEIPMIPASSWD"
        echo -e "        IPMI passwd check using paswd: $REMOTEIPMIPASSWD : `$IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user test 2 16 $REMOTEIPMIPASSWD 2>&1`"
        echo -e " "
     fi
     echo "ONE"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus mc info 
     echo "TWO"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan print 1 

     echo -e "\nCurrent -remote- IPMI Users"
     echo -e "-----------------------------------"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user list 1 

     echo -e "\nCurrent -REMOTE- IPMI events"
     echo -e "---------------------------"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel 
     echo " "
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel list

     echo -e "\nCurrent -remote- IPMI date"
     echo -e "--------------------------"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel time get
  fi
}


# -----------------
# Function: ipmiset
# -----------------
function ipmiset {

  if [ "$UID" != "0" ]; then
     echo -e "\nYou must be root to use set\nAborting.\n"
     exit 1
  fi

  if [ $IPMITYPE = "local" ]; then
     echo -e "\nAbout to set -LOCAL- IPMI card to the following using defaults:\n"
  fi
  if [ $IPMITYPE = "remote" ]; then
     echo -e "\nAbout to set -REMOTE- IPMI card to the following using defaults:\n"
  fi

  #Setting the IPADDR and MACADDR are optional so lets test for them
  if [ -n "$NEWIPADDR" ]; then
     if [ $IPMITYPE != "local" ]; then
        if [ -n "`ping -w 1 -c 1 $TESTIPMISERVER | grep icmp_seq`" ]; then
           if [ -n "`ping -w 1 -c 2 $NEWIPADDR | grep icmp_seq`" ]; then
              echo -e "\nError: New IPMI IP address: $NEWIPADDR is already in use."
              echo -e "Abortingi.\n\n"
              exit 1
             else
              echo -e "      New IPMI ip addr: $NEWIPADDR"
           fi
          else
              echo -e "\nError: Cannot reach IPMI test server.  Check to see if you have a route."
              echo -e "Aborting.\n\n"
              exit 1
        fi
       else
         echo -e "      New IPMI ip addr: $NEWIPADDR"
     fi
  fi

  if [ -n "$MACADDR" ]; then
    echo -e "      New IPMI mac addr: $MACADDR"
  fi

  if [ -n "$NEWIPADDR" ]; then
    echo -e "      New Netmask: $NETMASK"
    echo -e "      New DGW IP:  $DGWIP"
  fi

  if [ -n "$DGWMAC" ]; then
     echo -e "      New DGW MAC: $DGWMAC"
  fi 

  if [ -n "$NEWREMOTEIPMIPASSWD" ]; then
     echo -e "      New IPMI passwd: $NEWREMOTEIPMIPASSWD"
  fi

  if [ -n "$SNMPCOMMUNITY" ]; then
     echo -e "      New SNMP community: $SNMPCOMMUNITY"
  fi

  echo -e "\nContinue?  If you want different settings, please change the"
  echo -e "command line options or edit the ipmi-s-v.sh script to alter the"
  echo -e "defaults and re-run this script."
  echo -e "\nPress any to continue to write these changes or Control-C to abort"

  read $DUMMY

  
  #LOCAL - Set primary network access
  if [ $IPMITYPE = "local" ]; then
     echo -e "\nSetting -local- IPMI LAN settings"
     if [ -n "$NEWIPADDR" ]; then
        $SUDO $IPMITOOL -I open lan set 1 ipaddr $NEWIPADDR
        $SUDO $IPMITOOL -I open lan set 1 netmask $NETMASK
        $SUDO $IPMITOOL -I open lan set 1 defgw ipaddr $DGWIP
     fi
     if [ -n "$MACADDR" ]; then
        $SUDO $IPMITOOL -I open lan set 1 macaddr $MACADDR
     fi
     if [ -n "$DGWMAC" ]; then
        $SUDO $IPMITOOL -I open lan set 1 defgw macaddr $DGWMAC
     fi
  fi

  #LOCAL - Set extra items
  if [ $IPMITYPE = "local" ]; then
     echo -e "\nSetting -local- IPMI extra settings"
     echo -e "\nClearing SEL events, setting TIME, and SNMP\n"
     $SUDO $IPMITOOL -I open sel clear
     echo -e "\nSleeping for 3 seconds"
     sleep 3
     echo -e "Setting TIME"
     $SUDO $IPMITOOL -I open sel time set "`date '+%m/%d/%Y %H:%M:%S'`"

     if [ -n "$SNMPCOMMUNITY" ]; then
        echo -e "\nSetting SNMP Community"
        $SUDO $IPMITOOL -I open lan set 1 snmp $SNMPCOMMUNITY
     fi


     #Dells have IPMI LAN access disabled by default.  Enable it
     echo -e "\nEnable IPMI LAN access"
     $SUDO $IPMITOOL -I open lan set 1 access on
     
     #Needed for Supermicro BMCs but not clear for Dell
     $SUDO $IPMITOOL -I open lan set 1 arp respond on
     #GARP support - not needed
     $SUDO $IPMITOOL -I open lan set 1 arp generate off

     echo -e "\nDate according to IPMI:"
     $SUDO $IPMITOOL -I open sel time get

     #Must be set last
     if [ -n "$NEWREMOTEIPMIPASSWD" ]; then
        echo -e "\nNew IPMI password being set"
        $SUDO $IPMITOOL -I open user set password 2 $NEWREMOTEIPMIPASSWD
     fi
  fi


  #REMOTE - Set extra items
  #------------------------
  if [ $IPMITYPE = "remote" ]; then

     #Make sure you have a good IPMI username:password
     ipmiremotetest

     echo -e "\nSetting -remote- IPMI extra settings"
     echo -e "\nClearing SEL events"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel clear

     echo -e "\nSleeping for 3 seconds"
     sleep 3
     echo -e "Setting date/time"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel time set "`date '+%m/%d/%Y %H:%M:%S'`"

     if [ -n "$SNMPCOMMUNITY" ]; then
        echo -e "\nSetting SNMP community"
        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 snmp $SNMPCOMMUNITY
     fi

     #Used for legacy Supermicro BMCs
     #
     echo -e "Setting ARP respond"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 arp respond on
     echo -e "Setting GARP off"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 arp generate off

     echo -e "\nDate according to IPMI:"
     $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus sel time get
  fi

  #REMOTE- Set primary network access -- MUST BE done last if switching subnets
  #----------------------------------------------------------------------------
  if [ $IPMITYPE = "remote" ]; then
     #Make sure you have a good IPMI username:password
     ipmiremotetest

     echo -e "\nSetting -remote- IPMI LAN settings"
     if [ -n "$NEWIPADDR" ]; then
        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 ipaddr $NEWIPADDR

        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 netmask $NETMASK

        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 defgw ipaddr $DGWIP
     fi

     #
     # NOTE: There is a race condition here between changing the IP address and when it goes into effect
     #

     #Don't set the BMC and DGW MACs for remote configs as they are already 
     #working
     #if [ -n "$MACADDR" ]; then
     #   $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 macaddr $MACADDR
     #fi
      #$SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus lan set 1 defgw macaddr $DGWMAC

     #Must be set last
     if [ -n "$NEWREMOTEIPMIPASSWD" ]; then
        echo -e "\nNew IPMI password being set"
        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus user set password 2 $NEWREMOTEIPMIPASSWD
     fi
  fi
}

# ------------------
# Function: ipmireset
# ------------------
function ipmireset {

  #HACK - don't use $SUDO for reads
  if [ $DEBUG = 1 ]; then
     echo -e "\nDEBUG:"
     echo -e "         Overriding SUDO variable"
  fi
  SUDO=""
  
  #LOCAL - get IPMI settings
  if [ $IPMITYPE = "local" ]; then
     echo -e "\nSince you are running this command from the local machine, IPMI is not the way to reset"
     echo -e "the hardware.  Please issue a '/sbin/shutdown -r now" or "/sbin/shutdown -h now"
     echo -e "\n\n"
     exit 0
  fi

  #REMOTE - get IPMI settings
  if [ $IPMITYPE = "remote" ]; then
     #Make sure you have a good IPMI username:password
     ipmiremotetest

     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "         REMOTEIPMIUSER:     $REMOTEIPMIUSER"
        echo -e "         REMOTEIPMIPASSWD:   $REMOTEIPMIPASSWD"
        echo -e "         REMOTEIPMIHOST:     $REMOTEIPMIHOST"
        echo -e "         NEWREMOTEIPMIHOST:  $NEWREMOTEIPMIHOST"
        echo -e "         OPTARG:             $OPTARG"
        echo -e " "
     fi

     echo -e "\nAre you ABSOLUTELY sure you want to issue a hardware reset?"
     echo -e "Resets are preferred over PDU power cycles as is's less rough on the hardware".
     echo -en "\n\nProceed with RESET? (yes/no): "
     read RESET

     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "         RESET:              $RESET"
     fi
     
     if [ "$RESET" = "yes" ]; then
        echo -e "Resetting device now.."
        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus power reset
        echo -e "\nComplete\n\n"
        exit 0
       else
        echo -e "\n\nReset was not asserted.  \nExiting.\n\n"
        exit 0
     fi
  fi
}


# ------------------
# Function: ipmipower
# ------------------
function ipmipower () {

  #HACK - don't use $SUDO for reads
  if [ $DEBUG = 1 ]; then
     echo -e "\nDEBUG:"
     echo -e "         Overriding SUDO variable"
  fi
  SUDO=""
  
  #LOCAL - get IPMI settings
  if [ $IPMITYPE = "local" ]; then
     echo -e "\nSince you are running this command from the local machine, IPMI is not the way to power"
     echo -e "up/down the hardware.  Please issue a '/sbin/shutdown -r now" or "/sbin/shutdown -h now"
     echo -e "\n\n"
     exit 0
  fi

  #REMOTE - get IPMI settings
  if [ $IPMITYPE = "remote" ]; then
     #Make sure you have a good IPMI username:password
     ipmiremotetest

     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "         REMOTEIPMIUSER:     $REMOTEIPMIUSER"
        echo -e "         REMOTEIPMIPASSWD:   $REMOTEIPMIPASSWD"
        echo -e "         REMOTEIPMIHOST:     $REMOTEIPMIHOST"
        echo -e "         NEWREMOTEIPMIHOST:  $NEWREMOTEIPMIHOST"
        echo -e "         OPTARG:             $OPTARG"
        echo -e " "
     fi

     echo -e "\nAre you ABSOLUTELY sure you want to issue a hardware command?"
     echo -e "Resets are preferred over PDU power cycles as is's less rough on the hardware".
     echo -en "\n\nProceed with POWER CMD? (yes/no): "
     read POWERAUTH

     if [ $DEBUG = 1 ]; then
        echo -e "\nDEBUG:"
        echo -e "         POWERAUTH:              $POWERAUTH"
     fi

     POWERCMD=$1
     
     if [ "$POWERAUTH" = "yes" ]; then
        echo -e "Setting power command: $POWERCMD now"
        #echo "DEBUG: 0: $0 1: $1 2: $2 3: $3 4: $4 5: $5"
        $SUDO $IPMITOOL -U $REMOTEIPMIUSER -P $REMOTEIPMIPASSWD -H $REMOTEIPMIHOST -I lanplus power $POWERCMD
        echo -e "\nComplete\n\n"
        exit 0
       else
        echo -e "\n\nPower CMD was not asserted.  \nExiting.\n\n"
        exit 0
     fi
  fi
}



#------
# Main
#------

#Make sure the local IPMI system is ready
ipmimodtest

if [ -z "$1" ]; then
   usage
fi

#old setup -- remove
#  echo -e "\nDo you want to v)iew or s)et IPMI settings."
#  echo -en "\nPlease enter in "v" or "s" now: "
#  read INPUT
#  echo -e "\nINPUT: $INPUT"
# else
#  echo -e "\nCMD line option given: $1"
#  INPUT=$1
#fi

#disable case sensitive
shopt -s nocaseglob

#Run initial debug run to see if we will chance any variables
debug

# -r <remote IPMI address to connect to else assume local>"
# -p <IPMI password> or <prompt> for interactive passwd"
# -n <new remote IPMI address to set the BMC to>"
# -a <v:iew : s:et : reset ) IPMI values"

if [ $DEBUG -eq 1 ]; then
  echo "DEBUG: evaling command line options"
fi


while getopts r:n:p:a:h: o

#Need to get this -r test working
#
#echo "DEBUG: O is $o"
#echo "DEBUG2: $o"
#echo "DEBUG: `echo $o | grep '\-r'`"
#echo "DEBUG: `echo $o | grep '\-n'`"
#if [ -z "`echo $o | grep '\-r'`" -o -z "`echo $o | grep '\-n'`" ]; then
#  echo -e "\nERROR: options -r or -n are not set\nAborting!\n\n"
#  exit 1
#fi

do case "$o" in
        r)      REMOTEIPMIHOST="$OPTARG"
                IPMITYPE=remote
                if [ $DEBUG = 1 ]; then
                   echo "DEBUG: Getting REMOTEIPHOST"
                fi
                ;;
        n)      NEWIPADDR="$OPTARG"
                if [ $DEBUG = 1 ]; then
                   echo "DEBUG: Getting NEWIPADDR"
                fi
                ;;
        p)      REMOTEIPMIPASSWD="$OPTARG"
                if [ $DEBUG = 1 ]; then
                   echo "DEBUG: Getting REMOTEIPMIPASSWD"
                fi
                if [ "$REMOTEIPMIPASSWD" = "prompt" ]; then
                   echo -en "\nEnter IPMI password: "
                   read REMOTEIPMIPASSWD
                fi
                ;;
        a)      if [ $DEBUG = 1 ]; then
                   echo "DEBUG: Getting Action"
                fi
                if [ "$OPTARG" = "s" ]; then
                   debug
                   ipmiset
                   echo -e "\n\n"
                   exit 0
                fi
                if [ "$OPTARG" = "v" ]; then
                   debug
                   ipmiview
                   echo -e "\n\n"
                   exit 0
                fi
                if [ "$OPTARG" = "reset" ]; then
                   debug
                   ipmireset
                   echo -e "\n\n"
                   exit 0
                fi
                if [ "$OPTARG" = "powerstatus" ]; then
                   debug
                   ipmipower status
                   echo -e "\n\n"
                   exit 0
                fi
                if [ "$OPTARG" = "poweron" ]; then
                   debug
                   ipmipower on
                   echo -e "\n\n"
                   exit 0
                fi
                if [ "$OPTARG" = "poweroff" ]; then
                   debug
                   ipmipower off
                   echo -e "\n\n"
                   exit 0
                fi
                ;;
        h)      usage
                exit 1
                ;;
        [?])    usage 
                exit 1
                ;;
        esac
done

if [ $DEBUG -eq 1 ]; then
  echo "DEBUG: CMD line args: $@"
fi
#if [ -z $REMOTEIPMIHOST ] || [ -z $NEWIPADDR ] || [ -z $OPTARG ]; then
#   echo -e "\n-r or -n was not specified"
#   usage
#   exit
#fi


echo -e "\ndone.\n\n"

