#!/bin/sh

# Hook script to update grub on kernel installation/removal.
# Put this script or symlinks to it in /etc/kernel/{postinst,postrm}.d/

# NB: this script must be run after any initrd script.

# Kernels built with kernel-package set DEB_MAINT_PARAMS to the
# phase of the package installation/removal (configure, remove, purge, etc.)
# and this is used to run the script only once. Official kernel packages
# unfortunately don't provide this means and so the script is run twice
# on purge

set -eu

if echo "$0" | grep -q '/postrm.d/'; then

    if [ -n "${DEB_MAINT_PARAMS:-}" ]; then
        eval set -- "${DEB_MAINT_PARAMS}"
        if [ "x$1" != "xremove" ]; then
            echo "not running update-grub" >&2
            exit 0
        fi
    fi

elif echo "$0" | grep -q '/postinst.d/'; then

    if [ -n "${DEB_MAINT_PARAMS:-}" ]; then
        eval set -- "${DEB_MAINT_PARAMS}"
        if [ "x$1" != "xconfigure" ]; then
            echo "not running update-grub" >&2
            exit 0
        fi
    fi

fi

echo "running update-grub" >&2
/usr/sbin/update-grub


