#! /bin/bash
set -e

declare kversion="$1" # # We're passed the version of the kernel being installed
echo "Armbian: installing DTB for GRUB: $kversion" >&2

if [[ ! -f /etc/armbian-grub-with-dtb ]]; then
echo "Armbian: /etc/armbian-grub-with-dtb not found, skipping DTB for GRUB" >&2
exit 0
fi

declare BOOT_FDT_FILE OVERLAY_PREFIX
# shellcheck source=/dev/null
source /etc/armbian-grub-with-dtb
exec 9< /etc/armbian-grub-with-dtb
flock 9
declare target_dtb_file="/boot/armbian-dtb-${kversion}"
declare source_dtb_file="/usr/lib/linux-image-${kversion}/${BOOT_FDT_FILE}"
declare overlay_dir="/usr/lib/linux-image-${kversion}/${BOOT_FDT_FILE%%/*}/overlay"
declare tmp_dtb_file="${target_dtb_file}.tmp"
declare -a overlay_prefixes=() overlays=() user_overlays=() dtbos=()
declare name prefix found
declare -i merge_failed=0
trap 'rm -f "${tmp_dtb_file}"' EXIT

function armbian_env_value() {
sed -n "s/^${1}=//p" /boot/armbianEnv.txt | tr -d '\r' | tail -n 1
}
if [[ -n "${OVERLAY_PREFIX}" && -f /boot/armbianEnv.txt ]]; then
read -r -a overlay_prefixes <<< "$(armbian_env_value overlay_prefix)"
read -r -a overlays <<< "$(armbian_env_value overlays)"
read -r -a user_overlays <<< "$(armbian_env_value user_overlays)"
fi

for name in "${overlays[@]}"; do
found=""
for prefix in "${overlay_prefixes[@]}"; do
if [[ -f "${overlay_dir}/${prefix}-${name}.dtbo" ]]; then
found="${overlay_dir}/${prefix}-${name}.dtbo"
break
fi
done
if [[ -z "${found}" && -f "${overlay_dir}/${name}.dtbo" ]]; then
found="${overlay_dir}/${name}.dtbo"
fi
if [[ -z "${found}" ]]; then
echo "Armbian: overlay ${name} not found in ${overlay_dir}, skipping it" >&2
continue
fi
dtbos+=("${found}")
done

for name in "${user_overlays[@]}"; do
if [[ -f "/boot/overlay-user/${name}.dtbo" ]]; then
dtbos+=("/boot/overlay-user/${name}.dtbo")
else
echo "Armbian: user overlay ${name} not found in /boot/overlay-user, skipping it" >&2
fi
done

echo "Armbian: installing DTB for GRUB: $source_dtb_file -> $target_dtb_file" >&2
cp "${source_dtb_file}" "${tmp_dtb_file}"
if [[ ${#dtbos[@]} -gt 0 ]]; then
if ! command -v fdtoverlay > /dev/null; then
echo "Armbian: fdtoverlay not found, install device-tree-compiler" >&2
merge_failed=1
elif fdtoverlay -i "${source_dtb_file}" -o "${tmp_dtb_file}" "${dtbos[@]}"; then
echo "Armbian: applied overlays: ${dtbos[*]##*/}" >&2
else
echo "Armbian: applying the overlays failed" >&2
cp "${source_dtb_file}" "${tmp_dtb_file}"
merge_failed=1
fi
fi
# Only the overlay helper gets a failed merge as an error, package installs keep going
if [[ ${merge_failed} -ne 0 && -n "${ARMBIAN_GRUB_DTB_STRICT}" ]]; then
if [[ -f "${target_dtb_file}" ]]; then
echo "Armbian: keeping the previous ${target_dtb_file}" >&2
else
mv "${tmp_dtb_file}" "${target_dtb_file}"
echo "Armbian: installed the DTB without overlays" >&2
fi
exit 2
fi
mv "${tmp_dtb_file}" "${target_dtb_file}"
if [[ ${merge_failed} -ne 0 ]]; then
echo "Armbian: installed the DTB without overlays" >&2
fi
echo "Armbian: installing DTB for GRUB: done." >&2
