#!/bin/sh

count=0
while [ -z "$(/usr/local/bin/ipmi sensors get Bat_Pack_Invalid | grep '\[State Deasserted\]')" ] && [ "$count" -lt 5 ] ; do
    sleep 1
    count=$((count+1))
done

if [ "$count" -eq 5 ] ; then
    #Make sure battery has charger enabled and run wakeup sequence
    /usr/local/bin/ipmi oem battery load wakeup

    if [ -z "$(/usr/local/bin/ipmi sensors get Bat_Pack_Invalid | grep '\[State Deasserted\]')" ] ; then
        #Failure
        /usr/local/bin/sel_logger "SP.notice" "Failure on battery wake up attempt"
    else
        #Successfully woke up
        /usr/local/bin/sel_logger "SP.notice" "Successfully woke up the battery"
    fi
fi
