#!/bin/sh -e
# NAME=Firmware reflashing
# DESCRIPTION=Firmware reflashing
# DESTROYS_HDD=false
# IS_INTERACTIVE=false
# POWEROFF_DURING_TEST=true
# VERSION=0.1
# TAGS=bios,firmware
# DEPENDS=Mainboard,Disk Controller

# UNTESTED DEVELOPMENT VERSION!

. /usr/share/inquisitor/functions-test

exit_handler()
{
	[ -r "$FIRMWARES" ] && rm $FIRMWARES
}

FIRMWARES=`mktemp`

get_needed_firmwares_list > $FIRMWARES
[ ! -s $FIRMWARES ] && test_succeeded "No firmwares needed to be updated"

cat $FIRMWARES | while read component; do
	# Parse each component
	type=`echo $component | awk -F :: '{print $1}'`
	version=`echo $component | awk -F :: '{print $2}'`
	firmware_file=`echo $component | awk -F :: '{print $3}'`

	# Detect version
	case $type in
	"BMC")
		current_version=`ipmitool mc info | grep "Firmware Revision" | awk '{print $NF}'`
		;;
	"Mainboard")
		current_version=`grep -A 3 "BIOS Information" $HOME/dmi.log | 
					sed -n '/Version:/s/^.*Version: \(.*\)$/\1/pg'`
		;;
	"Disk Controller")
		if [ `einarc -l | wc -l` -eq 0 ]; then
			print_red_message "Disk controller does not supported by einarc"
			print_red_message "Do you want to either (s)kip this stage or to (f)orce firmware update?"
			read choice
			if [ "$choice" = "s" ]; then
				current_version=$version
			else
				current_version="Unsupported by einarc"
			fi
		else
			einarc_module=`einarc -l | awk '{print $1}' | sed -n '1p'`
			if [ `einarc -l | grep "$einarc_module" | wc -l` -gt 1 ]; then
				print_red_message "There are more than one disk controller of ony type installed"
				print_red_message "Currently only some controllers can be flashed automatically"
				print_red_message "Do you want to either (s)kip this stage or to (f)orce firmware update?"
				read choice
				if [ "$choice" = "s" ]; then
					current_version=$version
				else
					for adapter_num in `einarc -l | grep "$einarc_module" | awk '{print $2}'`; do
						current_version=`einarc -t "$einarc_module" -a $adapter_num adapter info |
							sed -n '/Firmware version/s/^Firmware version\s*\(.*\)$/\1/pg'`
						if [ "$current_version" = $version ]; then
							true
						else
							print_green_message "Trying to update firmware on $adapter_num adapter..."
							cd $SHARE_DIR/firmwares
							if einarc -t "$einarc_module" -a $adapter_num firmware write $firmware_file; then
								true
							else
								current_version="Unable to update firmware via einarc."
								break
							fi
						fi
					done
					print_green_message "Rebooting for applying hardware changes..."
					sleep 30
					hard_reboot
				fi
			else
				current_version=`einarc -t "$einarc_module" adapter info |
					sed -n '/Firmware version/s/^Firmware version\s*\(.*\)$/\1/pg'`
				if [ "$current_version" = $version ]; then
					true
				else
					print_green_message "Trying to update firmware..."
					cd $SHARE_DIR/firmwares
					if einarc -t "$einarc_module" firmware write $firmware_file; then
						print_green_message "Rebooting for applying hardware changes..."
						sleep 30
						hard_reboot
					else
						current_version="Unable to update firmware via einarc."
					fi
				fi
			fi
		fi
		;;
	esac

	# Reboot with necessary reflasher
	[ "$current_version" != $version ] || continue
	print_red_message "Necessary $type firmware version does not found!"
	print_red_message "Found \"$current_version\", instead of \"$version\""
	print_red_message "Going to reflash with \"$firmware_file\""
	boot_from_image "$firmware_file"
	sleep 30
	hard_reboot
done

# If all firmwares are already updates
test_succeeded
