#!/bin/sh

# This small shell script sets various iDRAC configuration
# so that the serial console can work by default.

set -e

IPMI_TYPE=$(ipmitool sdr elist mcloc | awk '{print $1}')

if ! [ -x /usr/bin/racadm ] ; then
	exit 0
fi

JOB=no

if [ "${IPMI_TYPE}" = "iDRAC6" ] || [ "${IPMI_TYPE}" = "iDRAC7" ] || [ "${IPMI_TYPE}" = "iDRAC8" ] || [ "${IPMI_TYPE}" = "iDRAC9" ] ; then
	RACADM=racadm

	if [ "${IPMI_TYPE}" = "iDRAC6" ] || [ "${IPMI_TYPE}" = "iDRAC7" ] ; then
		${RACADM} config -g cfgLanNetworking -o cfgDNSDomainNameFromDHCP 0
		${RACADM} config -g cfgIpmiLan -o cfgIpmiLanEnable 1

		${RACADM} config -g cfgNetTuning -o cfgNetTuningNicAutoneg 1
		${RACADM} config -g cfgNetTuning -o cfgNetTuningNicFullDuplex 1
		${RACADM} config -g cfgNetTuning -o cfgNetTuningNicMtu 1500

		# Serial over LAN config setup
		${RACADM} config -g cfgUserAdmin -o cfgUserAdminSolEnable 1 -i 2
		${RACADM} config -g cfgSerial -o cfgSerialBaudRate 115200
		${RACADM} config -g cfgSerial -o cfgSerialConsoleEnable 1
		${RACADM} config -g cfgSerial -o cfgSerialConsoleIdleTimeout 0
		${RACADM} config -g cfgSerial -o cfgSerialConsoleNoAuth 1
		${RACADM} config -g cfgSerial -o cfgSerialSshEnable 1
		${RACADM} config -g cfgSerial -o cfgSerialCom2RedirEnable 1
		${RACADM} config -g cfgIpmiSol -o cfgIpmiSolEnable 1
		${RACADM} config -g cfgIpmiSol -o cfgIpmiSolBaudRate 115200
		${RACADM} config -g cfgIpmiSerial -o cfgIpmiSerialBaudRate 115200
		${RACADM} config -g cfgIpmiSerial -o cfgIpmiSerialConnectionMode 1
		${RACADM} config -g cfgIpmiSerial -o cfgIpmiSerialHandshakeControl 1

		# Activate the web interface:
		${RACADM} config -g cfgRacTuning -o cfgRacTuneWebserverEnable 1
		${RACADM} config -g cfgRacTuning -o cfgRacTuneRemoteRacadmEnable 1
		${RACADM} config -g cfgRacTuning -o cfgRacTuneVirtualConsoleAuthorizeMultipleSessions 1

		# First boot device
		#${RACADM} config -g cfgServerInfo -o cfgServerFirstBootDevice PXE
	else
		# Make sure the user 2 is activated
		${RACADM} set iDRAC.Users.2.Enable Enabled

		# Various general BIOS settings
		${RACADM} set BIOS.SysSecurity.AcPwrRcvry On

		# IPMI ip configuration
		${RACADM} set iDRAC.IPv4.DHCPEnable Disabled
		${RACADM} set iDRAC.IPv4.DNSFromDHCP Disabled
		${RACADM} set iDRAC.IPv4.Enable Enabled
		${RACADM} set iDRAC.IPv4Static.DNSFromDHCP Disabled

		# IPMI NIC configuration
		${RACADM} set iDRAC.NIC.Failover All
		${RACADM} set iDRAC.NIC.Enable Enabled
		${RACADM} set iDRAC.IPMILan.Enable Enabled

		# IPMI serial config
		${RACADM} set BIOS.SerialCommSettings.RedirAfterBoot Enabled
		${RACADM} set BIOS.SerialCommSettings.ExtSerialConnector Serial1
		${RACADM} set BIOS.SerialCommSettings.ConTermType Vt100Vt220
		SERIAL_COM=$(${RACADM} get BIOS.SerialCommSettings.SerialComm | grep SerialComm= | cut -d= -f2)
		if ! [ "${SERIAL_COM}" = "OnConRedirCom2" ] ; then
			${RACADM} set BIOS.SerialCommSettings.SerialComm OnConRedirCom2
			JOB=yes
		fi
		SERIAL_PORT_ADDR=$(${RACADM} get BIOS.SerialCommSettings.SerialPortAddress | grep SerialPortAddress | cut -d= -f2)
		if ! [ "${SERIAL_PORT_ADDR}" = "Serial1Com1Serial2Com2" ] ; then
			${RACADM} set BIOS.SerialCommSettings.SerialPortAddress Serial1Com1Serial2Com2
			JOB=yes
		fi

		${RACADM} set iDRAC.IPMISerial.BaudRate 115200
		${RACADM} set iDRAC.IPMISerial.FlowControl RTS/CTS
		${RACADM} set iDRAC.IPMISerial.HandshakeControl Enabled
		${RACADM} set iDRAC.IPMISOL.BaudRate 115200
		${RACADM} set iDRAC.IPMISOL.Enable Enabled
		${RACADM} set iDRAC.Serial.BaudRate 115200
		${RACADM} set iDRAC.Serial.Enable Enabled
		${RACADM} set iDRAC.SerialRedirection.Enable Enabled

		# Enable the WEB interface
		${RACADM} set iDRAC.WebServer.Enable Enabled

		# SATA settings
		${RACADM} set BIOS.SataSettings.EmbSata AhciMode

		# Some CPU settings
		VIRTU_ON=$(${RACADM} get BIOS.ProcSettings.ProcVirtualization | grep ProcVirtualization | cut -d= -f2)
		if ! [ "${VIRTU_ON}" = "Enabled" ] ; then
			${RACADM} set BIOS.ProcSettings.ProcVirtualization Enabled
			JOB=yes
		fi
		ADJ_CACHE_LINE=$(${RACADM} get BIOS.ProcSettings.ProcAdjCacheLine | grep ProcAdjCacheLine | cut -d= -f2)
		if ! [ "${ADJ_CACHE_LINE}" = "Enabled" ] ; then
			${RACADM} set BIOS.ProcSettings.ProcAdjCacheLine Enabled
			JOB=yes
		fi
		if [ "${JOB}" = "yes" ] ; then
			# This creates a job on next reboot, as some settings needs this.
			${RACADM} jobqueue create BIOS.Setup.1-1
		fi
	fi
fi

exit 0
