#!/bin/bash -f
function usage {
echo "
   Name:  
      ipmiCmd  - use ipmitool to check an EED Controls Industrial PC (IPC)
                 power status or set the power state, via the IPC managment
                 port, which is an IPMI-enabled device.

   Usage:
          ipmiCmd [option] -c <cpu> -f <command> 

   Options:   
          -c   CPU node name, without -mgt
          -f   Chassis power command (ie. status, on, off, cycle, reset, diag soft)
          -h   Display this help

   Commands:
           status  - show current chassis power status
           on      - power on chassis
           off     - power off chassis into soft off (S4/S5 state).
                     WARNING: This command does not initiate a clean shutdown
                     of the operating system prior to powering down the system.
           cycle    -Provides a power off interval of at least 1 second.  
                     No action should occur if chassis power is in S4/S5 state, but it is
                     recommended to check power state first and only issue a power cycle 
                     command if the  system  power is on or in lower sleep state than S4/S5.
           reset   - perform a hard reset.
           diag    - pulse a diagnostic interrupt (NMI) directly to the processor(s)
           soft    - Initiate a soft-shutdown of OS via ACPI.  This can be done in a 
                     number of ways, commonly by simulating an  overtemperture
                     or by simulating a power button press.  It is necessary for
                     there to be Operating System support for ACPI and some sort
                     of daemon watching for events for this soft power to work.

   Examples:
          ipmiCmd -c cpu-b34-mp03 -f status   Return the power status of the management port
          ipmiCmd -c cpu-b34-mp02 -f on       Set the IPC power state to on
          ipmiCmd -c cpu-b34-mp02 -f off      Set the IPC power state to off
          
  Exit conditions
  0 - normal successful completion
  1 - too few arguments

   Auth: 27-Aug-2019, K. Luchini  (luchini):
"
}
#!--------------------------------------------------------------
#! Auth: 27-Aug-2019, K. Luchini      (luchini)
#! Rev:  dd-mmm-yyyy, Reviewer's Name (usename)
#----------------------------------------------------------------
# Mod: (most recent change listed first)
#      
#      dd-mmm-yyyy, Firt Lastname (username):
#       comment
#
#================================================================
if [ $# -lt 0 ]; then
   echo "No arguments, see ipmiCmd -h for help"
   exit 1;
fi

cpu=
cmd=
while getopts c:f:h opt
do
   case "$opt" in
       h) usage; exit 0 ;;
       c) cpu=`echo $OPTARG | tr '[:upper:]' '[:lower:]'`;;
       f) f=`echo $OPTARG | tr '[:upper:]' '[:lower:]'`; case "$f" in
            status) cmd="$f";;
            on)     cmd="$f";;
            off)    cmd="$f";;
            cycle)  cmd="$f";;
            reset)  cmd="$f";;
            diag)   cmd="$f";;
            soft)   cmd="$f";;      
            *)      echo "chassis power command not recognized. Must be status, on, off, cycle, reset, diag, soft"; exit 1;;
            esac ;;
       *) echo "Unknown Arguments, see ipmiCmd -h for help"; exit 1;;
   esac
done
shift $((OPTIND-1))

# Check for insufficient arguments.
if [ -z "$cpu" ] || [ -z "$cmd"  ]; then
   echo "Too few arguments, see ipmiCmd -h for help"
   exit 1;
fi

# Ok, now execute the chassis power command
ipmitool -H ${cpu}-mgt -I lanp -U ADMIN -P Ipmi1Admin power ${cmd}

# End of script
