#!/bin/bash

# + ============================================================== +
# Copyright (c) 2025, Intel Corporation.

# This source code and any documentation accompanying it ("Material") is furnished
# under license and may only be used or copied in accordance with the terms of that
# license.  No license, express or implied, by estoppel or otherwise, to any
# intellectual property rights is granted to you by disclosure or delivery of these
# Materials.  The Materials are subject to change without notice and should not be
# construed as a commitment by Intel Corporation to market, license, sell or support
# any product or technology.  Unless otherwise provided for in the license under which
# this Material is provided, the Material is provided AS IS, with no warranties of
# any kind, express or implied, including without limitation the implied warranties
# of fitness, merchantability, or non-infringement.  Except as expressly permitted by
# the license for the Material, neither Intel Corporation nor its suppliers assumes
# any responsibility for any errors or inaccuracies that may appear herein.  Except
# as expressly permitted by the license for the Material, no part of the Material
# may be reproduced, stored in a retrieval system, transmitted in any form, or
# distributed by any means without the express written consent of Intel Corporation.

# Module Name:  bios_update.sh

# Abstract:  Shell Script file for invoking system software updates for
# Intel(R) Server Board D50DNP Family.

CURRENT_PATH=`pwd`
BASE_PATH="sysfwupdt/Linux_x64"
BIOS="R01.02.0006_DenaliPass_EBG_EMR_UpdateCapsule_prd.bin"
ARG=$1

refresh_terminal()
{
	echo "Refreshing environment ..."
	if [[ $PATH != *"sysfwupdt"* ]]; then
		PATH=$PATH:/usr/bin/sysfwupdt
	else
		echo "Sysfwupdt entry is already present in environment path"
	fi
	source ~/.bashrc
}

biosupdate()
{
	echo -e "\n\n"
	echo "===================================================== BIOS UPDATE ====================================================="
	echo "Updating BIOS ..."
	echo "sysfwupdt -u $BIOS ImmReset"
	sysfwupdt -u $BIOS ImmReset
}

restart()
{
	if [ -z $ARG ]; then
		echo -e "\n\n"
		echo "Restarting system in 1 minute, System will take approximately 10 minutes to boot back"
		shutdown -r +1
		echo -e "\n\n"
		echo "**************************************************************"
		echo "To abort the scheduled restart, use 'shutdown -c' command."
		echo "**************************************************************"
	fi
}


startupdate()
{
	if [ -z $ARG ]; then
		sh sysfwupdt_install.sh no_restart
		refresh_terminal
	fi
	biosupdate
	restart
}


# Triggering updates
startupdate