#!/bin/sh
# Hand fan control back to the BMC's automatic curve.
# Paired with ipmi-fancontrol-manual via ipmi-fancontrol.service.
set -eu

SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
CONFIG=${IPMI_FANCONTROL_CONFIG:-$SCRIPT_DIR/conf/ipmi-fancontrol.conf}

if [ ! -r "$CONFIG" ]; then
    echo "Error: cannot read $CONFIG" >&2
    echo "Copy conf/ipmi-fancontrol.conf.example there and fill in the BMC credentials." >&2
    exit 1
fi

. "$CONFIG"

: "${IPMI_HOST:?not set in $CONFIG}"
: "${IPMI_USER:?not set in $CONFIG}"
: "${IPMI_PASSWORD:?not set in $CONFIG}"

# ipmitool -E reads the password from IPMI_PASSWORD, keeping it out of argv.
export IPMI_PASSWORD

# Re-enable automatic fan control.
ipmitool -I lanplus -H "$IPMI_HOST" -U "$IPMI_USER" -E raw 0x30 0x30 0x01 0x01
