#!/bin/sh

version="$1"
bootdir=/boot

[ -x /usr/bin/dracut ] || exit 0

# passing the kernel version is required
[ -z "${version}" ] && exit 0

# exit if custom kernel does not need an initramfs
[ "$INITRD" = 'No' ] && exit 0

# absolute file name of kernel image may be passed as a second argument;
# create the initrd in the same directory
if [ -n "$2" ]; then
	bootdir=$(dirname "$2")
fi


echo ===== $0
echo DPKG_MAINTSCRIPT_PACKAGE $DPKG_MAINTSCRIPT_PACKAGE
echo DEB_MAINT_PARAMS $DEB_MAINT_PARAMS
echo ===== $0


# avoid running multiple times
if [ -n "$DEB_MAINT_PARAMS" ]; then
        eval set -- "$DEB_MAINT_PARAMS"
        if [ -z "$1" ] || [ "$1" != "configure" ]; then
                exit 0
        fi
fi

# case "$DPKG_MAINTSCRIPT_PACKAGE" in
#     # if DEB_MAINT_PARAMS == configure and linux-image
#     linux-image-*)  ;; # do create the initrd

#     # trigger will be activated by linux-image-*
#     dracut) if dpkg-trigger --no-await update-initramfs; then
#             echo "update-initramfs: deferring update (trigger activated)"
#             exit 0
#        fi
#        ;;
# esac


# check if modules.dep already exists. If not create it
# maybe this problem could also be solved via Debian triggers
if [ ! -f $bootdir/../lib/modules/$version/modules.dep ]; then
    depmod -a -F $bootdir/System.map-$version $version
fi

# we're good - create initramfs
echo "dracut: Generating $bootdir/initrd.img-${version}"
dracut -q --force $bootdir/initrd.img-${version} "${version}" >&2
