rshimlog=$(which bfrshlog 2> /dev/null)
RC=0
err_msg=""
export LC_ALL=C

logfile=dpu.installation.log
LOG=/var/log/$logfile

fspath=$(readlink -f "$(dirname $0)")

if [ -x /usr/bin/flint ]; then
	FLINT=/usr/bin/flint
fi
FLINT=${FLINT:-mstflint}

cx_pcidev=${cx_pcidev:-$(lspci -nD 2> /dev/null | grep 15b3:a2d[26cf] | awk '{print $1}' | head -1)}
cx_dev_id=$(lspci -nD -s ${cx_pcidev} 2> /dev/null | awk -F ':' '{print strtonum("0x" $NF)}')
pciids=$(lspci -nD 2> /dev/null | grep 15b3:a2d[26cf] | awk '{print $1}')
flint_dev=$cx_pcidev
dpu_part_number=${dpu_part_number:-$($FLINT -d $flint_dev q full | grep "Part Number:" | awk '{print $NF}')}
PSID=$($FLINT -d $flint_dev q | grep PSID | awk '{print $NF}')

if [ -z "${dpu_part_number}" ]; then
	mst_dev=$(/bin/ls -1 /dev/mst/mt*_pciconf0 2> /dev/null)
	if [ ! -n "${mst_dev}" ]; then
		mst start > /dev/null 2>&1
	fi
	flint_dev=$(/bin/ls -1 /dev/mst/mt*_pciconf0 2> /dev/null)
	dpu_part_number=$($FLINT -d $flint_dev q full | grep "Part Number:" | awk '{print $NF}')
fi

total_weight=0
current_progress=0
declare -A step_weights
step_weights["install_setup"]=0
step_weights["dpu_os"]=0
step_weights["atf_uefi"]=0
step_weights["nic_firmware"]=0
step_weights["uefi_password"]=0
step_weights["bmc_password"]=0
step_weights["bmc_fw"]=0
step_weights["cec_fw"]=0
step_weights["dpu_golden_image"]=0
step_weights["nic_firmware_golden_image"]=0
step_weights["finished"]=100
step_weights["activate"]=100

rlog()
{
	msg=$(echo "$*" | sed 's/INFO://;s/ERROR:/ERR/;s/WARNING:/WARN/')
	if [ -n "$rshimlog" ]; then
		$rshimlog "$msg"
	fi
}

ilog()
{
	msg="[$(date +%H:%M:%S)] $*"
	echo "$msg" >> $LOG
	echo "$msg" > /dev/ttyAMA0
	echo "$msg" > /dev/hvc0
}

log()
{
	ilog "$*"
	rlog "$*"
}

calculate_total_weight()
{
	step_weights["install_setup"]=3

	if [ "$UPDATE_ATF_UEFI" == "yes" ]; then
		step_weights["atf_uefi"]=3
	fi

	if [ "$WITH_NIC_FW_UPDATE" == "yes" ]; then
		step_weights["nic_firmware"]=10
	fi

	if [[ ! -z "$BMC_USER" && ! -z "$BMC_PASSWORD" ]]; then
		if [[ ! -z "$UEFI_PASSWORD" && ! -z "$NEW_UEFI_PASSWORD" ]]; then
			step_weights["uefi_password"]=5
		fi
		if [ ! -z "$NEW_BMC_PASSWORD" ]; then
			step_weights["bmc_password"]=5
		fi
		if [ "$UPDATE_BMC_FW" == "yes" ]; then
			step_weights["bmc_fw"]=200
		fi
		if [ "$UPDATE_CEC_FW" == "yes" ]; then
			step_weights["cec_fw"]=5
		fi
		if [ "$UPDATE_DPU_GOLDEN_IMAGE" == "yes" ]; then
			step_weights["dpu_golden_image"]=60
		fi
		if [ "$UPDATE_NIC_FW_GOLDEN_IMAGE" == "yes" ]; then
			step_weights["nic_firmware_golden_image"]=60
		fi
	fi

	for weight in "${step_weights[@]}"; do
		((total_weight += weight))
	done
}

step2str()
{
	case "$1" in
	"install_setup")
		echo "BFB Installation Environment"
	;;
	"atf_uefi")
		echo "ARM Capsule"
	;;
	"nic_firmware")
		echo "NIC FW"
	;;
	"uefi_password")
		echo "UEFI Password"
	;;
	"bmc_password")
		echo "BMC Password"
	;;
	"bmc_fw")
		echo "BMC Image"
	;;
	"cec_fw")
		echo "Glacier Image"
	;;
	"dpu_golden_image")
		echo "DPU Golden Image"
	;;
	"nic_firmware_golden_image")
		echo "NIC FW Golden Image"
	;;
	"finished")
		echo "DPU Components Firmware"
	;;
	"activate")
		echo "Activation"
	;;
	esac
}

update_progress()
{
    local step=$1
	local status=$2
    local weight=${step_weights[$step]}

	if [ $weight -eq 0 ]; then
		if function_exists set_upgrade_progress; then
			set_upgrade_progress 0 0
		fi
		return
	fi
    ((current_progress += weight))
    if [ $total_weight -eq 0 ]; then
	    total_weight=$current_progress
    fi
    local percentage=$((current_progress * 100 / total_weight))

	if [[ $step == "finished" || $step == "activate" ]]; then
		percentage=100
	fi

	if [ $status -eq 0 ]; then
		status_str="passed"
	else
		status_str="failed"
	fi

	step_str=$(step2str $step)
	printf "BFB-Installer: Installing %s %s, total %d%% complete" "$step_str" "$status_str" $percentage
	printf "\n"
	ilog "BFB-Installer: Installing ${step_str} ${status_str}, total ${percentage}% complete"
	if function_exists set_upgrade_progress; then
		set_upgrade_progress $percentage $status
	fi
}

save_log()
{
	for pw in $(grep "PASSWORD=" $LOG | cut -d '=' -f 2- | sed 's/["'\'']//'g)
	do
		sed -i -e "s,$pw,xxxxxx,g" $LOG
	done
	sync
}

function_exists()
{
	declare -f -F "$1" > /dev/null
	return $?
}
