#!/bin/sh
#
# Trigger to regenerate an initramfs for every kernel with a module directory
# in lib/modules on package post-install.
#
# To change the initramfs generator, edit or create the file
# etc/default/initramfs-regenerate and add or change the line
#
#     INITRAMFS_GENERATOR=<generator>
#
# where <generator> is one of "dracut", "mkinitcpio" or "none". By default, a
# value of "dracut" is assumed.
#
# Arguments:	$ACTION = [run/targets]
#		$TARGET = [post-install/post-remove]
#		$PKGNAME
#		$VERSION
#		$UPDATE = [yes/no]

ACTION="$1"
TARGET="$2"
PKGNAME="$3"
VERSION="$4"
UPDATE="$5"

export PATH="usr/bin:/usr/sbin:/usr/bin:/sbin:/bin"

case "$ACTION" in
	targets)
		echo "post-install post-remove"
		exit 0
		;;
	run)
		;;
	*)
		exit 1
		;;
esac

# Read the configuration, if it exists
[ -f etc/default/initramfs-regenerate ] && . etc/default/initramfs-regenerate

# dracut explicitly sets umask 0077, other generators may not
umask 0077

case "${INITRAMFS_GENERATOR:-dracut}" in
	dracut)
		if command -v dracut >/dev/null 2>&1; then
			echo "Regenerating initramfs with dracut"
			dracut -f -q --regenerate-all
		fi
		;;
	mkinitcpio)
		if command -v mkinitcpio >dev/null 2>&1; then
			echo "Regenerating initramfs with mkinitcpio"
			# Regenerate images for every kernel version with modules
			for kdir in usr/lib/modules/*; do
				[ -d "${kdir}/kernel" ] || continue
				kver="${kdir##*/}"
				mkinitcpio -g "boot/initramfs-${kver}.img" -k "${kver}"
			done
		fi
		;;
	none)
		;;
	*)
		echo "unrecognized INITRAMFS_GENERATOR for initramfs-regenerate hook"
		exit 1
		;;
esac

exit 0
# end
