# Template file for 'dracut'
pkgname=dracut
version=059
revision=7
build_style=configure
configure_args="--prefix=/usr --sysconfdir=/etc"
conf_files="/etc/dracut.conf"
hostmakedepends="pkg-config asciidoc"
makedepends="libkmod-devel"
depends="bash coreutils cpio eudev gzip kmod>=3.7 kpartx util-linux"
short_desc="Low-level tool for generating an initramfs/initrd image"
maintainer="Zach Dykstra <dykstra.zachary@gmail.com>"
license="GPL-2.0-or-later, LGPL-2.0-or-later"
homepage="https://github.com/dracutdevs/dracut/wiki"
changelog="https://raw.githubusercontent.com/dracutdevs/dracut/master/NEWS.md"
distfiles="https://github.com/dracutdevs/dracut/archive/refs/tags/${version}.tar.gz"
checksum=eabf0bb685420c1e1d5475b6855ef787104508f0135ff570312845256e0fcecf
alternatives="
 initramfs:/etc/kernel.d/post-install/20-initramfs:/usr/libexec/dracut/kernel-hook-postinst
 initramfs:/etc/kernel.d/post-remove/20-initramfs:/usr/libexec/dracut/kernel-hook-postrm
"
subpackages="dracut-network"
# Checks require distfiles come from a git repository
make_check=no

make_dirs="
 /etc/dracut.conf.d 0755 root root
 /usr/lib/dracut/dracut.conf.d 0755 root root"

if [ "$XBPS_TARGET_LIBC" = "musl" ]; then
	makedepends+=" musl-fts-devel"
fi

case "$XBPS_TARGET_MACHINE" in
	i686*|x86_64*) # archs supported by dracut for EFI bundle
		subpackages+=" dracut-uefi"
	;;
esac

pre_build() {
	# Upstream tarball is missing version information
	DRACUT_VERSION="${version}" make dracut-version.sh
}

post_install() {
	# kernel hooks.
	vinstall ${FILESDIR}/kernel-hook-postinst 755 usr/libexec/dracut
	vinstall ${FILESDIR}/kernel-hook-postrm 755 usr/libexec/dracut

	# We don't need the systemd stuff.
	rm -r ${DESTDIR}/usr/lib/dracut/modules.d/*systemd*

	# systemd dependent additional dracut modules
	for f in 06rngd 06dbus-broker 06dbus-daemon 09dbus \
		35connman 35network-manager 35network-wicked 80lvmmerge \
		99memstrack 99squash; do
		rm -r ${DESTDIR}/usr/lib/dracut/modules.d/${f}
	done

	# don't need s390x architecture dependent dracut modules
	for f in 80cms 81cio_ignore 91zipl 95dasd 95dasd_mod \
		95dasd_rules 95dcssblk 95qeth_rules 95zfcp \
		95zfcp_rules 95znet; do
		rm -r ${DESTDIR}/usr/lib/dracut/modules.d/${f}
	done

	# ifcfg network-scripts dependent (RHEL-derived)
	rm -r ${DESTDIR}/usr/lib/dracut/modules.d/45ifcfg

	rm ${DESTDIR}/usr/share/man/man8/*.service.*
	rm -r ${DESTDIR}/usr/lib/kernel
}

dracut-network_package() {
	depends="dhclient ${sourcepkg}-${version}_${revision}"
	short_desc+=" - network modules"
	pkg_install() {
		for f in 35network-legacy 40network 45url-lib \
			90kernel-network-modules 90qemu-net 90livenet \
			95cifs 95fcoe 95fcoe-uefi 95iscsi 95nbd 95nfs 95ssh-client; do
			vmove usr/lib/dracut/modules.d/${f}
		done
	}
}

dracut-uefi_package() {
	depends="binutils gummiboot-efistub ${sourcepkg}>=${version}_${revision}"
	short_desc+=" - UEFI bundle hook"
	conf_files="/etc/default/dracut-uefi-hook"

	pkg_install() {
		vinstall ${FILESDIR}/dracut-uefi-hook.confd 644 etc/default dracut-uefi-hook
		vinstall ${FILESDIR}/kernel-uefi-hook-postinst 755 etc/kernel.d/post-install 20-dracut-uefi
		vinstall ${FILESDIR}/kernel-uefi-hook-postrm 755 etc/kernel.d/post-remove 20-dracut-uefi
	}
}
