#!/bin/sh -e

# Copyright (C) 2024, John Clark <inindev@gmail.com>

# kernel post-install hook: /etc/kernel/postinst.d
# script to copy the specified dtb file to /boot
# after a new kernel is package installed

version="$1"
source1='/boot/<DTB_FILE>'
source2="/usr/lib/linux-image-${version}/rockchip/<DTB_FILE>"
target="/boot/<DTB_FILE>-${version}"

# passing the kernel version is required
if [ -z "${version}" ]; then
	echo >&2 "E: dtb_cp: ${DPKG_MAINTSCRIPT_PACKAGE:-kernel package} did not pass a version number"
	exit 2
fi

if [ -e "${source1}" ]; then
	echo -n "I: dtb_cp: symlink "
	ln -sfv "$(basename "${source1}")" "${target}"
elif [ -e "${source2}" ]; then
	echo "I: dtb_cp: installing ${source2} to ${target}"
	install -vm 644 "${source2}" "${target}"
else
	echo >&2 "E: dtb_cp: neither ${source1} nor ${source2} found"
	exit 3
fi

