#!/bin/sh

set -ex

KERNEL_VERSION=$1
DTBO_SRC_DIR="/lib/firmware/${KERNEL_VERSION}/device-tree"
DTBO_DST_DIR="/boot/dtbo"
ARCH=$(uname -p)

DTBO_FILES=""
# Populate DTBO_FILES based on the ARCH
case "${ARCH}" in
	"armv7l")
		DTBO_FILES="imx6ull-14x14-evk-codec-wm8962.dtbo"
		;;
	*)
		echo "No DTBO needs to be handled"
		;;
esac

# Ensure the destination directory exists
mkdir -p "${DTBO_DST_DIR}"

# Copy each DTBO file
for DTBO in ${DTBO_FILES}; do
	if [ -f "${DTBO_SRC_DIR}/${DTBO}" ]; then
		cp -v "${DTBO_SRC_DIR}/${DTBO}" "${DTBO_DST_DIR}"
		echo "Copied ${DTBO} to ${DTBO_DST_DIR}"
	else
		echo "Warning: ${DTBO} not found in ${DTBO_SRC_DIR}, skipping"
	fi
done

sync
echo "All DTBO files processed for ARCH '${ARCH}' and kernel ${KERNEL_VERSION}"
