#!/bin/sh

# Copyright 2015,2016 Cumulus Networks, Inc.
# All rights reserved.

# This is a kernel postinst/postrm script that copies/removes the
# device tree dtb in /boot for GRUB.

# The guts of the script are shamelessly borrowed from
# /etc/kernel/postinst.d/zz-update-grub.

set -e

if [ -n "$1" ] ; then
	LINUX_KERNEL_VERSION="$1"
fi

. /usr/share/onie-u-boot/functions

DTB_PATH="$(get_dtb_path)"
GRUB_DTB="/boot/dtb-$(get_kernel_version)"

set -- $DEB_MAINT_PARAMS
mode="${1#\'}"
mode="${mode%\'}"
case $0:$mode in
	# Only run on postinst configure and postrm remove, to avoid
	# wasting time.  Also run if we have no DEB_MAINT_PARAMS, in order
	# to work with old kernel packages.
	*/postinst.d/*:|*/postinst.d/*:configure)
		# Install the platform specific .dtb file
		if [ ! -r "$DTB_PATH" ] ; then
			logerr "DTB file does not exist: $DTB_PATH"
			exit 1
		fi
		/bin/cp -fa "$DTB_PATH" "$GRUB_DTB"
		;;
	*/postrm.d/*:|*/postrm.d/*:remove)
		# Remove the platform specific .dtb file
		rm -f "$GRUB_DTB"
		;;
esac

exit 0
