Code:
#!/bin/sh
# This script installs RockPro64 devicetree files into /boot when new kernels
# are installed, removes them when kernels are removed, and updates the
# "dtbs" and "dtbs.old" symlinks accordingly.
# It should be copied into /etc/kernel/postinst.d/ and /etc/kernel/postrm.d/
# and given execute permission.
# https://www.debian.org/doc/manuals/debian-kernel-handbook/ch-update-hooks.html
set -e
exec </dev/null >&2  # Don't use stdin or stdout
version="$1"
pkgname=${DPKG_MAINTSCRIPT_PACKAGE:-kernel package}
if [ -z "$version" ]; then
    echo "W: $pkgname did not pass a version number; exiting."
    exit 0
fi
eval set -- "$DEB_MAINT_PARAMS"
action="$1"
# return 0 if $1 matches any of the remaining args
among() {
    val="$1"
    shift
    for a in "$@"; do
        if [ "$val" = "$a" ]; then return 0; fi
    done
    return 1;
}
# update /boot/dtbs and /boot/dtbs.old
# expects $1 to be "configure" or "remove"
# expects $version to be set
# see also: linux-update-symlinks
update_symlinks() {
    # build a list of dtbs-<ver> symlink targets, highest priority first...
    # ...first priority is a newly installed kernel
    if [ "$action" = configure ]; then
        set -- "dtbs-$version"
    else
        set --
    fi
    if [ "$action" = remove ]; then
        rmtarget=dtbs-$version
    fi
    # ...next priority is the existing primary symlink (if not being removed)
    link1=/boot/dtbs
    target1="$(readlink "$link1" || true)"
    if [ -e "$link1" ] && ! among "$target1" "$@"; then
        if [ "$target1" != "$rmtarget" ]; then
            set -- "$@" "$target1"
        fi
    fi
    # ...next priority is the existing secondary symlink (if not being removed)
    link2=/boot/dtbs.old
    target2="$(readlink "$link2" || true)"
    if [ -e "$link2" ] && ! among "$target2" "$@"; then
        if [ "$target2" != "$rmtarget" ]; then
            set -- "$@" "$target2"
        fi
    fi
    # ...finally, any other installed kernels, newest first
    for v in $(linux-version list | linux-version sort --reverse); do
        if [ "$action" = remove ] && [ "$v" = "$version" ]; then
            continue;
        fi
        if ! among "dtbs-$v" "$@"; then
            set -- "$@" "dtbs-$v"
        fi
    done
    # create/replace/remove the primary and secondary symlinks
    if [ $# -ge 1 ]; then
        echo "I: creating symlink $link1 -> $1"
        ln -sfn "$1" "$link1"
    elif [ -L "$link1" ]; then
        echo "I: removing $link1"
        rm -f "$link1"
    fi
    if [ $# -ge 2 ]; then shift; fi
    if [ $# -ge 1 ]; then
        echo "I: creating symlink $link2 -> $1"
        ln -sfn "$1" "$link2"
    elif [ -L "$link2" ]; then
        echo "I: removing $link2"
        rm -f "$link2"
    fi
    if [ ! -e "$link1" ]; then
        echo "W: $link1 symlink is missing; unable to find a target for it"
    fi
}
srcdir=/usr/lib/linux-image-${version}/rockchip
destbase=dtbs-${version}
destdir=/boot/$destbase/rockchip
fname=rk3399-rockpro64-v2.dtb
if [ "$action" = configure ]; then
    if [ -f "$srcdir/$fname" ]; then
        echo "I: installing $destdir/$fname"
        mkdir -p "$destdir"
        cp "$srcdir/$fname" "$destdir/$fname"
    else
        echo "W: $pkgname doesn't contain $srcdir/$fname; making symlink anyway"
    fi
    update_symlinks "$action"
fi
if [ "$action" = remove ]; then
    update_symlinks "$action"
    if [ -f "$destdir/$fname" ]; then
        echo "I: removing $destdir/$fname"
        rm "$destdir/$fname"
        rmdir "$destdir" || true
        rmdir "/boot/$destbase" || true
    fi
fi
