#!/bin/sh
# /etc/kernel/preinst.d/aa-jd2-clean-initrd
#
# Runs in the preinst phase (before dpkg unpacks files, before postinst).
# Removes any stale /boot/initrd.img-$KVER so the postinst chain's
# /etc/kernel/postinst.d/initramfs-tools hook (which hardcodes
# `update-initramfs -c`) succeeds on same-KVER reinstall — the migration
# case where PlymouthThemeMaintenanceTask already created an initrd for
# this KVER under the zip-overlay install.
#
# aa- prefix orders us first in the preinst.d alphabetical chain. Match
# -jd2$ so we don't disturb stock raspi kernel preinst flows.

set -e

KVER="$1"

case "${KVER}" in
    *-jd2|*-jd2.*) ;;   # old terminal "-jd2" AND new "-jd2.r<N>.g<hash>" KVER
    *) exit 0 ;;
esac

if [ -e "/boot/initrd.img-${KVER}" ]; then
    rm -f "/boot/initrd.img-${KVER}"
fi

exit 0
