acpi_dir=/boot/acpi_hotpatch
export acpi_dir

function _acpi {
	if [ -f "$1" ]; then
		echo "$acpi_string1 ($1)..."
		if ! acpi "$1"; then
			echo "$acpi_string2 $1"
			sleep 3
		fi
	else
		echo "$acpi_string3 $1"
		sleep 3
	fi
}

function acpi_hotpatch {
	if ! [ -d "$acpi_dir" ]; then
		return 0
	fi

	# translations are in the grub2 package
	set acpi_string1=$"Applying modified ACPI table"
	set acpi_string2=$"ERROR! Failed to apply modified ACPI table: "
	set acpi_string3=$"ERROR! Modified ACPI table file not found: "

	# See https://www.hpe.com/psnow/resources/ebooks/a00114902en_us_v2/SMBIOS_Specification.html

	# Workaround for buggy BIOS firmware (some do not support bios_major/minor/version, but system_manufacturer)
	if ! smbios --type 1 --get-string 4 --set system_manufacturer; then
		return 0
	fi
	if [ "$system_manufacturer" != "TIMI" ]; then
		return 0
	fi

	smbios --type 0 --get-byte 20 --set bios_major
	smbios --type 0 --get-byte 21 --set bios_minor
	smbios --type 0 --get-string 5 --set bios_version

	smbios --type 1 --get-string 5 --set system_productname
	smbios --type 1 --get-string 6 --set system_version
	smbios --type 1 --get-string 25 --set system_skunumber
	smbios --type 1 --get-string 26 --set system_family

	smbios --type 2 --get-string 4 --set baseboard_manufacturer
	smbios --type 2 --get-string 5 --set baseboard_productname
	smbios --type 2 --get-string 6 --set baseboard_version

	if [ "$system_manufacturer" = "TIMI" ]; then
		if [ "$system_productname" = "Xiaomi Book Pro 14 2022" ]; then
			if [ "$baseboard_productname" = "TM2119" ]; then
				if [ "$bios_major" == 1 -a "$bios_minor" == 11 ]; then
					_acpi "$acpi_dir"/mibook14.aml
				fi
			fi
		fi
	fi
}
