#!/bin/sh

set -e

ME=$(basename $0)

usage () {
	echo "${ME} <serial>"
	exit 1
}

if [ $# != 1 ] ; then
	usage
fi

SERIAL=${1}

TMP_FILE=$(mktemp -t ${ME}-machine-attributes)
ocicli -csv machine-list -a --filter serial=${SERIAL} >${TMP_FILE}

product_name=$(cat ${TMP_FILE} | q -H -d, "SELECT product_name FROM -")
system_manufacturer=$(cat ${TMP_FILE} | q -H -d, "SELECT system_manufacturer FROM -")
ipmi_username=$(cat ${TMP_FILE} | q -H -d, "SELECT system_manufacturer FROM -")

rm -f ${TMP_FILE}

# If username is "-" it means we do not have IPMI credentials, and we cant do anything.
if [ ${ipmi_username} = "-" ] ; then
	exit 0
fi

case "${system_manufacturer}" in
"HPE")
	case "${product_name}" in
	"ProLiant DL385 Gen11")
		# We do || true, because if the setting is already there,
		# ilorest will not return zero.
	        oci-ilorest ${SERIAL} set --selector=Bios LastLevelCacheAsNUMANode=Enabled --commit || true
	        oci-ilorest ${SERIAL} set --selector=Bios WorkloadProfile=Virtualization-MaxPerformance --commit || true
	        oci-ilorest ${SERIAL} set --selector=Bios AmdSecureMemoryEncryption=Enabled --commit || true
	        oci-ilorest ${SERIAL} set --selector=Bios AmdVirtualDrtmDevice=Enabled --commit || true
	        oci-ilorest ${SERIAL} set --selector=Bios TransparentSecureMemoryEncryption=Enabled --commit || true
	        oci-ilorest ${SERIAL} set --selector=Bios AmdSecureNestedPaging=Enabled --commit || true
	        exit 0
	;;
	*)
		exit 0
	;;
	esac
;;
*)
	exit 0
;;
esac
