update_grub_config()
{
        if [ -f /boot/grub/grub.cfg ]; then
                /usr/bin/grub-mkconfig -o /boot/grub/grub.cfg
        fi
}

remove_old_initrd()
{
	# old initramfs-name
	_OLD_FILENAME=initramfs-${1/-/-fw}.img

	# Remove only the old version to be compatible with lts
	echo "Removing /boot/${_OLD_FILENAME}"
	find /boot -type f -name "${_OLD_FILENAME}" -delete

	# also remove old initrd style files
	find /boot -type f -name "initrd-*fw*" -delete

	# also remove old LTS files
	find /boot -type f -name "initramfs-6.1.*.img" -delete
}

post_install()
{

	# Creating initrd
	# Note: don't default to non-host-only since it overrides user options in dracut.conf*
	/usr/bin/dracut --kver @_F_kernelmod_uname@

	update_grub_config
}

post_upgrade()
{
        # cleanup
	remove_old_initrd $2

	# Create new initrd but this time value taken from dracut config file
	# we need force bc the relbumps , we installed already another kernel
	/usr/bin/dracut --force --kver  @_F_kernelmod_uname@

	# well just to be sure right ?
	if [ ! -e /boot/initramfs-@_F_kernelmod_uname@.img ]; then
		echo "Cannot find the just created initramfs!!.. Trying to recover.."
		/usr/bin/dracut --force --kver  @_F_kernelmod_uname@
	fi

	## paranoia!! but better tell what's going on and do not let the user reboot
	if [ ! -e /boot/initramfs-@_F_kernelmod_uname@.img ]; then
		echo "ERROR occured , DO NOT REBOOT YOUR BOX.."
		echo "Run the following comand manually ( as root or using sudo):"
		echo "   /usr/bin/dracut --force --kver  @_F_kernelmod_uname@"
	fi

	update_grub_config
}

op=$1
shift
$op $*

# vim: ft=sh
