#!/bin/bash

# Now make sure the modules are loaded
for module in ipmi_si ipmi_devintf ipmi_msghandler; do
    if lsmod |grep -q $module; then
        echo "$module already loaded"
    else
        echo "Loading $module..."
        modprobe $module
        result=$?
        # If the IPMI drivers don't load, it could be the system has no BMC, or
        # the IPMI driver is bad.  We should error and Fail here.
        if [ $result -eq 1 ]; then
            echo "ERROR: Unable to load module $module" >&2
            echo "Aborting IPMI test run." >&2
            exit 1
        else
            echo "Successfully loaded module $module"
        fi
    fi
done

# Now get our info from ipmitool to make sure communication works
# First lest check chassis status
echo "Checking for chassis status"
ipmitool chassis status && echo "Successfully got chassis status" && chassis=0 || chassis=1
echo "Checking to see if we can get sensor data"
ipmitool sdr list full && echo "Successfully got sensor data" && sensor=0 || sensor=1
echo "Checking to see if we can get info on the BMC"
ipmitool bmc info && echo "Successfully got BMC information" && bmc=0 || bmc=1

# if everything passes, exit 0
[ $chassis -eq 0 ] && [ $sensor -eq 0 ] && [ $bmc -eq 0 ] && exit 0 || echo "FAILURE: chassis: $chassis  sensor: $sensor  bmc: $bmc"

# otherwise exit 1
exit 1
