# Generate kernel and rootfs image for Qcom ABL booting
declare -g BOARD_NAME="Ayn Odin2"
declare -g BOARD_MAINTAINER="FantasyGmm"
declare -g BOARDFAMILY="qcom-abl"
declare -g KERNEL_TARGET="sm8550"
declare -g KERNELPATCHDIR="arm64-sm8550"
declare -g EXTRAWIFI="no"
declare -g BOOTCONFIG="none"
declare -g BOOTIMG_CMDLINE_EXTRA="clk_ignore_unused pd_ignore_unused panic=30 audit=0 allow_mismatched_32bit_el0 rw mem_sleep_default=s2idle"
declare -g IMAGE_PARTITION_TABLE="gpt"
declare -g -a ABL_DTB_LIST=("qcs8550-ayn-odin2-hypdtbo")

# Use the full firmware, complete linux-firmware plus Armbian's
declare -g BOARD_FIRMWARE_INSTALL="-full"

function post_family_config_branch_sm8550__edk2_kernel() {	
	declare -g KERNELSOURCE='https://github.com/edk2-porting/linux-next'
	declare -g KERNEL_MAJOR_MINOR="6.7" # Major and minor versions of this kernel.
	declare -g KERNELBRANCH="branch:integration/ayn-odin2"
	declare -g LINUXCONFIG="linux-${ARCH}-${BRANCH}" # for this board: linux-arm64-sm8550
	display_alert "Setting up kernel ${KERNEL_MAJOR_MINOR} for" "${BOARD}" "info"
}

function ayn-odin2_is_userspace_supported() {
	[[ "${RELEASE}" == "trixie" ]] && return 0
	[[ "${RELEASE}" == "noble" ]] && return 0
	return 1
}

function post_family_tweaks__ayn-odin2_enable_services() {
	if ! ayn-odin2_is_userspace_supported; then
		if [[ "${RELEASE}" != "" ]]; then
			display_alert "Missing userspace for ${BOARD}" "${RELEASE} does not have the userspace necessary to support the ${BOARD}" "warn"
		fi
		return 0
	fi

	if [[ "${RELEASE}" == "noble" ]];then
		display_alert "Adding Mesa PPA For Ubuntu " "${BOARD}" "info"
		do_with_retries 3 chroot_sdcard add-apt-repository ppa:oibaf/graphics-drivers --yes --no-update
	fi

	# We need unudhcpd from armbian repo, so enable it
	mv "${SDCARD}"/etc/apt/sources.list.d/armbian.list.disabled "${SDCARD}"/etc/apt/sources.list.d/armbian.list

	# Add zink env
	echo '__GLX_VENDOR_LIBRARY_NAME=mesa' | tee -a "${SDCARD}"/etc/environment
	echo 'MESA_LOADER_DRIVER_OVERRIDE=zink' | tee -a "${SDCARD}"/etc/environment
	echo 'GALLIUM_DRIVER=zink' | tee -a "${SDCARD}"/etc/environment
	# Add Gamepad udev rule
	echo 'SUBSYSTEM=="input", ATTRS{name}=="Ayn Odin2 Gamepad", MODE="0666", ENV{ID_INPUT_MOUSE}="0", ENV{ID_INPUT_JOYSTICK}="1"'> "${SDCARD}"/etc/udev/rules.d/99-ignore-gamepad.rules
	# Add video decoder/encoder udev rule
	echo 'SUBSYSTEM=="video4linux", ATTR{name}=="qcom-venus-decoder", SYMLINK+="video-dec%n"' >> "${SDCARD}"/etc/udev/rules.d/90-browser-video.rules
	echo 'SUBSYSTEM=="video4linux", ATTR{name}=="qcom-venus-encoder", SYMLINK+="video-enc%n"' >> "${SDCARD}"/etc/udev/rules.d/90-browser-video.rules
	# No driver support for suspend
	chroot_sdcard systemctl mask suspend.target
	# Add Bt Mac Fixed service
	install -Dm655 $SRC/packages/bsp/ayn-odin2/bt-fixed-mac.sh "${SDCARD}"/usr/local/bin/
	install -Dm644 $SRC/packages/bsp/ayn-odin2/bt-fixed-mac.service "${SDCARD}"/usr/lib/systemd/system/
	chroot_sdcard systemctl enable bt-fixed-mac

	do_with_retries 3 chroot_sdcard_apt_get_update
	display_alert "$BOARD" "Installing board tweaks" "info"
	do_with_retries 3 chroot_sdcard_apt_get_install alsa-ucm-conf unudhcpd mkbootimg git

	# Disable armbian repo back
	mv "${SDCARD}"/etc/apt/sources.list.d/armbian.list "${SDCARD}"/etc/apt/sources.list.d/armbian.list.disabled
	do_with_retries 3 chroot_sdcard_apt_get_update

	do_with_retries 3 chroot_sdcard_apt_get_install mesa-vulkan-drivers qbootctl qrtr-tools protection-domain-mapper tqftpserv

	return 0
}

function post_family_tweaks_bsp__ayn-odin2_bsp_firmware_in_initrd() {
	random_mac=$(openssl rand -hex 6 | sed 's/\(..\)/\1:/g; s/.$//')
	declare -g BOOTIMG_CMDLINE_EXTRA="${BOOTIMG_CMDLINE_EXTRA} bt_mac=${random_mac}"
	display_alert "Generate a random Bluetooth MAC address, Mac:${random_mac}" "info"
	display_alert "Adding to bsp-cli" "${BOARD}: firmware in initrd" "info"
	declare file_added_to_bsp_destination # Will be filled in by add_file_from_stdin_to_bsp_destination
	# Using odin2's firmware for now
	add_file_from_stdin_to_bsp_destination "/etc/initramfs-tools/hooks/ayn-odin2-firmware" <<- 'FIRMWARE_HOOK'
		#!/bin/bash
		[[ "$1" == "prereqs" ]] && exit 0
		. /usr/share/initramfs-tools/hook-functions
		for f in /lib/firmware/qcom/sm8550/ayn/odin2/* ; do
			add_firmware "${f#/lib/firmware/}"
		done
		add_firmware "qcom/a740_sqe.fw" # Extra one for dpu
		add_firmware "qcom/gmu_gen70200.bin" # Extra one for gpu
		# Extra one for wifi
		for f in /lib/firmware/ath12k/WCN7850/hw2.0/* ; do
			add_firmware "${f#/lib/firmware/}"
		done
		# Extra one for bt
		for f in /lib/firmware/qca/* ; do
			add_firmware "${f#/lib/firmware/}"
		done	
	FIRMWARE_HOOK
	run_host_command_logged chmod -v +x "${file_added_to_bsp_destination}"
}
